HELP! Number Conversion Problem

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!!


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


  • [color=Blue]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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion