# how can i convert dword integer value to ascii character

iam very new in assemble and i will be very thankful
if someone help me in that matter
iam already can convert byte and word integer value to ascii character
and print it
but if i want output integer value longer that what i need to do?
thanks

• If you have no objection to using 32-bit instructions, then it will be easy for you: just use a 32-bit DIV much like the conversions for words and bytes to decimals. Try http://www.df.lth.se/~john_e/gems/gem002a.html if you need a 64-bit DIV routine. If you need to write for a 16-bit processor (say for an ASM class), I know there's code somewhere on the net to do the DIV.

Untested:
[code]
PrintDecimal32:
mov ebp,10 ; Destination base is decimal
xor cx,cx ; CX = 0, it will be our digit counter

.DivideAgain:
xor edx,edx ; Clear high portion of dividend to prevent divide errors
div ebp ; EDX:EAX/EBP -> EAX r EDX

push dx ; Push the remainder (always 0-9) onto the stack
inc cx ; Increment the number of digits we know we have on the stack

test eax,eax ; Is the quotient from DIV zero?
jnz .DivideAgain ; If not, then there are more digits to be extracted from the number, so loop

.PrintMore:
pop ax ; Pop the next digit to be printed (0-9)
add al,"0" ; Convert it to an ASCII character ("0"-"9")
int 29h ; Print it
dec cx ; Decrement our counter of the number of digits we know are remaining on the stack
jnz .PrintMore ; Zero? If not then we have more digits to print.

[/code]
• [b][red]This message was edited by Al_Mo3tasm at 2005-11-21 2:7:50[/red][/b][hr]
: If you have no objection to using 32-bit instructions, then it will be easy for you: just use a 32-bit DIV much like the conversions for words and bytes to decimals. Try http://www.df.lth.se/~john_e/gems/gem002a.html if you need a 64-bit DIV routine. If you need to write for a 16-bit processor (say for an ASM class), I know there's code somewhere on the net to do the DIV.
:
: Untested:
: [code]
: PrintDecimal32:
: mov ebp,10 ; Destination base is decimal
: xor cx,cx ; CX = 0, it will be our digit counter
:
: .DivideAgain:
: xor edx,edx ; Clear high portion of dividend to prevent divide errors
: div ebp ; EDX:EAX/EBP -> EAX r EDX
:
: push dx ; Push the remainder (always 0-9) onto the stack
: inc cx ; Increment the number of digits we know we have on the stack
:
: test eax,eax ; Is the quotient from DIV zero?
: jnz .DivideAgain ; If not, then there are more digits to be extracted from the number, so loop
:
: .PrintMore:
: pop ax ; Pop the next digit to be printed (0-9)
: add al,"0" ; Convert it to an ASCII character ("0"-"9")
: int 29h ; Print it
: dec cx ; Decrement our counter of the number of digits we know are remaining on the stack
: jnz .PrintMore ; Zero? If not then we have more digits to print.
:
: [/code]
:

[b][size=4][blue]If you need to write for a 16-bit processor? yes my friend this what i need unfortunately :-(

• It has been so long since I wrote ASM, I don't even understand this
16 bit Dword Decimal printer with CRLF
BUT, it was in my "already checked out directory" so here it is, in FASM

[CODE]
; ------ WDDD = Write a Decimal Digit Dword at the cursor & a CRLF ------
;
; Call with, DX:AX = Dword value to print
; Returns, CF set on error, if DX:AX > 655359 max#
; (see WDECEAX.inc for larger number prints)
align 16
WDDD: CMP DX,10
JB WDDDok
STC ;CF=set
RET ;error DX:AX exceeded max value
WDDDok: PUSH AX
PUSH BX
PUSH CX
PUSH DX
XOR CX,CX ; clear count register for push count
MOV BX,10
WDDDnz: DIV BX ; divide DX:AX by BX=10
PUSH DX ; put least siginificant number (remainder) on stack
XOR DX,DX ; clear remainder reciever for next divide
OR AX,AX ; check to see if AX=number is divided to 0 yet
LOOPNE WDDDnz ; get another digit? count the pushes
MOV AH,2 ; function 2 for interupt 21h write digit
NEG CX ; two's compliment, reverse CX
MOV BL,48 ; '0'
WDDDwr: POP DX ; get digit to print, last pushed=most significant
ADD DL,BL ; convert remainder to ASCII character
INT 21h ; print ascii interupt 21h ( function 2 in AH )
LOOP WDDDwr ; deincrement CX, write another, if CX=0 we done
MOV DL,13 ; CR carriage return (AH=2 still)
INT 21h
MOV DL,10 ; LF line feed
INT 21h
POP DX
POP CX
POP BX
POP AX
CLC ;CF=clear, sucess
RET

; A divide error occurs if DX has any value
; when DIV trys to put the remainder into it
; after the DIVide is completed.
; So, DX:AX can be a larger number if the divisor is a larger number.
[/CODE]