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 3
  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 (getch() demo in this case).
  32.  
  33.         push    caption
  34.         push    -1
  35.         push    -1
  36.         push    -1
  37.         push    -1
  38.         call    [con_init]
  39.  
  40. ; C-equivalent of the following code:
  41. ; con_printf(start_string);
  42. ; int c;
  43. ; while ((c=con_getch())!=27) // Esc=exit
  44. ; {
  45. ;   if (c)
  46. ;     con_printf("normal character with code %d=0x%02X\n",c,c);
  47. ;   else
  48. ;   {
  49. ;     c=con_getch();
  50. ;     con_printf("extended character with code %d=0x%02X\n",c,c);
  51. ;   }
  52. ; }
  53.         push    start_string
  54.         call    [con_printf]
  55.         pop     ecx
  56. mainloop:
  57.         call    [con_getch]
  58.         cmp     al, 27
  59.         jz      done
  60.         test    eax, eax
  61.         jz      extended
  62.         push    eax
  63.         push    eax
  64.         push    string_normal
  65. @@:
  66.         call    [con_printf]
  67.         add     esp, 12
  68.         jmp     mainloop
  69. extended:
  70.         call    [con_getch]
  71.         test    eax, eax
  72.         jz      done
  73.         push    eax
  74.         push    eax
  75.         push    string_extended
  76.         jmp     @b
  77. done:
  78.         push    1
  79.         call    [con_exit]
  80. exit:
  81.         or      eax, -1
  82.         int     0x40
  83.  
  84. proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
  85.         pushad
  86. ; load DLL
  87.         push    68
  88.         pop     eax
  89.         push    19
  90.         pop     ebx
  91.         mov     ecx, [_dllname]
  92.         int     0x40
  93.         test    eax, eax
  94.         jz      import_fail
  95.  
  96. ; initialize import
  97.         mov     edi, eax
  98.         mov     esi, [_imports]
  99. import_loop:
  100.         lodsd
  101.         test    eax, eax
  102.         jz      import_done
  103.         mov     edx, edi
  104. import_find:
  105.         mov     ebx, [edx]
  106.         test    ebx, ebx
  107.         jz      import_not_found
  108.         push    eax
  109. @@:
  110.         mov     cl, [eax]
  111.         cmp     cl, [ebx]
  112.         jnz     import_find_next
  113.         test    cl, cl
  114.         jz      import_found
  115.         inc     eax
  116.         inc     ebx
  117.         jmp     @b
  118. import_find_next:
  119.         pop     eax
  120.         add     edx, 8
  121.         jmp     import_find
  122. import_found:
  123.         pop     eax
  124.         mov     eax, [edx+4]
  125.         mov     [esi-4], eax
  126.         jmp     import_loop
  127. import_not_found:
  128. import_fail:
  129.         popad
  130.         xor     eax, eax
  131.         ret
  132. import_done:
  133.         popad
  134.         xor     eax, eax
  135.         inc     eax
  136.         ret
  137. endp
  138.  
  139. align 4
  140.  
  141. imports:
  142. dll_start          dd szStart
  143. dll_ver            dd szVersion
  144. con_init           dd szcon_init
  145. con_printf         dd szcon_printf
  146. con_exit           dd szcon_exit
  147. con_getch          dd szcon_getch
  148.                    dd 0
  149.  
  150. szStart            db 'START',0
  151. szVersion          db 'version',0
  152. szcon_init         db 'con_init',0
  153. szcon_printf       db 'con_printf',0
  154. szcon_exit         db 'con_exit',0
  155. szcon_getch        db 'con_getch',0
  156.  
  157. dllname  db '/sys/lib/console.obj',0
  158.  
  159. caption            db 'Console test - getch()',0
  160. start_string       db 'Press any key to see its code, or Esc to exit',10,0
  161. string_normal      db 'normal character with code %d=0x%02X',10,0
  162. string_extended    db 'extended character with code %d=0x%02X',10,0
  163.  
  164. i_end:
  165.  
  166. align 4
  167. rb 2048 ; stack
  168. mem:
  169.