home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / BCDASM.ZIP / BCDASM / SRC / BIN2ASC.ASM < prev    next >
Encoding:
Assembly Source File  |  1997-06-03  |  4.6 KB  |  201 lines

  1.     title    BIN2ASC -- Copyright 1997, Morten Elling
  2.     subttl    Binary-to-Ascii number conversion
  3.  
  4.     include    model.inc
  5.     include    modelt.inc
  6.     include bin2asc.ash
  7.  
  8.     @CODESEG
  9.  
  10. ;//////////////////////////////////////////////////////////////////////
  11. ;//    Name    bin2asc
  12. ;//    Desc    Convert signed binary number to decimal Asciiz string.
  13. ;//
  14. ;//
  15. ;//    Entry    Passed args
  16. ;//    Exit    Left-justified Asciiz string returned to destination.
  17. ;//        Acc = string length (0 if bad srcsz).
  18. ;//
  19. ;//    Note    Destination storage must hold sufficient space.
  20. ;//        Insignificant leading zeros are not output.
  21. ;//
  22. ;//        Uses a shift-and-subtract algorithm whose primary 
  23. ;//        virtue is its ability to handle very large numbers.
  24.  
  25.     MAX_SIZEOF_BIN = 20h
  26.         DIVISOR = 10d           ; Decimal conversion
  27.  
  28. bin2asc proc
  29. arg    dstStr    :dataptr, \    ; Addr of Asciiz destination
  30.     srcBIN    :dataptr, \    ; Addr of signed binary source
  31.     srcsz    :@uint        ; Byte size of source (even, min. 2)
  32. local    @@src    :byte :MAX_SIZEOF_BIN ; Work buffer
  33. @uses    ds,es,rsi,rdi,rbx,rcx,rdx
  34. ;.
  35. ; ----- Check size parameter
  36.         @cld
  37.     mov   rax, [srcsz]
  38.     shr   rax, 1
  39.     jbe sh @@err
  40.     add   rax, rax
  41.     cmp   rax, MAX_SIZEOF_BIN
  42.     jbe sh @@cpy
  43. @@err:    @LES  rdi, [dstStr]    ; Set up pointer
  44.     jmp   @@end
  45.  
  46. ; ----- Copy src to local storage
  47. @@cpy:    xchg  rbx, rax        ; rbx = size of src
  48.     @LDS  rsi, [srcBIN]
  49.     @LDSEGM es, ss, rdi
  50.     lea   rdi, [@@src]
  51.     mov   rcx, rbx
  52.     rep   movsb
  53.     mov   ah, [rsi-1]    ; Get src's sign
  54.  
  55. ; ----- Load pointers
  56.     @LES  rdi, [dstStr]
  57.     if @isStackFar
  58.     @LDSEGM ds, ss, rsi
  59.     endif
  60.     lea   rsi, [@@src]
  61.     test  ah, 80h
  62.     mov   al, '+'           ; Assume positive
  63.     jns sh @@sgn        ; sf = 0 if positive
  64.  
  65. ; ----- Source is negative, negate it
  66.     mov   rcx, rbx
  67.     ; cf = 0 after 'test'
  68. @@neg:    mov   al, 00h
  69.     sbb   al, [rsi]
  70.     mov   [rsi], al
  71.     inc   rsi
  72.     dec   rcx
  73.     jnz   @@neg
  74.     mov   al, '-'
  75.  
  76. ; ----- Store sign character
  77. @@sgn:    stosb
  78.  
  79.  
  80. ; ----- Perform the conversion by repeatedly dividing source
  81. ;    by DIVISOR to extract one Ascii digit on each loop.
  82. ;    Repeat until quotient is zero.
  83. @@looptop:
  84.     mov   rdx, rbx        ; Byte size of src
  85.     @shl  rdx, 3        ; * 8 = no. of bits in source
  86.     lea   rsi, [@@src]    ; Addr of source
  87.     sub   rax, rax        ; Clear remainder
  88. @@shl:    dec   rdx        ; Decrement bit counter
  89.     js sh @@stor        ; Loop done when < 0
  90.     mov   rcx, rbx        ; Loop count =
  91.     shr   rcx, 1        ;   no. of source words, cf=0
  92.     @alignn
  93. @@shm:    rcl   @wptr [rsi], 1    ; Rotate word thru carry left
  94.     inc   rsi        ; Step
  95.     inc   rsi        ;   source pointer
  96.     dec   rcx        ; Loop
  97.     jnz   @@shm        ;   thru whole source
  98.     rcl   rax, 1        ; Update remainder
  99.     ;
  100.     sub   rsi, rbx        ; Point to @@src[0]
  101.     cmp   rax, DIVISOR    ; Compare remainder against divisor
  102.     jb    @@shl        ; Not yet
  103.     sub   rax, DIVISOR    ; One hit
  104.     inc   @bptr [rsi]    ; Update quotient (in low source)
  105.     jmp   @@shl        ; Keep going thru all bits
  106.  
  107. ; ----- Store digit in wrong order
  108. ;    (acc = source MOD DIVISOR)
  109. @@stor: or    al, '0'           ; Make Ascii
  110.     stosb            ; Store digit
  111.  
  112. ; ----- Check if quotient is zero
  113.     ; rsi -> @@src[0]
  114.     mov   rcx, rbx
  115.     sub   al, al
  116.     dec   rsi
  117. @@qzr:    inc   rsi
  118.     or    al, [rsi]
  119.     jnz   @@looptop     ; Keep going until quotient = 0
  120.     dec   rcx
  121.     jnz   @@qzr
  122.  
  123.  
  124. ; ----- Reverse digits in destination
  125.     mov   rsi, @uiptr [dstStr]
  126.     inc   rsi        ; Remove this line to make
  127.     ;            ;  the sign char. a suffix
  128.     mov   rcx, rdi
  129.     sub   rcx, rsi
  130.     sar   rcx, 1
  131.     jle sh @@end
  132.         push  rdi
  133.     if @isDataFar
  134.     @LDSEGM ds, es
  135.     endif
  136.     dec   rsi
  137. @@rv:    inc   rsi
  138.     dec   rdi
  139.     mov   al, [rsi]
  140.     mov   ah, [rdi]
  141.     mov   [rdi], al
  142.     mov   [rsi], ah
  143.     dec   rcx
  144.     jnz   @@rv
  145.     pop   rdi
  146.  
  147. ; ----- Return string length
  148. @@end:    mov   al, 00h        ; Zero-terminate string
  149.     stosb
  150.     lea   rax, [rdi-1]
  151.     sub   rax, @uiptr [dstStr]
  152.     RET
  153. bin2asc    endp
  154.  
  155.  
  156. ;//////////////////////////////////////////////////////////////////////
  157. ;//    Name    HexN
  158. ;//    Desc    Convert unsigned binary word/dword to hexadecimal
  159. ;//        Asciiz string.
  160. ;//
  161. ;//
  162. ;//    Entry    Passed args
  163. ;//    Exit    Acc = string length.
  164.  
  165. HexN    proc
  166. arg    pStr    :dataptr, \    ; Addr of destination Asciiz string
  167.     mval    :@uint        ; 16-bit: word; 32-bit: dword value
  168. @uses    es,rdi,rbx,rcx
  169. ;.
  170.     MSZ = @WordSize
  171.     @cld
  172.     @LES  rdi, [pStr]
  173.     lea   rbx, [mval+MSZ-1]    ; Start at high byte
  174.     mov   ch, MSZ
  175. @@hb:    if @isStackFar
  176.     mov   al, ss:[rbx]    ; ('SEGSS' hangs TASM32 v5.0)
  177.     else
  178.     mov   al, [rbx]
  179.     endif
  180.     mov   ah, al        ; Convert high nibble
  181.     @shr  al, 4
  182.     cmp   al, 10d
  183.     sbb   al, 69h
  184.     das
  185.     stosb
  186.     mov   al, ah        ; Convert low nibble
  187.     and   al, 0fh
  188.     cmp   al, 10d
  189.     sbb   al, 69h
  190.     das
  191.     stosb
  192.     dec   rbx        ; Next byte
  193.     dec   ch
  194.     jnz   @@hb
  195.     sub   rax, rax
  196.     stosb
  197.     mov   al, MSZ*2        ; Return string length
  198.     RET
  199. HexN    endp
  200.  
  201.     END