• Please review our updated Terms and Rules here

Yet another INT to ASCII string routine ...

neilobremski

Experienced Member
Joined
Oct 9, 2016
Messages
55
Location
Seattle, USA
If you've been following along then you know I'm doing some programming in assembler using DEBUG. Very early on in any program with an interface, one is faced with displaying an integer in decimal (base 10) which means converting it to ASCII characters. It's the end of the day for me so I didn't want to get too fancy but I did want to try this on my own and here's what I've come up with ...

Code:
XOR DX, DX	; zero DX (used for high word in 32 bit divide)
DIV BX		; divide by decimal place "BX" (1h, Ah, 64h, 3E8h, 2710h ...)
ADD AL, 30	; convert result to ASCII character (30h = '0')
STOSB		; store character in ES:DI and increment DI
MOV CX, DX	; save division remainder in CX
XOR DX, DX	; zero DX (remember it's used for high word in 32-bit DIV)
MOV AX, BX	; divide decimal place "BX" ...
MOV BX, A	; ... by 10 ...
DIV BX		; ... to move to the next decimal place
MOV BX, AX	; new decimal place in "BX"
MOV AX, CX	; put first remainder into accumulator (dividend)
CMP BX, 1	; last decimal place?
JNZ (-19)	; back to top! (JNZ 100 if starting at 100h in DEBUG)
ADD AL, 30	; convert ones to ASCII
STOSB		; store ones place in ES:DI and increment DI

To call this, put the integer in AX and the maximum place value in BX. Thus if you want to display 4 digits, you must put 1000 (3E8h) in BX. This routine assumes that BX is evenly divisible by 10 (Ah) and that the maximum number of digits is represented by BX. For example, if you have 999 in AX and you try to set BX to 10 then this will fail ... probably horribly. Also, if BX is not divisible by 10 then the result is going to be bad.

Anyway, it's good enough for quickly converting fixed number strings like that of the move count in Magenta's Maze. I'll make some modifications later for error checking and space-padding.
 
Back
Top