Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-18-2001, 04:34 PM   #1
Member (6 bit)
 
Join Date: May 1999
Location: Wales
Posts: 40
Angry Fire how to convert ascii to decimal in assembler ?

I need to write a program that converts a character (entered from keyboard & echoed back to screen) into ascii and output it to a dos screen, I have managed to do this but cannot understand how on earth i can convert the ascii to a Decimal number and output to the dos screen(without having a massive lookup table for the entire ascii codes).

Any help with this problem would be appreciated.

(PS: in 8x86 assembler, and im using Tasm)
Awesome_Nut is offline   Reply With Quote
Old 05-18-2001, 04:47 PM   #2
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Well, I don't understand what you mean about "converting the character entered into ASCII" -- because the character should BE in ASCII already.

To convert an ascii string representing a decimal number, use the following pseudocode:

int the_result = 0

for (each character starting with most significant){

char ch = (the character);
if (ch < 0x30 || ch > 0x39) then "error, not a valid digit";
int the_decimal = ch - 0x30; // (0x30 is '0')
the_result = the_result * 10; //shift all previously entered digits to the left
the_result = the_result + the_decimal; // add the new digit

} //end of for loop.

This is of course simple, it does not allow for decimal points or signs, which you may need, but the basic idea is the same.
__________________
Paul M. Victorey
------------------
I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk.
Paul Victorey is offline   Reply With Quote
Old 05-18-2001, 04:50 PM   #3
MITotaku
Guest
 
Posts: n/a
i think he meant numeral or digit... or maybe he meant convert char into numeral... now I'm confused. I think I'm going to learn assembler now, it seems a lot of people use it - or is it just a required thing to learn for programming courses???
  Reply With Quote
Old 05-18-2001, 05:12 PM   #4
Member (6 bit)
 
Join Date: May 1999
Location: Wales
Posts: 40
In assembler:

To display a character's Ascii code I have to mov it from AX register to DL, but printing this to screen would result in just the character being displayed rather than the ascii. SO if i pressed 'a' the ouput would be 'a' rather than 61h just for you to get an idea

-anway i have done this bit, i just needed to know how to convert the ascii(eg 61h) into a Decimal numer (eg 97) for all ascii codes. Is there some king of formula ?

the screen output would look something like this:

Char Ascii Decimal
a 61 97
b 62 98

I could post the code if you want but its a bit long.
Awesome_Nut is offline   Reply With Quote
Old 05-18-2001, 11:44 PM   #5
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Ohhhhhhh, you are doing the OPPOSITE of what I thought you meant!

You are desiring to take an integer (because each character, for example 'a', would internally be stored as the integer 0x0061) and make it into an ASCII string to print to the screen.

Well, you have the character as an int. Now, the easiest way is to convert the int from the least significant (decimal) digit to the most significant (decimal) digit. You could use the stack to reverse the order; this is especially easy if you decide simply to use a 3 digit code (i.e. output "000" through "255")

I think this will work (it's harder as x86 does not have a modulo operation)

assume AX starts off with the character in its integer format:

Also, my apoligies if the commands are inaccurate, I'm doing this from memory.

push ax
push bx
puch cx
push dx ;just in case we need them later
mov cx, 0 ;cx holds a record of how many times through the loop we go
:loop
mov bx,ax ;make a copy of ax in bx.
idiv ax,10 ;Do integer division of ax by 10 -- this gets rid of the lowest digit
mov dx, ax ;we need this divided by ten number later.
imul ax,10 ;Now, bx and ax differ only in the least significant (decimal) digit
sub bx,ax ;Now, bx has the least significant (decimal) digit -- effectively we've done the modulus
add bx, 0x30 ;this converts from a decimal digit to the ASCII representation for that digit
push bx
mov ax, dx ;put the /10 copy into the main register for use on the next loop
inc cx ;increment the counter
cmp cx,3
jne loop

;now, we have the ASCII string on the stack, so we just pop it off three times and display those characters. We could then also pop the original registers if we needed them.
Paul Victorey is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:20 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2