Subversion Repositories Kolibri OS

Rev

Rev 2287 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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