home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / APOG / ASM7.ZIP / WRDDECSP.ASM < prev    next >
Encoding:
Assembly Source File  |  1987-02-14  |  3.4 KB  |  121 lines

  1.         name    wrddecsp
  2.         page    55,132
  3.         title   WORD_TO_DEC_SP - binary to ASCII decimal, Leading Spaces'
  4.     subttl Adapted from DUNCAN.ARC, Downloaded from VOR BBS
  5.  
  6. ; Call with AX = value to convert
  7. ;           DL = Character to replace leading zeros with
  8. ;           DI = address for string, which should be at least 6 bytes long
  9.  
  10. ; Returns with CX = count of actual characters written
  11. ;              DX = address of first character written
  12. ;              Direction flag forward (cleared)
  13. ; Destroys registers AX, CX, DX, DI
  14.  
  15. ; Based on Dan Daetwyler's binary to ASCII decimal conversion routines.
  16. ; Lew Paper
  17. ; 2/14/87
  18.  
  19.     PUBLIC    word_to_dec_sp
  20.  
  21. convert_99 MACRO            ;; Convert a 2 digit number in AL to
  22.                     ;; ASCII and store it in one position
  23.                     ;; to the left of the byte pointed
  24.                     ;; at by SI.  Update SI
  25.     AAM                ; Second low digit => AH
  26.                     ; Low digit => AL
  27.     OR    AX,'00'            ; Convert to ASCII
  28.     STOSB                ; Low digit
  29.     MOV    AL,AH            ; Second low digit => AL
  30.     STOSB                ; Store as AL, then AH
  31.     ENDM                ; convert_99
  32. cseg     segment para public 'CODE'
  33.  
  34.     assume    cs:cseg,ds:cseg,es:cseg,ss:cseg
  35.  
  36. word_to_dec_sp    PROC    NEAR
  37.  
  38.     XCHG    AX,DX            ; Fill character => AX
  39.                     ; Number => DX
  40.     MOV    AH,AL            ; Duplicate fill character for
  41.                     ; word fill
  42.     MOV    CX,3            ; Number of words to fill.
  43.                     ; Makes 6 characters
  44.     REP    STOSW            ; Fill string with spaces
  45.     PUSH    DI            ; Offset of end of string + 1
  46.     DEC    DI            ; Point DI at the end of string
  47.  
  48.     OR    DX,DX            ; Check for zero and negative
  49.     JNZ    not_zero        ; Not zero
  50.     MOV    BYTE PTR [DI],'0'    ; Write zero
  51.     DEC    DI            ; Simulate STOSB
  52.     JMP    exit
  53.  
  54. not_zero:
  55.     STD                ; Set direction flag backwards
  56.     PUSHF                ; Save sign and direction
  57.     MOV    AX,DX            ; Number => AX
  58.     JNS    positive        ; Positive number
  59.     NEG    AX            ; Make negative number positive
  60. positive:
  61.     TEST    AX,0FF00H        ; Is it less than 256?
  62.     JZ    less_than_256        ; Yes
  63.  
  64.     XOR    DX,DX            ; Prepare to divide
  65.     MOV    CX,100            ; To get low order two digits
  66.     DIV    CX            ; Number / 100 => AX
  67.                     ; Number mod 100 => DX
  68.     XCHG    AX,DX            ; Number mod 100 => AX
  69.                     ; Number / 100 => AX
  70.     convert_99            ; Convert and store the next 2 digits
  71.     MOV    AX,DX            ; Upper three digits
  72.     TEST    AX,0FF00H        ; Are they less than 256
  73.     JZ    less_than_256        ; Yes
  74.  
  75.     MOV    CL,10            ; New divisor
  76.     DIV    CL            ; Top digit => AH
  77.                     ; Bottom two digits => AL
  78.     OR    AH,'0'            ; Convert top digit to ASCII
  79.     MOV    [DI],AH            ; Store top digit
  80.     DEC    DI            ; End simulate STOSB
  81.     convert_99            ; Convert and store the next 2 digits
  82.     JMP    SHORT check_sign    ; Check sign and exit
  83.  
  84. less_than_256:
  85.     AAM                ; Upper two digits => AH
  86.                     ; Lower digit => AL
  87.     OR    AL,'0'            ; Convert to ASCII
  88.     STOSB                ; Save it
  89.     OR    AH,AH            ; Is it a three digit number?
  90.     JZ    check_sign        ; No
  91.     MOV    AL,AH            ; Top two digits => AL
  92.     AAM                ; Top digit => AH
  93.                     ; Second to top digit => AL
  94.     OR    AX,'00'            ; Convert to ASCII
  95.     STOSB                ; Store second high digit
  96.     CMP    AH,'0'            ; Is it a four digit number
  97.     JE    check_sign        ; Yes
  98.     MOV    AL,AH            ; Top digit => AL
  99.     STOSB
  100.  
  101. check_sign:
  102.     POPF                ; Sign flag
  103.     JNS    exit            ; Positive
  104.     MOV    AL,'-'            ; Sign
  105.     STOSB                ; Store it
  106.  
  107. exit:
  108.     CLD                ; Set direction flag forward
  109.     MOV    DX,DI            ; Offset of start of string - 1
  110.     INC    DX            ; Offset of start of string
  111.     POP    CX            ; Offset of end of string + 1
  112.     SUB    CX,DX            ; Number of characters in string
  113.     RET                ; To caller
  114.  
  115. word_to_dec_sp    ENDP
  116.  
  117. cseg     ends
  118.  
  119.     end
  120.  
  121.