|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (6 bit)
Join Date: May 1999
Location: Wales
Posts: 40
|
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) |
|
|
|
|
|
#2 |
|
Member (12 bit)
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. |
|
|
|
|
|
#3 |
|
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???
|
|
|
|
#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. |
|
|
|
|
|
#5 |
|
Member (12 bit)
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. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|