Subversion Repositories Kolibri OS

Rev

Rev 852 | Rev 1145 | Go to most recent revision | 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. ;   con_gets2(mycallback,s,256);
  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.         cmp     [s], 10
  57.         jz      done
  58.         push    str2
  59.         call    [con_write_asciiz]
  60.         push    s
  61.         call    [con_write_asciiz]
  62.         jmp     mainloop
  63. done:
  64.         push    1
  65.         call    [con_exit]
  66. exit:
  67.         or      eax, -1
  68.         int     0x40
  69.  
  70. proc mycallback stdcall, keycode:dword, pstr:dword, pn:dword, ppos:dword
  71.         mov     eax, [keycode]
  72.         cmp     al, 0x0F
  73.         jz      .tab
  74.         cmp     al, 0x3B
  75.         jz      .f1
  76.         cmp     al, 0x48
  77.         jz      .up
  78.         cmp     al, 0x50
  79.         jz      .down
  80.         xor     eax, eax
  81.         ret
  82. .tab:
  83. ; Tab pressed - insert "[autocomplete]" to current position
  84.         push    esi edi
  85.         mov     eax, [ppos]
  86.         mov     eax, [eax]
  87.         mov     ecx, [pn]
  88.         mov     ecx, [ecx]
  89.         mov     esi, [pstr]
  90.         mov     esi, [esi]
  91.         add     ecx, esi
  92.         add     esi, eax
  93.         mov     edx, esi
  94. @@:
  95.         lodsb
  96.         test    al, al
  97.         jnz     @b
  98.         lea     edi, [esi+str3.len]
  99.         cmp     edi, ecx
  100.         jbe     @f
  101.         mov     edi, ecx
  102.         lea     esi, [edi-str3.len]
  103. @@:
  104.         cmp     esi, edx
  105.         jbe     @f
  106.         dec     esi
  107.         dec     edi
  108.         mov     al, [esi]
  109.         mov     [edi], al
  110.         jmp     @b
  111. @@:
  112.         cmp     edi, ecx
  113.         jb      @f
  114.         dec     edi
  115. @@:
  116.         mov     ecx, edi
  117.         sub     ecx, edx
  118.         mov     edi, edx
  119.         mov     esi, str3
  120.         rep     movsb
  121.         mov     eax, [pstr]
  122.         sub     edi, [eax]
  123.         mov     eax, [ppos]
  124.         mov     [eax], edi
  125.         pop     edi esi
  126.         xor     eax, eax
  127.         inc     eax
  128.         ret
  129. .f1:
  130. ; F1 pressed - say message
  131.         push    str4
  132.         call    [con_write_asciiz]
  133.         push    str1
  134.         call    [con_write_asciiz]
  135.         push    2
  136.         pop     eax
  137.         ret
  138. .up:
  139.         push    esi
  140.         mov     esi, str5
  141.         mov     ecx, str5.len
  142.         jmp     @f
  143. .down:
  144.         push    esi
  145.         mov     esi, str6
  146.         mov     ecx, str6.len
  147. @@:
  148.         push    edi
  149.         mov     edi, [pstr]
  150.         mov     edi, [edi]
  151.         mov     eax, [ppos]
  152.         mov     [eax], ecx
  153.         rep     movsb
  154.         xor     eax, eax
  155.         stosb
  156.         pop     edi esi
  157.         inc     eax
  158.         ret
  159. endp
  160.  
  161. proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
  162.         pushad
  163. ; load DLL
  164.         push    68
  165.         pop     eax
  166.         push    19
  167.         pop     ebx
  168.         mov     ecx, [_dllname]
  169.         int     0x40
  170.         test    eax, eax
  171.         jz      import_fail
  172.  
  173. ; initialize import
  174.         mov     edi, eax
  175.         mov     esi, [_imports]
  176. import_loop:
  177.         lodsd
  178.         test    eax, eax
  179.         jz      import_done
  180.         mov     edx, edi
  181. import_find:
  182.         mov     ebx, [edx]
  183.         test    ebx, ebx
  184.         jz      import_not_found
  185.         push    eax
  186. @@:
  187.         mov     cl, [eax]
  188.         cmp     cl, [ebx]
  189.         jnz     import_find_next
  190.         test    cl, cl
  191.         jz      import_found
  192.         inc     eax
  193.         inc     ebx
  194.         jmp     @b
  195. import_find_next:
  196.         pop     eax
  197.         add     edx, 8
  198.         jmp     import_find
  199. import_found:
  200.         pop     eax
  201.         mov     eax, [edx+4]
  202.         mov     [esi-4], eax
  203.         jmp     import_loop
  204. import_not_found:
  205. import_fail:
  206.         popad
  207.         xor     eax, eax
  208.         ret
  209. import_done:
  210.         popad
  211.         xor     eax, eax
  212.         inc     eax
  213.         ret
  214. endp
  215.  
  216. align 4
  217.  
  218. imports:
  219. dll_start          dd szStart
  220. dll_ver            dd szVersion
  221. con_init           dd szcon_init
  222. con_write_asciiz   dd szcon_write_asciiz
  223. con_exit           dd szcon_exit
  224. con_gets2          dd szcon_gets2
  225.                    dd 0
  226.  
  227. szStart            db 'START',0
  228. szVersion          db 'version',0
  229. szcon_init         db 'con_init',0
  230. szcon_write_asciiz db 'con_write_asciiz',0
  231. szcon_exit         db 'con_exit',0
  232. szcon_gets2        db 'con_gets2',0
  233.  
  234. dllname  db '/sys/lib/console.obj',0
  235.  
  236. caption            db 'Console test - gets2()',0
  237. str1               db 'Enter string (empty for exit): ',0
  238. str2               db 'You entered: ',0
  239. str3               db '[autocomplete]'
  240. str3.len = $ - str3
  241. str4               db 13,10,'Help? What help do you need?',13,10,0
  242. str5               db 'previous line in the history'
  243. str5.len = $ - str5
  244. str6               db 'next line in the history'
  245. str6.len = $ - str6
  246.  
  247. i_end:
  248.  
  249. s rb 256
  250.  
  251. align 4
  252. rb 2048 ; stack
  253. mem:
  254.