x86 Assembly

Moderators: None (Apply to moderate this forum)
Number of threads: 4563
Number of posts: 16029

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP! Number Conversion Problem Posted by calvarado777 on 1 Mar 2011 at 9:41 AM
Hey guys! I am just getting started in assembly language and my head hurts from staring at this problem. I'll paste in the program requirements, then my program and show where I am stuck...any help would be appreciated..I have a test in 7 hrs!!

Problem:

Number Conversion

In this lab, you will be converting a sequence of characters read from the console into a simple unsigned integer number.


You will need four sections in your program:
Part 1 - Sign on

Print out a simple program message with the name of your program and a prompt for the user to enter an integer number. We will ask the user to type in at most four digits only. You may assume (never a good idea in real life) that the user is smart, and only enters what you ask: four or less digits followed by the Enter key. (No error checking is needed beyond this).

Part 2 - Getting user data

Read the user input using the get_kb routine defined in the C library (include the asm_io line for this to work). This routine returns a single character in the EAX register - only the low 8 bits (AL) are of interest. This routine does not echo the character the user typed on the screen - if you want that to happen, you will need to use the print_char routine to make that happen.

Part 3 - Converting the characters to numbers

The character you get from the user ends up in the AL register as a binary number that is the ASCII code for the key pressed. You will need to convert this code into the right numerical value (look at the ASCII code table and you will see that the digits are in order starting at a code of 30h. So to convert the code for the digit 3, we could subtract 30h from that code and we would get a binary value of 3. Sound right?

Think about how you would do the rest of the conversion. You will see the user’s digits left to right. How can you form the answer?

You will probably need to look up the instruction to multiply an integer by another integer to figure this out (Try this: oldnum = oldnum * 10 + new num. That should get you started)
Part 4 - Print out the answer

Once you have the number converted, use the print_int routine from your library to display the result. You should see the number the user typed in.
------------------------------------
My code so far:

_asm_main:
enter 0,0
mov eax, message
call print_string ; display startup message
call print_nl
call print_nl

mov eax, prompt ; prompt user for number
call print_string
call print_nl

number_entry:
call get_kb ; read keyboard input
call print_char ; displays first digit
cmp al, 0dh ; end of user input?
jne number_entry ; if not, loop again
sub al, 30h ; subtracts hex 0 from hex input
mov bl, al ; copies what user input to memory

call print_nl
mov eax, message2
call print_string

convert_number:

-------------------------------------

so I can input, and echo user input. What's not making sense is how I deal with what is put into the AL register and how I manipulate it bit by bit. Also I vaguely recall reading that an ASCII character is a byte long so when the character that is input by the user is saved to AL is it a byte long? SO CONFUSED!!! please help


Report
Re: HELP! Number Conversion Problem Posted by AsmGuru62 on 2 Mar 2011 at 5:33 AM
The key is that formula given to you as a hint:

oldnum = oldnum*10 + newnum

Reform it into this one (same, but values called properly):

result = result*10 + digit

Let us perform 3 steps of the algorithm (A1-A3):

1. Reserve some memory variable RESULT
2. Set RESULT=0

A1: Get character from user - say it was '4'
'4' - '0' will give binary 4 then follow formula:

RESULT = 0*10 + 4 (gives RESULT=4)

A2: Get character from user - say it was '7'
'7' - '0' will give binary 7 then follow formula:

RESULT = 4*10 + 7 (gives RESULT=47)

A3: Get character from user - say it was '0'
'0' - '0' will give binary 0 then follow formula:

RESULT = 47*10 + 0 (gives RESULT=470)

So, user typed in all: "470" and RESULT is now 470!
Which is what you need.

Now, before adding integer digit - you need to tun it into
full 32-bit integer, say digit is in DL - you need to clear
the rest of bits before addition:

AND EDX, 0FFh ; <-- Leaves only AL bits intact - rest are cleared

Multiplication is easy:

IMUL EAX,10 ; <-- multiply EAX by 10



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.