Subversion Repositories Kolibri OS

Rev

Rev 8213 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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