English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

can anyone give me the code, or pseudo-code, or algorithm of how to convert a decimal number to its corresponding ASCII characters so that i can display it on a screen. Im coding in 8086 assembly.

2006-09-15 18:51:10 · 3 answers · asked by joker 2 in Computers & Internet Programming & Design

3 answers

if you can break the number down to single digits (i.e. 121 to 1, 2, 1), its as simple as or'ing it with 0x30. So a 1 becomes 0x31, 2 becomes 0x32, etc.

2006-09-16 02:53:31 · answer #1 · answered by justme 7 · 0 0

You want a procedure that takes a number (byte, word,dword) and prints out the number in ASCII? Okay.

In the spirit of pre-486 coding, I won't use floating point instructions for the division.
To keep this code shorter, I'll use 32 bit registers. So, you can print a number up to 0FFFFFFFFh.
After the ASCII numbers are copied to the buffer, characters 13,10, and '$' are added so that you can use DOS function 9 to print the buffer. Function 9 prints a '$' terminated string at DS:DX.

You do this with:
mov ax0900h
mov dx,offset
int 21h

NOTE: If you Assembler doesn't support the EVEN Assembler directive, then delete those lines.

even
printdec proc
;convert dword to ASCII decimal
;INPUT: DS:SI = offset to buffer for ASCII string
; EAX= unsigned number to convert

mov ebx,10
push 0ffffh

even
ddiv:
xor edx,edx
div ebx
cmp eax,0
je ddivdone
push dx
jmp ddiv

even
ddivdone:
push dx

even
ddpop:
pop dx
cmp dx,0ffffh
je dfix
add dl,48
mov byte ptr ds:[si],dl
inc si
jmp ddpop

even
dfix:
mov byte ptr ds:[si],13
inc si
mov byte ptr ds:[si],10
inc si
mov byte ptr ds:[si],'$'
ret
printdec endp

I hope I typed this out correctly.
The algorithm goes like this:
1) The ddiv loop divides the value in EAX by 10 each time through the loop. The remainder in EDX will be a number between 0 and 9, which is pushed onto the stack. When EAX (quotient) is zero, we exit the loop.
2) The ddpop loop pops each 0-9 value off the stack and adds 48 to it. Why? Well ASCII bytes 48-57 represent the ASCII numbers for 0-9.
Each ASCII 'digit' is then put into the buffer address that SI points to. The loop exits when 0FFFFh is found. Notice that I pushed that value onto the stack at the beginning of the procedure? Without it, the loop doesn't stop popping values off the stack!
3) Append bytes 10,13, and '$' so we can use DOS function 9 to print the string.

2006-09-15 20:26:42 · answer #2 · answered by Balk 6 · 0 0

OR a X '0F' to the sign posisition of the packed decimal number and then unpack the field.

Ex. today's date in decimal: 00 91 52 00 6C

OR X'0F'

Result 00 91 52 00 6F

Unpack field: F0 F9 F1 F5 F2 F0 F0 F6

Resulting printout: 09 15 2006 after editing

2006-09-15 19:00:42 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers