home *** CD-ROM | disk | FTP | other *** search
- name wrddecsp
- page 55,132
- title WORD_TO_DEC_SP - binary to ASCII decimal, Leading Spaces'
- subttl Adapted from DUNCAN.ARC, Downloaded from VOR BBS
-
- ; Call with AX = value to convert
- ; DL = Character to replace leading zeros with
- ; DI = address for string, which should be at least 6 bytes long
-
- ; Returns with CX = count of actual characters written
- ; DX = address of first character written
- ; Direction flag forward (cleared)
- ; Destroys registers AX, CX, DX, DI
-
- ; Based on Dan Daetwyler's binary to ASCII decimal conversion routines.
- ; Lew Paper
- ; 2/14/87
-
- PUBLIC word_to_dec_sp
-
- convert_99 MACRO ;; Convert a 2 digit number in AL to
- ;; ASCII and store it in one position
- ;; to the left of the byte pointed
- ;; at by SI. Update SI
- AAM ; Second low digit => AH
- ; Low digit => AL
- OR AX,'00' ; Convert to ASCII
- STOSB ; Low digit
- MOV AL,AH ; Second low digit => AL
- STOSB ; Store as AL, then AH
- ENDM ; convert_99
- cseg segment para public 'CODE'
-
- assume cs:cseg,ds:cseg,es:cseg,ss:cseg
-
- word_to_dec_sp PROC NEAR
-
- XCHG AX,DX ; Fill character => AX
- ; Number => DX
- MOV AH,AL ; Duplicate fill character for
- ; word fill
- MOV CX,3 ; Number of words to fill.
- ; Makes 6 characters
- REP STOSW ; Fill string with spaces
- PUSH DI ; Offset of end of string + 1
- DEC DI ; Point DI at the end of string
-
- OR DX,DX ; Check for zero and negative
- JNZ not_zero ; Not zero
- MOV BYTE PTR [DI],'0' ; Write zero
- DEC DI ; Simulate STOSB
- JMP exit
-
- not_zero:
- STD ; Set direction flag backwards
- PUSHF ; Save sign and direction
- MOV AX,DX ; Number => AX
- JNS positive ; Positive number
- NEG AX ; Make negative number positive
- positive:
- TEST AX,0FF00H ; Is it less than 256?
- JZ less_than_256 ; Yes
-
- XOR DX,DX ; Prepare to divide
- MOV CX,100 ; To get low order two digits
- DIV CX ; Number / 100 => AX
- ; Number mod 100 => DX
- XCHG AX,DX ; Number mod 100 => AX
- ; Number / 100 => AX
- convert_99 ; Convert and store the next 2 digits
- MOV AX,DX ; Upper three digits
- TEST AX,0FF00H ; Are they less than 256
- JZ less_than_256 ; Yes
-
- MOV CL,10 ; New divisor
- DIV CL ; Top digit => AH
- ; Bottom two digits => AL
- OR AH,'0' ; Convert top digit to ASCII
- MOV [DI],AH ; Store top digit
- DEC DI ; End simulate STOSB
- convert_99 ; Convert and store the next 2 digits
- JMP SHORT check_sign ; Check sign and exit
-
- less_than_256:
- AAM ; Upper two digits => AH
- ; Lower digit => AL
- OR AL,'0' ; Convert to ASCII
- STOSB ; Save it
- OR AH,AH ; Is it a three digit number?
- JZ check_sign ; No
- MOV AL,AH ; Top two digits => AL
- AAM ; Top digit => AH
- ; Second to top digit => AL
- OR AX,'00' ; Convert to ASCII
- STOSB ; Store second high digit
- CMP AH,'0' ; Is it a four digit number
- JE check_sign ; Yes
- MOV AL,AH ; Top digit => AL
- STOSB
-
- check_sign:
- POPF ; Sign flag
- JNS exit ; Positive
- MOV AL,'-' ; Sign
- STOSB ; Store it
-
- exit:
- CLD ; Set direction flag forward
- MOV DX,DI ; Offset of start of string - 1
- INC DX ; Offset of start of string
- POP CX ; Offset of end of string + 1
- SUB CX,DX ; Number of characters in string
- RET ; To caller
-
- word_to_dec_sp ENDP
-
- cseg ends
-
- end
-