Subversion Repositories Kolibri OS

Rev

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. get_tickcount:
  43.         push    ebx
  44.         mcall   SF_SYSTEM_GET,SSF_TIME_COUNT
  45.         imul    eax,10
  46.         pop     ebx
  47.         retn
  48.  
  49. macro BCDtoHEX al {
  50.         aam     16
  51.         aad     10 }
  52.  
  53. make_timestamp:
  54.         mcall   SF_GET_SYS_DATE ; $00SSMMHH (BCD)
  55.         mov     edx,eax
  56.         shr     eax,16
  57.         BCDtoHEX al
  58.         push    eax             ; SECONDS
  59.         mov     al,dh
  60.         BCDtoHEX al
  61.         push    eax             ; MINUTES
  62.         mov     al,dl
  63.         BCDtoHEX al
  64.         push    eax             ; HOURS
  65.         mcall   SF_GET_SYS_DATE ; $00DDMMYY (BCD)
  66.         mov     edx,eax
  67.         shr     eax,16
  68.         BCDtoHEX al
  69.         push    eax             ; DAY
  70.         mov     al,dl
  71.         BCDtoHEX al
  72.         add     eax,2000
  73.         push    eax             ; YEAR
  74.         mov     ecx,eax
  75.         mov     al,dh
  76.         BCDtoHEX al
  77.         push    eax             ; MONTH
  78. ; ecx: YEAR
  79. ; stack: MONTH, YEAR, DAY, HOURS, MINUTES, SECONDS, retaddr
  80.         mov     eax,ecx
  81.         sub     eax,1970
  82.         mov     ebx,365
  83.         mul     ebx
  84.         mov     ebp,eax
  85.         mov     eax,ecx
  86.         sub     eax,1969
  87.         shr     eax,2
  88.         add     ebp,eax
  89.         mov     eax,ecx
  90.         xor     edx,edx
  91.         sub     eax,1901
  92.         mov     ebx,100
  93.         div     ebx
  94.         sub     ebp,eax
  95.         mov     eax,ecx
  96.         xor     edx,edx
  97.         sub     eax,1601
  98.         mov     ebx,400
  99.         div     ebx
  100.         add     ebp,eax
  101.         pop     ecx
  102. ; ecx: MONTH
  103. ; stack: YEAR, DAY, HOURS, MINUTES, SECONDS, retaddr
  104.         mov     eax,ecx
  105.         dec     eax
  106.         mov     ebx,30
  107.         mul     ebx
  108.         add     ebp,eax
  109.         cmp     ecx,8
  110.         jbe     months_correction
  111.         mov     eax,ecx
  112.         sub     eax,7
  113.         shr     eax,1
  114.         add     ebp,eax
  115.         mov     ecx,8
  116.       months_correction:
  117.         mov     eax,ecx
  118.         shr     eax,1
  119.         add     ebp,eax
  120.         cmp     ecx,2
  121.         pop     ecx
  122. ; ecx: YEAR
  123. ; stack: DAY, HOURS, MINUTES, SECONDS, retaddr
  124.         jbe     day_correction_ok
  125.         sub     ebp,2
  126.         test    ecx,11b
  127.         jnz     day_correction_ok
  128.         xor     edx,edx
  129.         mov     eax,ecx
  130.         mov     ebx,100
  131.         div     ebx
  132.         or      edx,edx
  133.         jnz     day_correction
  134.         mov     eax,ecx
  135.         mov     ebx,400
  136.         div     ebx
  137.         or      edx,edx
  138.         jnz     day_correction_ok
  139.       day_correction:
  140.         inc     ebp
  141.       day_correction_ok:
  142.         pop     eax
  143. ; eax: DAY
  144. ; stack: HOURS, MINUTES, SECONDS, retaddr
  145.         dec     eax
  146.         add     eax,ebp
  147.         mov     ebx,24
  148.         mul     ebx
  149.         pop     ecx
  150. ; ecx: HOURS
  151. ; stack: MINUTES, SECONDS, retaddr
  152.         add     eax,ecx
  153.         mov     ebx,60
  154.         mul     ebx
  155.         pop     ecx
  156. ; ecx: MINUTES
  157. ; stack: SECONDS, retaddr
  158.         add     eax,ecx
  159.         mov     ebx,60
  160.         mul     ebx
  161.         pop     ecx
  162. ; ecx: SECONDS
  163. ; stack: retaddr
  164.         add     eax,ecx
  165.         retn
  166.  
  167. symbol_dump:
  168.  
  169.         push    edi
  170.         mov     edx,[memory_end]
  171.       symb_dump:
  172.         cmp     edx,[labels_list]
  173.         jbe     symbols_dumped
  174.         sub     edx,LABEL_STRUCTURE_SIZE
  175.         cmp     dword [edx+24],0
  176.         je      symb_dump       ; do not dump anonymous symbols
  177.         test    byte [edx+8],1
  178.         jz      symb_dump       ; do not dump symbols that didn't get defined
  179.         mov     ax,[current_pass]
  180.         cmp     ax,[edx+16]
  181.         jne     symb_dump
  182.         test    byte [edx+8],4 or 2
  183.         jnz     symb_dump       ; do not dump assembly-time variables
  184.                                 ; do not dump variables defined with '='
  185.         cmp     word [edx+12], 0
  186.         jnz     symb_dump       ; do not dump register-based variables
  187.  
  188.         mov     al, '0'
  189.         stosb
  190.         mov     al, 'x'
  191.         stosb
  192.         mov     eax, [edx+4]
  193.         mov     ecx, 8
  194. @@:
  195.         rol     eax, 4
  196.         test    al, 0xF
  197.         loopz   @b
  198.         jz      .nohigh
  199.         inc     ecx
  200. @@:
  201.         push    eax
  202.         and     al, 0xF
  203.         cmp     al, 10
  204.         sbb     al, 69h
  205.         das
  206.         stosb
  207.         pop     eax
  208.         rol     eax, 4
  209.         loop    @b
  210.         mov     eax, [edx]
  211.         mov     ecx, 8
  212.         jmp     .low
  213. .nohigh:
  214.         mov     eax, [edx]
  215.         mov     ecx, 8
  216. @@:
  217.         rol     eax, 4
  218.         test    al, 0xF
  219.         loopz   @b
  220.         inc     ecx
  221. .low:
  222.         push    eax
  223.         and     al, 0xF
  224.         cmp     al, 10
  225.         sbb     al, 69h
  226.         das
  227.         stosb
  228.         pop     eax
  229.         rol     eax, 4
  230.         loop    .low
  231.  
  232.         mov     al, ' '
  233.         stosb
  234.  
  235.         mov     esi,[edx+24]
  236.         movzx   ecx,byte [esi-1]
  237.         rep     movsb
  238.  
  239.         mov     ax,0A0Dh
  240.         stosw
  241.  
  242.         jmp     symb_dump
  243.  
  244.       symbols_dumped:
  245.         mov     edx,dbgfilename
  246.         push    esi edi
  247.         mov     esi, outfile
  248.         mov     edi, edx
  249. @@:
  250.         lodsb
  251.         stosb
  252.         test    al, al
  253.         jnz     @b
  254.         lea     ecx, [edi-1]
  255. @@:
  256.         dec     edi
  257.         cmp     edi, edx
  258.         jb      @f
  259.         cmp     byte [edi], '/'
  260.         jz      @f
  261.         cmp     byte [edi], '.'
  262.         jnz     @b
  263.         mov     ecx, edi
  264. @@:
  265.         mov     dword [ecx], '.dbg'
  266.         mov     byte [ecx+4], 0
  267.         pop     edi esi
  268.         call    create
  269.         mov     edx,[esp]
  270.         mov     ecx,edi
  271.         sub     ecx,edx
  272.         call    write
  273.         call    close
  274.         pop     edi
  275.  
  276.         retn
  277.  
  278. get_environment_variable:
  279.         mov     ecx,[memory_end]
  280.         sub     ecx,edi
  281.         cmp     ecx,7
  282.         jb      out_of_memory
  283.         cmp     dword[esi],'INCL'
  284.         jne     .finish
  285.         mov     esi,_ramdisk
  286.         mov     ecx,6
  287.         cld
  288.         rep     movsb
  289.   .finish:
  290. ;       stc
  291.         retn
  292.  
  293. alloc_handle:
  294. ; in:
  295. ; out: ebx = file handle
  296. ; on error: return to caller of caller with cf set
  297. ; preserves: esi, edi
  298.         call    make_fullpaths
  299.         mov     ebx, file_IO_slots+4
  300.         jmp     check_file_IO_slot
  301.       next_file_IO_slot:
  302.         add     ebx, 4+20+MAX_PATH
  303.         cmp     ebx, file_IO_end
  304.         jae     send_error_to_caller_of_caller
  305.       check_file_IO_slot:
  306.         cmp     [ebx+FILEIO.cmd], -1
  307.         jnz     next_file_IO_slot
  308.         and     [ebx+FILEIO.offset], 0
  309.         and     [ebx+FILEIO.flags], 0
  310.       adapt_path:
  311.         push    esi edi ecx
  312.         mov     esi, fullpath_open
  313.         lea     edi, [ebx+namedFILEIO.name]
  314.         mov     ecx, MAX_PATH
  315.       copy_path:
  316.         lodsb
  317.         cmp     al,'\'
  318.         jne     path_char_ok
  319.         mov     al,'/'
  320.       path_char_ok:
  321.         stosb
  322.         or      al,al
  323.         loopnz  copy_path
  324.         pop     ecx edi esi
  325.         jz      adapt_path.done
  326.       send_error_to_caller_of_caller:
  327.         stc
  328.         pop     eax
  329.       adapt_path.done:
  330.         ret
  331.  
  332. create:
  333. ; in:
  334. ; out: ebx = file handle, cf set on error
  335. ; preserves: esi, edi
  336.         call    alloc_handle
  337.         and     [ebx+FILEIO.filesize], 0
  338.         mov     [ebx+FILEIO.cmd], SSF_CREATE_FILE
  339.         retn
  340.  
  341.  
  342. open:
  343. ; in:
  344. ; out: ebx = file handle, cf set on error
  345. ; preserves: esi, edi
  346.         call    alloc_handle
  347.         mov     [ebx+FILEIO.cmd], SSF_GET_INFO
  348.         and     [ebx+FILEIO.count], 0
  349.         mov     [ebx+FILEIO.buffer], fileinfo
  350.         push    ebx
  351.         mcall   SF_FILE
  352.         pop     ebx
  353.         test    eax, eax
  354.         jnz     fail_close
  355.         mov     eax, dword[fileinfo.size]
  356.         mov     [ebx+FILEIO.filesize], eax
  357.         and     [ebx+FILEIO.cmd], SSF_READ_FILE
  358.         retn
  359.  
  360. fail_close:
  361.         stc
  362. close:
  363. ; in: ebx = file handle
  364. ; preserves: ebx, esi, edi, cf
  365.         mov     [ebx+FILEIO.cmd], -1 ; close handle
  366.         retn
  367. write:
  368. ; in: ebx = file handle, edx - data, ecx = number of bytes
  369. ; out: cf set on error
  370. ; preserves: ebx, esi, edi
  371.         call    read_write
  372.         mov     [ebx+FILEIO.cmd], SSF_WRITE_FILE
  373.         jmp     read_write_check_S_OK
  374. read:
  375. ; in: ebx = file handle, edx - buffer, ecx = number of bytes
  376. ; out: cf set on error
  377. ; preserves: ebx, esi, edi
  378.         call    read_write
  379.         cmp     eax, 6
  380.         jz      read_write_OK
  381.       read_write_check_S_OK:
  382.         test    eax, eax
  383.         jz      read_write_OK
  384.         stc
  385.         retn
  386.       read_write:
  387.         mov     [ebx+FILEIO.count], ecx
  388.         mov     [ebx+FILEIO.buffer], edx
  389.         push    ebx
  390.         mcall   SF_FILE
  391.         xchg    eax, [esp]
  392.         add     [eax+FILEIO.offset], ebx
  393.         adc     [eax+FILEIO.offshigh], 0
  394.         mov     ebx, eax
  395.         pop     eax
  396.       read_write_OK:
  397.         retn
  398.  
  399.  
  400. make_fullpaths:
  401.         pusha
  402.         push    edx
  403.         mov     esi,edx
  404.         mov     ecx, MAX_PATH
  405.       copy_edxpath:
  406.         lodsb
  407.         cmp     al,'\'
  408.         jne     edxpath_char_ok
  409.         mov     byte[esi-1],'/'
  410.       edxpath_char_ok:
  411.         or      al,al
  412.         loopnz  copy_edxpath
  413.  
  414.         mov     esi,path              ; open
  415. ;       DEBUGF  "   '%s'",esi
  416.         mov     edi,fullpath_open
  417.         cld
  418.       newc1:
  419.         movsb
  420.         cmp     byte[esi],0;' '
  421.         jne     newc1
  422.         mov     esi,[esp]
  423.  
  424.         cmp     byte[esi],'/'
  425.         je      abs_path
  426.         cmp     byte[esi],'\'
  427.         jne     @f
  428.     abs_path:
  429.         mov     edi,fullpath_open
  430.  
  431.     @@:
  432.         lodsb
  433.         stosb
  434.         cmp     al,0
  435.         jne     @b
  436.  
  437.         mov     esi,path              ; write
  438.         mov     edi,fullpath_write
  439.         cld
  440.       newc2:
  441.         movsb
  442.         cmp     byte[esi],0;' '
  443.         jne     newc2
  444.         mov     esi,[esp]
  445.  
  446.         cmp     byte[esi],'/'
  447.         jne     @f
  448.         cmp     byte[esi],'\'
  449.         jne     @f
  450.         mov     edi,fullpath_write
  451.  
  452.     @@:
  453.         lodsb
  454.         stosb
  455.         cmp     al,0
  456.         jne     @b
  457.  
  458.         mov     esi,path              ; start
  459.         mov     edi,file_io_start.path
  460.         cld
  461.       newc3:
  462.         movsb
  463.         cmp  byte[esi],0;' '
  464.         jne  newc3
  465.         pop     esi
  466.  
  467.         cmp     byte[esi],'/'
  468.         jne     @f
  469.         cmp     byte[esi],'\'
  470.         jne     @f
  471.         mov     edi,file_io_start.path
  472.  
  473.     @@:
  474.         lodsb
  475.         stosb
  476.         cmp     al,0
  477.         jne     @b
  478.         popa
  479.         retn
  480. lseek:
  481. ; in: ebx = file handle, al = method, edx = delta offset
  482. ; out: cf set on error
  483. ; preserves: ebx, esi, edi
  484.         cmp     al,FILE_BEGIN
  485.         jnz     @f
  486.         and     [ebx+FILEIO.offset], 0
  487.         jmp     .common
  488.     @@: cmp     al,FILE_END
  489.         jnz     @f
  490.         mov     eax, [ebx+FILEIO.filesize]
  491.         mov     [ebx+FILEIO.offset], eax
  492.         jmp     .common
  493.     @@:
  494.     .common:
  495.         add     [ebx+FILEIO.offset], edx
  496.         retn
  497.  
  498. display_character:
  499.         pusha
  500.         cmp     [_mode],NORMAL_MODE
  501.         jne     @f
  502.         cmp     al,$D
  503.         jz      dc2
  504.         cmp     al,$A
  505.         jnz     dc1
  506.         and     [textxy],0x0000FFFF
  507.         add     [textxy], 8 shl 16 and 0xFFFF0000 + 18
  508.         popa
  509.         retn
  510. dc1:
  511.         mov     [dc],al
  512.         mov     eax,[textxy]
  513.         cmp     ax,word[bottom_right]
  514.         ja      dc2
  515.         shr     eax,16
  516.         cmp     ax,word[bottom_right+2]
  517.         ja      dc2
  518.         mov     ecx,[sc.work_text]
  519.         or      ecx,$10000000
  520.         mcall   SF_DRAW_TEXT,[textxy],,dc,1
  521.         add     [textxy],0x00080000
  522. dc2:
  523.         popa
  524.         retn
  525. @@:
  526.         mov     cl,al
  527.         mcall   SF_BOARD,SSF_DEBUG_WRITE
  528.         popa
  529.         retn
  530.  
  531.  
  532. display_string:
  533. ; in:
  534. ;  esi - ASCIIZ string
  535. ; preserves: ebx, esi
  536.         push    esi
  537. @@:     lodsb
  538.         test    al,al
  539.         je      @f
  540.         call    display_character
  541.         jmp     @b
  542. @@:
  543.         pop     esi
  544.         retn
  545.  
  546. display_number:
  547.         push    ebx
  548.         mov     ecx,1000000000
  549.         xor     edx,edx
  550.         xor     bl,bl
  551. display_loop:
  552.         div     ecx
  553.         push    edx
  554.         cmp     ecx,1
  555.         je      display_digit
  556.         or      bl,bl
  557.         jnz     display_digit
  558.         or      al,al
  559.         jz      digit_ok
  560.         not     bl
  561. display_digit:
  562.         add     al,'0'
  563.         push    ebx ecx
  564.         call    display_character
  565.         pop     ecx ebx
  566. digit_ok:
  567.         mov     eax,ecx
  568.         xor     edx,edx
  569.         mov     ecx,10
  570.         div     ecx
  571.         mov     ecx,eax
  572.         pop     eax
  573.         or      ecx,ecx
  574.         jnz     display_loop
  575.         pop     ebx
  576.         retn
  577.  
  578. display_user_messages:
  579. ;    push   [skinh]
  580. ;    pop    [textxy]
  581. ;    add    [textxy], 7 shl 16 +53
  582.         mov     [displayed_count],0
  583.         call    show_display_buffer
  584.         cmp     [displayed_count],1
  585.         jb      line_break_ok
  586.         je      make_line_break
  587.         mov     ax,word[last_displayed]
  588.         cmp     ax,0A0Dh
  589.         je      line_break_ok
  590.         cmp     ax,0D0Ah
  591.         je      line_break_ok
  592. make_line_break:
  593.         mov     esi,crlf
  594.         call    display_string
  595. line_break_ok:
  596.         retn
  597.  
  598. display_block:
  599. ; in:
  600. ;  esi - string
  601. ;  ecx = string length
  602.         push    esi
  603. @@:     lodsb
  604.         call    display_character
  605.         loop    @b
  606.         pop     esi
  607.         retn
  608.  
  609. fatal_error:
  610. ; no return, trashes stack
  611.  
  612.         mov     esi,error_prefix
  613.         call    display_string
  614.         pop     esi
  615.         call    display_string
  616.         mov     esi,error_suffix
  617.         call    display_string
  618.         mov     esi,crlf
  619.         call    display_string
  620.         mov     al,0FFh
  621.         jmp     exit_program
  622.  
  623. assembler_error:
  624.         call    display_user_messages
  625.         push    0
  626.         mov     ebx,[current_line]
  627. get_error_lines:
  628.         push    ebx
  629.         test    byte [ebx+7],80h
  630.         jz      display_error_line
  631.         mov     edx,ebx
  632. find_definition_origin:
  633.         mov     edx,[edx+12]
  634.         test    byte [edx+7],80h
  635.         jnz     find_definition_origin
  636.         push    edx
  637.         mov     ebx,[ebx+8]
  638.         jmp     get_error_lines
  639. display_error_line:
  640.         mov     esi,[ebx]
  641.         call    display_string
  642.         mov     esi,line_number_start
  643.         call    display_string
  644.         mov     eax,[ebx+4]
  645.         and     eax,7FFFFFFFh
  646.         call    display_number
  647.         mov     al,']'
  648.         call    display_character
  649.         pop     esi
  650.         cmp     ebx,esi
  651.         je      line_number_ok
  652.         mov     al,' '
  653.         call    display_character
  654.         push    esi
  655.         mov     esi,[esi]
  656.         movzx   ecx,byte [esi]
  657.         inc     esi
  658.         call    display_block
  659.         mov     esi,line_number_start
  660.         call    display_string
  661.         pop     esi
  662.         mov     eax,[esi+4]
  663.         and     eax,7FFFFFFFh
  664.         call    display_number
  665.         mov     al,']'
  666.         call    display_character
  667. line_number_ok:
  668.         mov     esi,line_data_start
  669.         call    display_string
  670.         mov     esi,ebx
  671.         mov     edx,[esi]
  672.         call    open
  673.         mov     al,2
  674.         xor     edx,edx
  675.         call    lseek
  676.         mov     edx,[esi+8]
  677.         sub     eax,edx
  678.         push    eax
  679.         xor     al,al
  680.         call    lseek
  681.         mov     ecx,[esp]
  682.         mov     edx,[additional_memory]
  683.         lea     eax,[edx+ecx]
  684.         cmp     eax,[additional_memory_end]
  685.         ja      out_of_memory
  686.         call    read
  687.         call    close
  688.         pop     ecx
  689.         mov     esi,[additional_memory]
  690. get_line_data:
  691.         mov     al,[esi]
  692.         cmp     al,$A
  693.         je      display_line_data
  694.         cmp     al,$D
  695.         je      display_line_data
  696.         cmp     al,$1A
  697.         je      display_line_data
  698.         or      al,al
  699.         jz      display_line_data
  700.         inc     esi
  701.         loop    get_line_data
  702. display_line_data:
  703.         mov     ecx,esi
  704.         mov     esi,[additional_memory]
  705.         sub     ecx,esi
  706.         call    display_block
  707.         mov     esi,crlf
  708.         call    display_string
  709.         pop     ebx
  710.         or      ebx,ebx
  711.         jnz     display_error_line
  712.         mov     esi,error_prefix
  713.         call    display_string
  714.         pop     esi
  715.         call    display_string
  716.         mov     esi,error_suffix
  717.         call    display_string
  718.         jmp     exit_program