Subversion Repositories Kolibri OS

Rev

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

  1. ; Files *.asc routines by Maciej Guba
  2. ; Thanks to Reverend for integer/float/ascii conversion examples
  3. read_asc:
  4.     mov  eax,[fptr]
  5.  .find_vert:
  6.     cmp  dword[eax],'Vert'
  7.     je   @f
  8.     inc  eax
  9.     jmp  .find_vert
  10.   @@:
  11.     add   eax,4
  12.     cmp   dword[eax],'ices'
  13.     jne   .find_vert
  14.     add   eax,3
  15.   @@:
  16.     inc   eax
  17.     cmp   byte[eax],'0'           ; search end of ascii number of vertices string
  18.     jb    @b
  19.     cmp   byte[eax],'9'
  20.     ja    @b
  21. ;    eax - start ascii number
  22.   @@:
  23.     inc    eax
  24.     cmp    byte[eax],'0'
  25.     jb    .convert1
  26.     cmp    byte[eax],'9'
  27.     ja    .convert1
  28.     jmp   @b
  29.   .convert1:
  30.     dec   eax
  31.     mov   ebx,eax
  32.     push  eax
  33.     call  ascii_to_integer
  34.     mov   [points_count_var],edx
  35.     pop   eax
  36.  
  37.   @@:
  38.     inc   eax
  39.     cmp   dword[eax],'Face'
  40.     jne   @b
  41.     add   eax,3
  42.   @@:
  43.     inc   eax
  44.     cmp   byte[eax],'0'
  45.     jb    @b
  46.     cmp   byte[eax],'9'
  47.     ja    @b
  48.    ;    eax - start ascii number
  49.   @@:
  50.     inc    eax
  51.     cmp    byte[eax],'0'
  52.     jb    .convert2
  53.     cmp    byte[eax],'9'
  54.     ja    .convert2
  55.     jmp   @b
  56.    ;  eax - end ascii number
  57.   .convert2:
  58.     dec   eax
  59.     mov   ebx,eax
  60.     push  eax
  61.     call  ascii_to_integer
  62.     mov   [triangles_count_var],edx
  63.     pop   eax
  64.  
  65.   @@:
  66.     inc   eax
  67.     cmp   dword[eax],'Vert'
  68.     jnz   @b
  69.     inc   eax
  70.  
  71.     mov   edi,[points_ptr]
  72.     xor   ebx,ebx
  73.    .decode_vertices:
  74.     push   ebx
  75.   @@:
  76.     inc   eax
  77.     cmp   dword[eax],'Vert'
  78.     jne   @b
  79.     xor   ecx,ecx
  80.  
  81.   .decode_coord:
  82.     push  ecx
  83.   @@:
  84.     inc   eax
  85.     mov   dl,byte[eax]
  86.     cmp   dl,byte[XYZpartices+ecx]
  87.     jne   @b
  88.   @@:
  89.     inc   eax
  90.     cmp   byte[eax],'.'
  91.     je    .readF
  92.     cmp   byte[eax],'-'
  93.     je    .readF
  94.     cmp   byte[eax],'0'
  95.     jb    @b
  96.     cmp   byte[eax],'9'
  97.     ja    @b
  98. .readF: ; read float
  99.     mov   esi,eax
  100.     push  eax
  101.     push  ecx
  102.  
  103.     call   atof   ; st0 - desired dword float
  104.  
  105.     pop   ecx
  106.     pop   eax
  107.  
  108.     fstp  dword[edi]
  109.     add   edi,4
  110.  
  111.     pop   ecx
  112.     inc   ecx
  113.     cmp   ecx,3
  114.     jne   .decode_coord
  115.     pop   ebx
  116.     inc   ebx
  117.     cmp   ebx,[points_count_var]
  118.     jne   .decode_vertices
  119.     mov   dword[edi],-1
  120.  
  121.  
  122.  
  123.     mov   esi,eax
  124.    @@:
  125.     inc   esi
  126.     cmp   dword[esi],'Face'
  127.     jne   @b
  128.     xor   edx,edx
  129.     mov   edi,[triangles_ptr]
  130.     cld
  131.  .decode_face:
  132.  
  133.     push  edx
  134.    @@:
  135.     inc   esi
  136.     cmp   dword[esi],'Face'
  137.     jne   @b
  138.  @@:
  139.     inc   esi
  140.     cmp   byte[esi],'0'    ; face number start
  141.     jb    @b
  142.     cmp   byte[esi],'9'
  143.     ja    @b
  144.   @@:
  145.     inc    esi
  146.     cmp    byte[esi],'0'
  147.     jb    @f
  148.     cmp    byte[esi],'9'   ; face number end
  149.     ja    @f
  150.     jmp   @b
  151.   @@:
  152.     xor ecx,ecx
  153.  .next_vertex_number:
  154.  
  155.     push  ecx
  156.   @@:
  157.     inc   esi
  158.     cmp   byte[esi],'0'
  159.     jb    @b
  160.     cmp   byte[esi],'9'
  161.     ja    @b
  162.     ;    eax - start ascii number
  163.   @@:
  164.     inc    esi
  165.     cmp    byte[esi],'0'
  166.     jb    @f
  167.     cmp    byte[esi],'9'
  168.     ja    @f
  169.     jmp   @b
  170.    ;  eax - end ascii number
  171.   @@:
  172.     dec   esi
  173.     mov   ebx,esi
  174.     push  esi
  175.     call  ascii_to_integer
  176.     mov   eax,edx
  177.  
  178.     stosd
  179.     pop   esi
  180.     add   esi,4
  181.  
  182.     pop   ecx
  183.     inc   ecx
  184.     cmp   ecx,3
  185.     jne   .next_vertex_number
  186.     pop   edx
  187.     inc   edx
  188.     cmp   edx,[triangles_count_var]
  189.     jne   .decode_face
  190.     mov   dword[edi],-1 ;dword[triangles+ebx+2],-1   ; end mark
  191.     mov   eax,1  ;-> mark if ok
  192. ret
  193.  
  194. ascii_to_integer:
  195. ; in ---  [ebx] -> end of ascii string
  196. ; out --  edx  -> desired number
  197.     xor    edx,edx
  198.     xor    ecx,ecx
  199.   .again:
  200.     movzx  eax,byte[ebx]
  201.     sub    al,'0'
  202.     cwde
  203.     push   edx
  204.     mul    dword[convert_muler+ecx]
  205.     pop    edx
  206.     add    edx,eax
  207.     dec    ebx
  208.     cmp    byte[ebx],'0'
  209.     jb     .end
  210.     cmp    byte[ebx],'9'
  211.     ja     .end
  212.     add    ecx,4
  213.     jmp    .again
  214.   @@:
  215.  
  216.   .end:
  217. ret
  218.  
  219. ;===============================================================================
  220. ;       ASCII to float conversion procedure
  221. ;
  222. ;       input:
  223. ;       esi - pointer to string
  224. ;
  225. ;       output:
  226. ;       st0 - number changed into float
  227. ;
  228. ;===============================================================================
  229.  
  230. atof:
  231.         .string  equ ebp-4
  232.  
  233.         push    ebp
  234.         mov     ebp,esp
  235.         sub     esp,32
  236.         push    eax ecx esi
  237.         mov     [.string],esi
  238.         fninit
  239.         fldz
  240.         fldz
  241.  
  242.         cld
  243.         cmp     byte [esi], '-'
  244.         jnz     @F
  245.         inc     esi
  246.     @@:
  247.         xor     eax, eax
  248.         align   4
  249.   .loop.integer_part:
  250.         lodsb
  251.         cmp     al, '.'
  252.         jz      .mantisa
  253.         cmp     al,'0'
  254.         jb      .exit
  255.         cmp     al,'9'
  256.         ja      .exit
  257.         fimul   [i10]
  258.         sub     al, '0'
  259.         push    eax
  260.         fiadd   dword [esp]
  261.         add     esp, 4
  262.         jmp     .loop.integer_part
  263.  
  264.   .mantisa:
  265.         xor      ecx, ecx
  266.         xor      eax, eax
  267.         cld
  268.         fxch     st1
  269.      @@:
  270.  
  271.         lodsb
  272.         cmp     al,'0'
  273.         jb      .exit
  274.         cmp     al,'9'
  275.         ja      .exit
  276.         cmp     ecx,7*4
  277.         je      .exit           ; max 7 digits in mantisa
  278.         sub     al,'0'
  279.         push    eax
  280.         fild    dword[esp]
  281.         fidiv   dword[convert_muler+4+ecx]
  282.         faddp
  283.         add     esp,4
  284.         add     ecx,4
  285.         jmp     @b
  286.   .exit:
  287.         faddp
  288.  
  289.         mov     eax, [.string]
  290.         cmp     byte [eax], '-'
  291.         jnz     @F
  292.         fchs
  293.     @@:
  294.         cld
  295.         stc                             ; always returns no error
  296.         pop     esi ecx eax
  297.         mov     esp,ebp
  298.         pop     ebp
  299.         ret
  300.  
  301.  
  302. itoa:              ; unsigned dword integer to ascii procedure
  303. ;     in   eax - variable
  304. ;          esi - Pointer to ascii string
  305. ;   out    esi - desired ascii string
  306. ;          edi - end of ascii string  - ptr to memory
  307.  .temp_string  equ dword[ebp-36]
  308.  .ptr          equ dword[ebp-40]
  309.  .var          equ dword[ebp-44]
  310.            push  ecx
  311.            push  ebp
  312.            mov   ebp,esp
  313.            sub   esp,64
  314.            mov   .var,eax
  315.            mov   eax,-1
  316.            lea   edi,.temp_string
  317.            cld
  318.            mov   ecx,9
  319.            rep   stosd      ; make floor
  320.  
  321.  
  322.            mov   .ptr,esi
  323.            lea   edi,.temp_string
  324.            add   edi,34
  325.            std
  326.            xor   eax,eax
  327.            stosb               ; mark begin
  328.            mov   eax,.var
  329.            mov   esi,10
  330.         @@:
  331.            xor   edx,edx
  332.            div   esi
  333.            xchg  eax,edx
  334.            add   al,'0'
  335.            stosb
  336.            xchg  eax,edx
  337.            or    eax,eax
  338.            jnz   @b
  339.            stosb                     ; mark end
  340.  
  341.            lea   esi,.temp_string
  342.            cld
  343.         @@:
  344.            lodsb
  345.            or   al,al
  346.            jnz  @b
  347.  
  348.            mov  edi,.ptr
  349.          @@:
  350.            lodsb
  351.            stosb
  352.            or   al,al
  353.            jnz  @b
  354.  
  355.            mov  esp,ebp
  356.            pop  ebp
  357.            pop  ecx
  358. ret
  359. if 1
  360. ftoa_mac:
  361. ;   in  :  esi - pointer to  dword float
  362. ;          edi - pointer to ascii string
  363.   .ptr_f     equ dword[ebp-4]
  364.   .sign      equ dword[ebp-8]   ;  0 ->   less than zero, 1 - otherwise
  365.   .ptr_ascii equ dword[ebp-12]
  366.   .integer   equ dword[ebp-20]
  367.   .fraction  equ dword[ebp-28]
  368.   .status_orginal equ word[ebp-32]
  369.   .status_changed equ word[ebp-34]
  370.          push     ecx
  371.          push     ebp
  372.          mov      ebp,esp
  373.          sub      esp,64
  374.         fninit
  375.         fnstcw  .status_orginal
  376.         mov     ax, .status_orginal
  377.         or      ax, 0000110000000000b
  378.         mov     .status_changed, ax
  379.         fldcw   .status_changed
  380. ;       --------------------------------
  381. ;       check if signed
  382.         xor     eax, eax
  383.         fld     dword[esi]
  384.         fst     .sign
  385.         test    .sign, 80000000h
  386.         setz    al
  387.         mov     .sign, eax
  388.  
  389.          mov       .ptr_f,esi
  390.          mov       .ptr_ascii,edi
  391.          fabs
  392.          fld       st0
  393.          frndint
  394.          fist      .integer
  395.          fsubp     st1, st0
  396.  
  397.          mov       eax,.integer
  398.          mov       esi,.ptr_ascii
  399.          call      itoa
  400.       ;  edi   ->   ptr to end of ascii string
  401.          dec       edi
  402.          mov       al,'.'
  403.          stosb
  404.  
  405.         mov     ecx, 6   ; max 6 digits in fraction part
  406.     .loop:
  407.         fimul   [i10]
  408.         fld     st0
  409.         frndint
  410.         fist    .fraction
  411.         fsubp   st1, st0
  412.         mov     esi,edi
  413.         mov     eax,.fraction
  414.         add     al,'0'
  415.         stosb
  416.         ftst
  417.         fnstsw  ax
  418.         test    ax, 0100000000000000b
  419.         jz      @F
  420.         test    ax, 0000010100000000b
  421.         jz      .finish
  422.     @@:
  423.         loop    .loop
  424. if 0
  425.         fldcw   .status_orginal
  426.         fimul   [i10]
  427.         fist    .fraction
  428.     ;    mov     esi,edi
  429.         mov     eax,.fraction
  430.         add     al,'0'
  431.         stosb
  432.     ;    call    itoa
  433. ;       --------------------------------
  434. ;       restore previous values
  435.     .finish:
  436.       ;  fstp    st0
  437.          ffree     st
  438.          mov       eax,.fraction
  439.          mov       esi,edi
  440. ;         call      itoa
  441.  
  442.          add       al,'0'
  443.          stosb
  444. end if
  445.  .finish:
  446.   ffree st
  447.          cmp       .sign,0
  448.          jnz       @f
  449.          mov       esi,.ptr_ascii
  450.          dec       esi
  451.          mov       byte[esi],'-'
  452.        @@:
  453.          mov       esp,ebp
  454.          pop       ebp
  455.          pop       ecx
  456.  
  457. ret
  458. end if
  459. if 0
  460. ;===============================================================================
  461. ;       float to ASCII conversion procedure
  462. ;
  463. ;       input:
  464. ;       buffer - pointer to memory where output will be saved
  465. ;       precision - number of digits after dot
  466. ;
  467. ;       output:
  468. ;       no immediate output
  469. ;
  470. ;       notes:
  471. ;       separate integer and mantisa part with dot '.'
  472. ;       so GOOD   123.456
  473. ;          WRONG  123,456
  474. ;
  475. ;       coded by Reverend // HTB + RAG
  476. ;===============================================================================
  477. proc    ftoa                    buffer, precision
  478. locals
  479.   status_original             dw ?
  480.   status_changed              dw ?
  481.   integer                     dd ?
  482.   mantisa                     dd ?
  483.   signed                      dd ?
  484. endl
  485.         push    eax ecx;edi ecx
  486. ;       --------------------------------
  487. ;       set correct precision
  488.         mov     eax, [precision]
  489.         cmp     eax, 51
  490.         jb      @F
  491.         mov     eax, 51
  492.     @@:
  493.         mov     [precision], eax
  494. ;       --------------------------------
  495. ;       change control wortd of fpu to prevent rounding
  496.         fnstcw  [status_original]
  497.         mov     ax, [status_original]
  498.         or      ax, 0000110000000000b
  499.         mov     [status_changed], ax
  500.         fldcw   [status_changed]
  501. ;       --------------------------------
  502. ;       check if signed
  503.         xor     eax, eax
  504.         fst     [signed]
  505.         test    [signed], 80000000h
  506.         setnz   al
  507.         mov     [signed], eax
  508. ;       --------------------------------
  509. ;       cut integer and mantisa separately
  510.         fld     st0
  511.         fld     st0                     ; st0 = x, st1 = x
  512.         frndint
  513.         fist    [integer]               ; st0 = x, st1 = x
  514.         fabs
  515.         fsubp   st1, st0                ; st0 = mantisa(x)
  516. ;       --------------------------------
  517. ;       save integer part in buffer
  518.     ;    mov     edi, [buffer]
  519.         mov     esi,[buffer]
  520.       ;  push    [signed]
  521.       ;  push    edi
  522.       ;  push    10
  523.       ;  push    [integer]
  524.         mov     eax,[integer]
  525.         call    itoa
  526.  ;       add     edi, eax
  527.         mov     al, '.'
  528.         stosb
  529.         mov     esi,edi
  530. ;       --------------------------------
  531. ;       save mantisa part in buffer
  532.         mov     ecx, [precision]
  533.         dec     ecx
  534.     .loop:
  535.         fimul   [i10]
  536.         fld     st0
  537.         frndint
  538.         fist    [mantisa]
  539.         fsubp   st1, st0
  540. ;        push    0
  541. ;        push    edi
  542. ;        push    10
  543. ;        push    [mantisa]
  544.         mov     esi,edi
  545.         mov     eax,[mantisa]
  546.         call    itoa
  547.       ;  add     edi, eax
  548.         ftst
  549.         fnstsw  ax
  550.         test    ax, 0100000000000000b
  551.         jz      @F
  552.         test    ax, 0000010100000000b
  553.         jz      .finish
  554.     @@:
  555.         loop    .loop
  556.         fldcw   [status_original]
  557.         fimul   [i10]
  558.         fist    [mantisa]
  559. ;        push    0
  560. ;        push    edi
  561. ;        push    10
  562. ;        push    [mantisa]
  563.         mov     esi,edi
  564.         mov     eax,[mantisa]
  565.         call    itoa
  566. ;       --------------------------------
  567. ;       restore previous values
  568.     .finish:
  569.         fstp    st0
  570.         cmp     [signed],1
  571.         jnz   @f
  572.         mov   byte[buffer],'-'
  573.       @@:
  574.         stc
  575.         pop     ecx eax   ;edi eax
  576.         ret
  577. endp
  578. end if
  579. if 0
  580. write_asc:
  581.     .counter equ dword[ebp-4]
  582.         push  ebp
  583.         mov   ebp,esp
  584.         sub   esp,64
  585.         fninit
  586.         mov   edi,asc_file_buffer
  587.         mov   esi,asc_main_header
  588.         cld
  589.       @@:
  590.         lodsb
  591.         cmp   al,1
  592.         jz    @f
  593.         stosb
  594.         jmp   @b
  595.       @@:
  596.  
  597.         mov   esi,asc_info_header
  598.       @@:
  599.         lodsb
  600.         cmp   al,1
  601.         jz    @f
  602.         stosb
  603.         jmp   @b
  604.      @@:
  605.         push   esi  ; -> position in header info
  606.         movzx  eax,[points_count_var]
  607.         mov    esi,edi
  608.         call   itoa              ; unsigned dword integer to ascii procedure
  609.         pop   esi
  610.         inc   esi
  611.       @@:
  612.         lodsb
  613.         cmp   al,1
  614.         jz    @f
  615.         stosb
  616.         jmp   @b
  617.       @@:
  618.         push  esi
  619.         movzx eax,[triangles_count_var]
  620.         mov   esi,edi
  621.         call  itoa
  622.         pop   esi
  623.         inc   esi
  624.       @@:
  625.         lodsb
  626.         cmp   al,1
  627.         jz    @f
  628.         stosb
  629.         jmp   @b
  630.       @@:
  631. ;=============================================================
  632. ;================vertex list parser===========================
  633. ;=============================================================
  634.  
  635.        xor   ecx,ecx
  636.      .again_vertex:
  637.        push  ecx
  638.        mov   esi,asc_one_vertex_formula
  639.      @@:
  640.        lodsb
  641.        cmp   al,1
  642.        jz    @f
  643.        stosb
  644.        jmp   @b
  645.      @@:
  646.        mov   eax,ecx
  647.  ;      push  ecx
  648.        push  esi
  649.        mov   esi,edi
  650.        call  itoa
  651.        pop   esi
  652. ;       pop   ecx
  653.        inc   esi
  654.        xor   ebx,ebx
  655.     .next_vertex_coef:
  656.        push  ebx
  657.      @@:
  658.        lodsb
  659.        cmp   al,1
  660.        jz    @f
  661.        stosb
  662.        jmp   @b
  663.      @@:
  664.     ; int3
  665.        push  esi
  666.        lea   esi,[ecx*3]
  667.        shl   esi,2
  668.        add   esi,points_r
  669.        add   esi,ebx
  670.      ;  int3
  671.        call  ftoa_mac
  672.      ;  std
  673.      ;  fld     dword[esi]
  674.  
  675.  
  676.      ;  pushad
  677.      ;  stdcall ftoa, edi, 30
  678.      ;  popad
  679.      ;  add   edi,20
  680.  
  681.  
  682.        pop   esi
  683.        pop   ebx
  684.        add   ebx,4
  685.        cmp   ebx,12
  686.        jnz   .next_vertex_coef
  687.      @@:
  688.        lodsb
  689.        cmp   al,1
  690.        jz    @f
  691.        stosb
  692.        jmp   @b
  693.      @@:
  694.        pop   ecx
  695.        inc   ecx
  696.        cmp   cx,[points_count_var]
  697.        jnz   .again_vertex
  698.  
  699.  
  700.    ;   mov  edi,[temp_edi]
  701.  
  702.  
  703.        mov  esi,asc_face_list_header
  704.       @@:
  705.         lodsb
  706.         cmp   al,1      ; all face header
  707.         jz    @f
  708.         stosb
  709.         jmp   @b
  710.       @@:
  711.            ;=====================================
  712.            ; ==============face list parser======
  713.            ;=====================================
  714.        xor   ecx,ecx
  715.      .again_face:
  716.        push  ecx
  717.        mov   .counter,ecx
  718.        mov   esi,asc_one_face_formula
  719.      @@:
  720.        lodsb
  721.        cmp   al,1
  722.        jz    @f
  723.        stosb
  724.        jmp   @b
  725.      @@:
  726.        mov   eax,ecx
  727.        push  esi
  728.        mov   esi,edi
  729.        call  itoa
  730.        pop   esi
  731.        inc   esi
  732.      @@:
  733.        lodsb
  734.        cmp   al,1
  735.        jz    @f
  736.        stosb
  737.        jmp   @b
  738.      @@:
  739.  
  740.        xor    ebx,ebx
  741.       .next_face_index:
  742.         push   ebx
  743.         mov    ecx,.counter
  744.         lea    ecx,[ecx*3]
  745.         add    ecx,ecx
  746.         movzx  eax,word[triangles+ecx+ebx]
  747.         push   esi
  748.         mov    esi,edi
  749.         call   itoa
  750.         pop    esi
  751.        @@:
  752.         lodsb
  753.         cmp   al,1
  754.         jz    @f
  755.         stosb
  756.         jmp   @b
  757.       @@:
  758.         pop    ebx
  759.         add    ebx,2
  760.         cmp    ebx,6
  761.         jnz    .next_face_index
  762.  
  763. ;        push   esi
  764.         mov   esi,asc_material
  765.      @@:
  766.         lodsb
  767.         cmp   al,1
  768.         jz    @f
  769.         stosb
  770.         jmp   @b
  771.      @@:
  772. ;        pop   esi
  773.  
  774.          pop    ecx
  775.         inc    ecx
  776.         cmp    cx,[triangles_count_var]
  777.         jnz    .again_face
  778.  
  779.         ; write file
  780.     sub  edi,asc_file_buffer
  781.   ;  mov  [file_buffer+2],edi
  782.     mov  [FileSize],edi
  783.  
  784.     invoke  CreateFile,asc_file_name, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
  785.     mov  [hfile],eax
  786.     invoke  WriteFile,eax,asc_file_buffer,[FileSize], byteswritten, 0
  787.     invoke  CloseHandle,[hfile]
  788.  
  789.     mov  esp,ebp
  790.     pop  ebp
  791. ret
  792. end if
  793. if 0
  794. asc_file_buffer:
  795.               rd 65536
  796. ascii_string  rb 50
  797. ftoa_muler dd 1000000000
  798. file_size dd ?
  799. file_handle dd ?
  800. end if
  801.  
  802. if 0
  803. convert_muler:
  804. dd 1, 10, 100, 1000, 10000
  805. XYZpartices:
  806. db 'X','Y','Z'
  807. i10 dw 10
  808. points_count_var dd ?
  809. triangles_count_var dd ?
  810. points rb 100
  811. triangles rb 100
  812. asc_file:
  813.  
  814. file "2TORUS.ASC"
  815. end if