Subversion Repositories Kolibri OS

Rev

Rev 1145 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. include 'proc32.inc'
  3.  
  4. DLL_ENTRY equ 1
  5. DLL_EXIT  equ -1
  6. REQ_DLL_VER equ 4
  7.  
  8. use32
  9.         db      'MENUET01'
  10.         dd      1
  11.         dd      start
  12.         dd      i_end
  13.         dd      mem
  14.         dd      mem
  15.         dd      0
  16.         dd      0
  17.  
  18. start:
  19.         stdcall load_dll_and_import, dllname, imports
  20.         test    eax, eax
  21.         jz      exit
  22.  
  23. ; check version
  24.         cmp     word [dll_ver], REQ_DLL_VER
  25.         jb      exit
  26.         cmp     word [dll_ver+2], REQ_DLL_VER
  27.         ja      exit
  28.         push    DLL_ENTRY
  29.         call    [dll_start]
  30.  
  31. ; yes! Now do some work (gets2() demo in this case).
  32.  
  33.         push    caption
  34.         push    25
  35.         push    80
  36.         push    25
  37.         push    80
  38.         call    [con_init]
  39.  
  40. ; C-equivalent of the following code:
  41. ; for (;;)
  42. ; {
  43. ;   con_write_asciiz("Enter string (empty for exit): ");
  44. ;   if (!con_gets2(mycallback,s,256)) break;
  45. ;   if (s[0] == '\n') break;
  46. ;   con_write_asciiz("You entered: ");
  47. ;   con_write_asciiz(s);
  48. ; }
  49. mainloop:
  50.         push    str1
  51.         call    [con_write_asciiz]
  52.         push    256
  53.         push    s
  54.         push    mycallback
  55.         call    [con_gets2]
  56.         test    eax, eax
  57.         jz      done
  58.         cmp     [s], 10
  59.         jz      done
  60.         push    str2
  61.         call    [con_write_asciiz]
  62.         push    s
  63.         call    [con_write_asciiz]
  64.         jmp     mainloop
  65. done:
  66.         push    1
  67.         call    [con_exit]
  68. exit:
  69.         or      eax, -1
  70.         int     0x40
  71.  
  72. proc mycallback stdcall, keycode:dword, pstr:dword, pn:dword, ppos:dword
  73.         mov     eax, [keycode]
  74.         cmp     al, 0x0F
  75.         jz      .tab
  76.         cmp     al, 0x3B
  77.         jz      .f1
  78.         cmp     al, 0x48
  79.         jz      .up
  80.         cmp     al, 0x50
  81.         jz      .down
  82.         xor     eax, eax
  83.         ret
  84. .tab:
  85. ; Tab pressed - insert "[autocomplete]" to current position
  86.         push    esi edi
  87.         mov     eax, [ppos]
  88.         mov     eax, [eax]
  89.         mov     ecx, [pn]
  90.         mov     ecx, [ecx]
  91.         mov     esi, [pstr]
  92.         mov     esi, [esi]
  93.         add     ecx, esi
  94.         add     esi, eax
  95.         mov     edx, esi
  96. @@:
  97.         lodsb
  98.         test    al, al
  99.         jnz     @b
  100.         lea     edi, [esi+str3.len]
  101.         cmp     edi, ecx
  102.         jbe     @f
  103.         mov     edi, ecx
  104.         lea     esi, [edi-str3.len]
  105. @@:
  106.         cmp     esi, edx
  107.         jbe     @f
  108.         dec     esi
  109.         dec     edi
  110.         mov     al, [esi]
  111.         mov     [edi], al
  112.         jmp     @b
  113. @@:
  114.         cmp     edi, ecx
  115.         jb      @f
  116.         dec     edi
  117. @@:
  118.         mov     ecx, edi
  119.         sub     ecx, edx
  120.         mov     edi, edx
  121.         mov     esi, str3
  122.         rep     movsb
  123.         mov     eax, [pstr]
  124.         sub     edi, [eax]
  125.         mov     eax, [ppos]
  126.         mov     [eax], edi
  127.         pop     edi esi
  128.         xor     eax, eax
  129.         inc     eax
  130.         ret
  131. .f1:
  132. ; F1 pressed - say message
  133.         push    str4
  134.         call    [con_write_asciiz]
  135.         push    str1
  136.         call    [con_write_asciiz]
  137.         push    2
  138.         pop     eax
  139.         ret
  140. .up:
  141.         push    esi
  142.         mov     esi, str5
  143.         mov     ecx, str5.len
  144.         jmp     @f
  145. .down:
  146.         push    esi
  147.         mov     esi, str6
  148.         mov     ecx, str6.len
  149. @@:
  150.         push    edi
  151.         mov     edi, [pstr]
  152.         mov     edi, [edi]
  153.         mov     eax, [ppos]
  154.         mov     [eax], ecx
  155.         rep     movsb
  156.         xor     eax, eax
  157.         stosb
  158.         pop     edi esi
  159.         inc     eax
  160.         ret
  161. endp
  162.  
  163. proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
  164.         pushad
  165. ; load DLL
  166.         push    68
  167.         pop     eax
  168.         push    19
  169.         pop     ebx
  170.         mov     ecx, [_dllname]
  171.         int     0x40
  172.         test    eax, eax
  173.         jz      import_fail
  174.  
  175. ; initialize import
  176.         mov     edi, eax
  177.         mov     esi, [_imports]
  178. import_loop:
  179.         lodsd
  180.         test    eax, eax
  181.         jz      import_done
  182.         mov     edx, edi
  183. import_find:
  184.         mov     ebx, [edx]
  185.         test    ebx, ebx
  186.         jz      import_not_found
  187.         push    eax
  188. @@:
  189.         mov     cl, [eax]
  190.         cmp     cl, [ebx]
  191.         jnz     import_find_next
  192.         test    cl, cl
  193.         jz      import_found
  194.         inc     eax
  195.         inc     ebx
  196.         jmp     @b
  197. import_find_next:
  198.         pop     eax
  199.         add     edx, 8
  200.         jmp     import_find
  201. import_found:
  202.         pop     eax
  203.         mov     eax, [edx+4]
  204.         mov     [esi-4], eax
  205.         jmp     import_loop
  206. import_not_found:
  207. import_fail:
  208.         popad
  209.         xor     eax, eax
  210.         ret
  211. import_done:
  212.         popad
  213.         xor     eax, eax
  214.         inc     eax
  215.         ret
  216. endp
  217.  
  218. align 4
  219.  
  220. imports:
  221. dll_start          dd szStart
  222. dll_ver            dd szVersion
  223. con_init           dd szcon_init
  224. con_write_asciiz   dd szcon_write_asciiz
  225. con_exit           dd szcon_exit
  226. con_gets2          dd szcon_gets2
  227.                    dd 0
  228.  
  229. szStart            db 'START',0
  230. szVersion          db 'version',0
  231. szcon_init         db 'con_init',0
  232. szcon_write_asciiz db 'con_write_asciiz',0
  233. szcon_exit         db 'con_exit',0
  234. szcon_gets2        db 'con_gets2',0
  235.  
  236. dllname  db '/sys/lib/console.obj',0
  237.  
  238. caption            db 'Console test - gets2()',0
  239. str1               db 'Enter string (empty for exit): ',0
  240. str2               db 'You entered: ',0
  241. str3               db '[autocomplete]'
  242. str3.len = $ - str3
  243. str4               db 13,10,'Help? What help do you need?',13,10,0
  244. str5               db 'previous line in the history'
  245. str5.len = $ - str5
  246. str6               db 'next line in the history'
  247. str6.len = $ - str6
  248.  
  249. i_end:
  250.  
  251. s rb 256
  252.  
  253. align 4
  254. rb 2048 ; stack
  255. mem:
  256.