Subversion Repositories Kolibri OS

Rev

Rev 7836 | Rev 7863 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; flat assembler
  2. ; Copyright (c) 1999-2020, Tomasz Grysztar
  3. ; All rights reserved.
  4.  
  5. init_memory:
  6.  
  7. ;        mov ecx, 16*1024*1024
  8. ;
  9. ; allocate_memory:
  10.         mcall   SF_SYSTEM, SSF_GET_FREE_RAM
  11.         cmp     eax, 0x38000000 shr 9
  12.         jbe     @f
  13.         mov     eax, 0x38000000 shr 9
  14. @@:
  15.         shl     eax, 9
  16.         xchg    eax, ecx
  17.         mov     [memory_setting],ecx
  18.         mcall   SF_SYS_MISC, SSF_MEM_ALLOC
  19.         or      eax,eax
  20.         jz      out_of_memory
  21.         mov     [memblock], eax
  22.         mov     [additional_memory],eax
  23.         add     eax,[memory_setting]
  24.         mov     [memory_end],eax
  25.         mov     eax,[memory_setting]
  26.         shr     eax,2
  27.         add     eax,[additional_memory]
  28.         mov     [additional_memory_end],eax
  29.         mov     [memory_start],eax
  30.         retn
  31.  
  32. exit_program:
  33.         cmp     [_mode],NORMAL_MODE
  34.         jne     @f
  35.         mcall   SF_SYS_MISC, SSF_MEM_FREE, [memblock]
  36.         mov     esp,[processing_esp]
  37.         jmp     still
  38. @@:
  39.         or      eax,-1
  40.         mcall
  41.  
  42. make_timestamp:
  43.         push    ebx
  44.         mcall   SF_SYSTEM_GET,SSF_TIME_COUNT
  45.         imul    eax,10
  46.         pop     ebx
  47.         retn
  48.  
  49. symbol_dump:
  50.  
  51.         push    edi
  52.         mov     edx,[memory_end]
  53.       symb_dump:
  54.         cmp     edx,[labels_list]
  55.         jbe     symbols_dumped
  56.         sub     edx,LABEL_STRUCTURE_SIZE
  57.         cmp     dword [edx+24],0
  58.         je      symb_dump       ; do not dump anonymous symbols
  59.         test    byte [edx+8],1
  60.         jz      symb_dump       ; do not dump symbols that didn't get defined
  61.         mov     ax,[current_pass]
  62.         cmp     ax,[edx+16]
  63.         jne     symb_dump
  64.         test    byte [edx+8],4 or 2
  65.         jnz     symb_dump       ; do not dump assembly-time variables
  66.                                 ; do not dump variables defined with '='
  67.         cmp     word [edx+12], 0
  68.         jnz     symb_dump       ; do not dump register-based variables
  69.  
  70.         mov     al, '0'
  71.         stosb
  72.         mov     al, 'x'
  73.         stosb
  74.         mov     eax, [edx+4]
  75.         mov     ecx, 8
  76. @@:
  77.         rol     eax, 4
  78.         test    al, 0xF
  79.         loopz   @b
  80.         jz      .nohigh
  81.         inc     ecx
  82. @@:
  83.         push    eax
  84.         and     al, 0xF
  85.         cmp     al, 10
  86.         sbb     al, 69h
  87.         das
  88.         stosb
  89.         pop     eax
  90.         rol     eax, 4
  91.         loop    @b
  92.         mov     eax, [edx]
  93.         mov     ecx, 8
  94.         jmp     .low
  95. .nohigh:
  96.         mov     eax, [edx]
  97.         mov     ecx, 8
  98. @@:
  99.         rol     eax, 4
  100.         test    al, 0xF
  101.         loopz   @b
  102.         inc     ecx
  103. .low:
  104.         push    eax
  105.         and     al, 0xF
  106.         cmp     al, 10
  107.         sbb     al, 69h
  108.         das
  109.         stosb
  110.         pop     eax
  111.         rol     eax, 4
  112.         loop    .low
  113.  
  114.         mov     al, ' '
  115.         stosb
  116.  
  117.         mov     esi,[edx+24]
  118.         movzx   ecx,byte [esi-1]
  119.         rep     movsb
  120.  
  121.         mov     ax,0A0Dh
  122.         stosw
  123.  
  124.         jmp     symb_dump
  125.  
  126.       symbols_dumped:
  127.         mov     edx,dbgfilename
  128.         push    esi edi
  129.         mov     esi, outfile
  130.         mov     edi, edx
  131. @@:
  132.         lodsb
  133.         stosb
  134.         test    al, al
  135.         jnz     @b
  136.         lea     ecx, [edi-1]
  137. @@:
  138.         dec     edi
  139.         cmp     edi, edx
  140.         jb      @f
  141.         cmp     byte [edi], '/'
  142.         jz      @f
  143.         cmp     byte [edi], '.'
  144.         jnz     @b
  145.         mov     ecx, edi
  146. @@:
  147.         mov     dword [ecx], '.dbg'
  148.         mov     byte [ecx+4], 0
  149.         pop     edi esi
  150.         call    create
  151.         mov     edx,[esp]
  152.         mov     ecx,edi
  153.         sub     ecx,edx
  154.         call    write
  155.         call    close
  156.         pop     edi
  157.  
  158.         retn
  159.  
  160. get_environment_variable:
  161.         mov     ecx,[memory_end]
  162.         sub     ecx,edi
  163.         cmp     ecx,7
  164.         jb      out_of_memory
  165.         cmp     dword[esi],'INCL'
  166.         jne     .finish
  167.         mov     esi,_ramdisk
  168.         mov     ecx,6
  169.         cld
  170.         rep     movsb
  171.   .finish:
  172. ;       stc
  173.         retn
  174.  
  175. alloc_handle:
  176. ; in:
  177. ; out: ebx = file handle
  178. ; on error: return to caller of caller with cf set
  179. ; preserves: esi, edi
  180.         call    make_fullpaths
  181.         mov     ebx, file_IO_slots+4
  182.         jmp     check_file_IO_slot
  183.       next_file_IO_slot:
  184.         add     ebx, 4+20+MAX_PATH
  185.         cmp     ebx, file_IO_end
  186.         jae     send_error_to_caller_of_caller
  187.       check_file_IO_slot:
  188.         cmp     [ebx+FILEIO.cmd], -1
  189.         jnz     next_file_IO_slot
  190.         and     [ebx+FILEIO.offset], 0
  191.         and     [ebx+FILEIO.flags], 0
  192.       adapt_path:
  193.         push    esi edi ecx
  194.         mov     esi, fullpath_open
  195.         lea     edi, [ebx+namedFILEIO.name]
  196.         mov     ecx, MAX_PATH
  197.       copy_path:
  198.         lodsb
  199.         cmp     al,'\'
  200.         jne     path_char_ok
  201.         mov     al,'/'
  202.       path_char_ok:
  203.         stosb
  204.         or      al,al
  205.         loopnz  copy_path
  206.         pop     ecx edi esi
  207.         jz      adapt_path.done
  208.       send_error_to_caller_of_caller:
  209.         stc
  210.         pop     eax
  211.       adapt_path.done:
  212.         ret
  213.  
  214. create:
  215. ; in:
  216. ; out: ebx = file handle, cf set on error
  217. ; preserves: esi, edi
  218.         call    alloc_handle
  219.         and     [ebx+FILEIO.filesize], 0
  220.         mov     [ebx+FILEIO.cmd], SSF_CREATE_FILE
  221.         retn
  222.  
  223.  
  224. open:
  225. ; in:
  226. ; out: ebx = file handle, cf set on error
  227. ; preserves: esi, edi
  228.         call    alloc_handle
  229.         mov     [ebx+FILEIO.cmd], SSF_GET_INFO
  230.         and     [ebx+FILEIO.count], 0
  231.         mov     [ebx+FILEIO.buffer], fileinfo
  232.         push    ebx
  233.         mcall   SF_FILE
  234.         pop     ebx
  235.         test    eax, eax
  236.         jnz     fail_close
  237.         mov     eax, dword[fileinfo.size]
  238.         mov     [ebx+FILEIO.filesize], eax
  239.         and     [ebx+FILEIO.cmd], SSF_READ_FILE
  240.         retn
  241.  
  242. fail_close:
  243.         stc
  244. close:
  245. ; in: ebx = file handle
  246. ; preserves: ebx, esi, edi, cf
  247.         mov     [ebx+FILEIO.cmd], -1 ; close handle
  248.         retn
  249. write:
  250. ; in: ebx = file handle, edx - data, ecx = number of bytes
  251. ; out: cf set on error
  252. ; preserves: ebx, esi, edi
  253.         call    read_write
  254.         mov     [ebx+FILEIO.cmd], SSF_WRITE_FILE
  255.         jmp     read_write_check_S_OK
  256. read:
  257. ; in: ebx = file handle, edx - buffer, ecx = number of bytes
  258. ; out: cf set on error
  259. ; preserves: ebx, esi, edi
  260.         call    read_write
  261.         cmp     eax, 6
  262.         jz      read_write_OK
  263.       read_write_check_S_OK:
  264.         test    eax, eax
  265.         jz      read_write_OK
  266.         stc
  267.         retn
  268.       read_write:
  269.         mov     [ebx+FILEIO.count], ecx
  270.         mov     [ebx+FILEIO.buffer], edx
  271.         push    ebx
  272.         mcall   SF_FILE
  273.         xchg    eax, [esp]
  274.         add     [eax+FILEIO.offset], ebx
  275.         adc     [eax+FILEIO.offshigh], 0
  276.         mov     ebx, eax
  277.         pop     eax
  278.       read_write_OK:
  279.         retn
  280.  
  281.  
  282. make_fullpaths:
  283.         pusha
  284.         push    edx
  285.         mov     esi,edx
  286.         mov     ecx, MAX_PATH
  287.       copy_edxpath:
  288.         lodsb
  289.         cmp     al,'\'
  290.         jne     edxpath_char_ok
  291.         mov     byte[esi-1],'/'
  292.       edxpath_char_ok:
  293.         or      al,al
  294.         loopnz  copy_edxpath
  295.  
  296.         mov     esi,path              ; open
  297. ;       DEBUGF  "   '%s'",esi
  298.         mov     edi,fullpath_open
  299.         cld
  300.       newc1:
  301.         movsb
  302.         cmp     byte[esi],0;' '
  303.         jne     newc1
  304.         mov     esi,[esp]
  305.  
  306.         cmp     byte[esi],'/'
  307.         jne     @f
  308.         cmp     byte[esi],'\'
  309.         jne     @f
  310.         mov     edi,fullpath_open
  311.  
  312.     @@:
  313.         lodsb
  314.         stosb
  315.         cmp     al,0
  316.         jne     @b
  317.  
  318.         mov     esi,path              ; write
  319.         mov     edi,fullpath_write
  320.         cld
  321.       newc2:
  322.         movsb
  323.         cmp     byte[esi],0;' '
  324.         jne     newc2
  325.         mov     esi,[esp]
  326.  
  327.         cmp     byte[esi],'/'
  328.         jne     @f
  329.         cmp     byte[esi],'\'
  330.         jne     @f
  331.         mov     edi,fullpath_write
  332.  
  333.     @@:
  334.         lodsb
  335.         stosb
  336.         cmp     al,0
  337.         jne     @b
  338.  
  339.         mov     esi,path              ; start
  340.         mov     edi,file_io_start.path
  341.         cld
  342.       newc3:
  343.         movsb
  344.         cmp  byte[esi],0;' '
  345.         jne  newc3
  346.         pop     esi
  347.  
  348.         cmp     byte[esi],'/'
  349.         jne     @f
  350.         cmp     byte[esi],'\'
  351.         jne     @f
  352.         mov     edi,file_io_start.path
  353.  
  354.     @@:
  355.         lodsb
  356.         stosb
  357.         cmp     al,0
  358.         jne     @b
  359.         popa
  360.         retn
  361. lseek:
  362. ; in: ebx = file handle, al = method, edx = delta offset
  363. ; out: cf set on error
  364. ; preserves: ebx, esi, edi
  365.         cmp     al,FILE_BEGIN
  366.         jnz     @f
  367.         and     [ebx+FILEIO.offset], 0
  368.         jmp     .common
  369.     @@: cmp     al,FILE_END
  370.         jnz     @f
  371.         mov     eax, [ebx+FILEIO.filesize]
  372.         mov     [ebx+FILEIO.offset], eax
  373.         jmp     .common
  374.     @@:
  375.     .common:
  376.         add     [ebx+FILEIO.offset], edx
  377.         retn
  378.  
  379. display_character:
  380.         pusha
  381.         cmp     [_mode],NORMAL_MODE
  382.         jne     @f
  383.         cmp     al,$D
  384.         jz      dc2
  385.         cmp     al,$A
  386.         jnz     dc1
  387.         and     [textxy],0x0000FFFF
  388.         add     [textxy], 8 shl 16 and 0xFFFF0000 + 18
  389.         popa
  390.         retn
  391. dc1:
  392.         mov     [dc],al
  393.         mov     eax,[textxy]
  394.         cmp     ax,word[bottom_right]
  395.         ja      dc2
  396.         shr     eax,16
  397.         cmp     ax,word[bottom_right+2]
  398.         ja      dc2
  399.         mov     ecx,[sc.work_text]
  400.         or      ecx,$10000000
  401.         mcall   SF_DRAW_TEXT,[textxy],,dc,1
  402.         add     [textxy],0x00080000
  403. dc2:
  404.         popa
  405.         retn
  406. @@:
  407.         mov     cl,al
  408.         mcall   SF_BOARD,SSF_DEBUG_WRITE
  409.         popa
  410.         retn
  411.  
  412.  
  413. display_string:
  414. ; in:
  415. ;  esi - ASCIIZ string
  416. ; preserves: ebx, esi
  417.         push    esi
  418. @@:     lodsb
  419.         test    al,al
  420.         je      @f
  421.         call    display_character
  422.         jmp     @b
  423. @@:
  424.         pop     esi
  425.         retn
  426.  
  427. display_number:
  428.         push    ebx
  429.         mov     ecx,1000000000
  430.         xor     edx,edx
  431.         xor     bl,bl
  432. display_loop:
  433.         div     ecx
  434.         push    edx
  435.         cmp     ecx,1
  436.         je      display_digit
  437.         or      bl,bl
  438.         jnz     display_digit
  439.         or      al,al
  440.         jz      digit_ok
  441.         not     bl
  442. display_digit:
  443.         add     al,'0'
  444.         push    ebx ecx
  445.         call    display_character
  446.         pop     ecx ebx
  447. digit_ok:
  448.         mov     eax,ecx
  449.         xor     edx,edx
  450.         mov     ecx,10
  451.         div     ecx
  452.         mov     ecx,eax
  453.         pop     eax
  454.         or      ecx,ecx
  455.         jnz     display_loop
  456.         pop     ebx
  457.         retn
  458.  
  459. display_user_messages:
  460. ;    push   [skinh]
  461. ;    pop    [textxy]
  462. ;    add    [textxy], 7 shl 16 +53
  463.         mov     [displayed_count],0
  464.         call    show_display_buffer
  465.         cmp     [displayed_count],1
  466.         jb      line_break_ok
  467.         je      make_line_break
  468.         mov     ax,word[last_displayed]
  469.         cmp     ax,0A0Dh
  470.         je      line_break_ok
  471.         cmp     ax,0D0Ah
  472.         je      line_break_ok
  473. make_line_break:
  474.         mov     esi,crlf
  475.         call    display_string
  476. line_break_ok:
  477.         retn
  478.  
  479. display_block:
  480. ; in:
  481. ;  esi - string
  482. ;  ecx = string length
  483.         push    esi
  484. @@:     lodsb
  485.         call    display_character
  486.         loop    @b
  487.         pop     esi
  488.         retn
  489.  
  490. fatal_error:
  491. ; no return, trashes stack
  492.  
  493.         mov     esi,error_prefix
  494.         call    display_string
  495.         pop     esi
  496.         call    display_string
  497.         mov     esi,error_suffix
  498.         call    display_string
  499.         mov     esi,crlf
  500.         call    display_string
  501.         mov     al,0FFh
  502.         jmp     exit_program
  503.  
  504. assembler_error:
  505.         call    display_user_messages
  506.         push    0
  507.         mov     ebx,[current_line]
  508. get_error_lines:
  509.         push    ebx
  510.         test    byte [ebx+7],80h
  511.         jz      display_error_line
  512.         mov     edx,ebx
  513. find_definition_origin:
  514.         mov     edx,[edx+12]
  515.         test    byte [edx+7],80h
  516.         jnz     find_definition_origin
  517.         push    edx
  518.         mov     ebx,[ebx+8]
  519.         jmp     get_error_lines
  520. display_error_line:
  521.         mov     esi,[ebx]
  522.         call    display_string
  523.         mov     esi,line_number_start
  524.         call    display_string
  525.         mov     eax,[ebx+4]
  526.         and     eax,7FFFFFFFh
  527.         call    display_number
  528.         mov     al,']'
  529.         call    display_character
  530.         pop     esi
  531.         cmp     ebx,esi
  532.         je      line_number_ok
  533.         mov     al,' '
  534.         call    display_character
  535.         push    esi
  536.         mov     esi,[esi]
  537.         movzx   ecx,byte [esi]
  538.         inc     esi
  539.         call    display_block
  540.         mov     esi,line_number_start
  541.         call    display_string
  542.         pop     esi
  543.         mov     eax,[esi+4]
  544.         and     eax,7FFFFFFFh
  545.         call    display_number
  546.         mov     al,']'
  547.         call    display_character
  548. line_number_ok:
  549.         mov     esi,line_data_start
  550.         call    display_string
  551.         mov     esi,ebx
  552.         mov     edx,[esi]
  553.         call    open
  554.         mov     al,2
  555.         xor     edx,edx
  556.         call    lseek
  557.         mov     edx,[esi+8]
  558.         sub     eax,edx
  559.         push    eax
  560.         xor     al,al
  561.         call    lseek
  562.         mov     ecx,[esp]
  563.         mov     edx,[additional_memory]
  564.         lea     eax,[edx+ecx]
  565.         cmp     eax,[additional_memory_end]
  566.         ja      out_of_memory
  567.         call    read
  568.         call    close
  569.         pop     ecx
  570.         mov     esi,[additional_memory]
  571. get_line_data:
  572.         mov     al,[esi]
  573.         cmp     al,$A
  574.         je      display_line_data
  575.         cmp     al,$D
  576.         je      display_line_data
  577.         cmp     al,$1A
  578.         je      display_line_data
  579.         or      al,al
  580.         jz      display_line_data
  581.         inc     esi
  582.         loop    get_line_data
  583. display_line_data:
  584.         mov     ecx,esi
  585.         mov     esi,[additional_memory]
  586.         sub     ecx,esi
  587.         call    display_block
  588.         mov     esi,crlf
  589.         call    display_string
  590.         pop     ebx
  591.         or      ebx,ebx
  592.         jnz     display_error_line
  593.         mov     esi,error_prefix
  594.         call    display_string
  595.         pop     esi
  596.         call    display_string
  597.         mov     esi,error_suffix
  598.         call    display_string
  599.         jmp     exit_program