Subversion Repositories Kolibri OS

Rev

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 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.         push    eax
  72.         push    eax
  73.         push    string_extended
  74.         jmp     @b
  75. done:
  76.         push    1
  77.         call    [con_exit]
  78. exit:
  79.         or      eax, -1
  80.         int     0x40
  81.  
  82. proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
  83.         pushad
  84. ; load DLL
  85.         push    68
  86.         pop     eax
  87.         push    19
  88.         pop     ebx
  89.         mov     ecx, [_dllname]
  90.         int     0x40
  91.         test    eax, eax
  92.         jz      import_fail
  93.  
  94. ; initialize import
  95.         mov     edi, eax
  96.         mov     esi, [_imports]
  97. import_loop:
  98.         lodsd
  99.         test    eax, eax
  100.         jz      import_done
  101.         mov     edx, edi
  102. import_find:
  103.         mov     ebx, [edx]
  104.         test    ebx, ebx
  105.         jz      import_not_found
  106.         push    eax
  107. @@:
  108.         mov     cl, [eax]
  109.         cmp     cl, [ebx]
  110.         jnz     import_find_next
  111.         test    cl, cl
  112.         jz      import_found
  113.         inc     eax
  114.         inc     ebx
  115.         jmp     @b
  116. import_find_next:
  117.         pop     eax
  118.         add     edx, 8
  119.         jmp     import_find
  120. import_found:
  121.         pop     eax
  122.         mov     eax, [edx+4]
  123.         mov     [esi-4], eax
  124.         jmp     import_loop
  125. import_not_found:
  126. import_fail:
  127.         popad
  128.         xor     eax, eax
  129.         ret
  130. import_done:
  131.         popad
  132.         xor     eax, eax
  133.         inc     eax
  134.         ret
  135. endp
  136.  
  137. align 4
  138.  
  139. imports:
  140. dll_start          dd szStart
  141. dll_ver            dd szVersion
  142. con_init           dd szcon_init
  143. con_printf         dd szcon_printf
  144. con_exit           dd szcon_exit
  145. con_getch          dd szcon_getch
  146.                    dd 0
  147.  
  148. szStart            db 'START',0
  149. szVersion          db 'version',0
  150. szcon_init         db 'con_init',0
  151. szcon_printf       db 'con_printf',0
  152. szcon_exit         db 'con_exit',0
  153. szcon_getch        db 'con_getch',0
  154.  
  155. dllname  db '/sys/lib/console.obj',0
  156.  
  157. caption            db 'Console test - getch()',0
  158. start_string       db 'Press any key to see its code, or Esc to exit',10,0
  159. string_normal      db 'normal character with code %d=0x%02X',10,0
  160. string_extended    db 'extended character with code %d=0x%02X',10,0
  161.  
  162. i_end:
  163.  
  164. align 4
  165. rb 2048 ; stack
  166. mem:
  167.