Converting a 4-byte packed (BCD) decimal number to a string of ASCII decimals.

Started by kponenation, May 03, 2005, 11:26:46 PM

Previous topic - Next topic

kponenation

I need to write a procedure that coverts a 4-byte packed BCD to a string of ASCII decimals...
Can anyone help me with this? Thank you.

pbrennick

kponenation,
Welcome to the forum, I hope you find it a useful tool.  About your programming problem, why don't you post your best effort and then we will help you perfect it.

Paul

raymond

Being new to this forum, you may not have seen this recent thread.

http://www.masmforum.com/simple/index.php?topic=1271.0

You could have found that thread by doing a search with "BCD" for example. You can find a lot of information already existing on this forum if you search for it.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

kponenation

I'll try my best to write the code but i'm new to assemly language...(i'm a java programmer :))
let's say my eax= 12345678 and i want to covert this number.

.data
array byte 9 DUP()

.code
mov ebx, eax ; b/c i don't want to modify eax
add ebx,0F0000000h ; to get the first digit. in this case 1. (is this correct?)

now i need to take the first digit (in this case 1) and "OR" this with 30h  to get ASCII
but i don't know how..... and I don't know how to put the result back to the array
I also want to know how to take the next digit and repeat this process until I reach the last digit.
and printing the ascii to a screen.
Thank you guys for replying...

AeroASM

the "add" should be an "and" because it is a bit mask. Once you have done that you have got "1000000" instead of "12345678".
However you want "00000001". There is an instruction shr to do that.

Now you have 1 in hexadecimal in register bl. You need to convert that to the ASCII value of "1" (look in a character map) nad then just write it to the array.

(NOTE that the hexadecimal interpretation of a number is the same as the packed BCD interpretation of it)

thomasantony

Hi,
  Aero is right. You only have to use the dw2hex proc in MASM32 lib. Other wise here is a small proc to convert a BCD byte to binary

; eax contains byte to convert to binary
BCDtoBin:
mov edx,eax
and edx,0Fh ; Take lower nibble in DL
shr eax,4 ; Take upper nibble in AL
mov ecx,eax ; multiply upper nibble by 10
shl ecx,3 ; multiply one part by 8
shl eax,1 ; other part by 2
add eax,ecx ; add both to get number * 10
add eax,edx ; add lower nibble to get binary
and eax,0FFh                  ; use only lower byte
ret


Thomas
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

kponenation

Thank you guys...
So after I convert "1" to ASCII do I need to save this to an array?
how do I do this?

pbrennick

kponenation,
You would create a buffer with the proper size for the conversion plus one extra byte (for the ASCIIZ terminator).  Now when you get each byte, use ecx by starting with a value of 1 and then add 1 to ecx after each conversion.  Now you can use ecx as an index into the buffer (or array).  When all the conversions are done store a zero in the last buffer location so that it becomes an ASCIIZ string that you can easily print to the screen.

hth:
Paul

raymond

Thomas

QuoteYou only have to use the dw2hex proc in MASM32 lib

How will the use of the dw2hex proc help in converting packed BCDs to ASCII (the subject of this thread)???? ::)

Strait conversion is so much easier.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com