;; Bin2Dec--Convert a word of binary to a decimal character string ;; ;; Input Parameters: ;; ax = the word to be converted ;; ds:di = start address for resulting string of decimal characters ;; ;; Output Parameters: ;; ds:di = address of first character after end of output string ;; ;; Errors: none ;; ;; Library source text from "Assembly Language for the IBM PC Family" by ;; William B. Jones, (c) Copyright 1992, 1997, Scott/Jones Inc. ;; .MODEL SMALL .DATA M32768 DB '-32768' ; Special case PowerOf10 DW 10000, 1000, 100, 10 .CODE PUBLIC Bin2Dec Bin2Dec PROC push es ; Save registers for later restoring push ds push ax push bx push cx push dx push si pushf cld ; Make sure direction is increasing mov bx, ds mov es, bx ; es := ds (can't be done directly) ASSUME es : NOTHING ; can't make any assumptions now mov bx, @data mov ds, bx ; ds --> .DATA segment ASSUME ds : @data cmp ax, -32768 ; Not really necessary, but comforting je SpecCase test ax, ax ; If ax < 0 jge NonNeg mov BYTE PTR es:[di], '-' ; ...output '-' inc di; ...updating di (can't use ; stosb as it uses ax) neg ax ; ...and set ax := -ax NonNeg: mov si, OFFSET PowerOf10 mov cx, 4 ; Number of items in PowerOf10 array RangeLoop: cmp ax, [si] ; Find size of argument jge GetDigits add si, 2 loop RangeLoop jmp OneDigit GetDigits: cwd div WORD PTR [si] add al, '0' stosb mov ax, dx add si, 2 loop GetDigits OneDigit: add al, '0' stosb Exit: popf ; Restore registers pop si pop dx pop cx pop bx pop ax pop ds pop es ret SpecCase: mov si, OFFSET M32768 movsw movsw movsw jmp Exit Bin2Dec ENDP END