8086, hex to decimal program , converting prob

Discussion in 'Homework Help' started by fortune2k, Mar 31, 2009.

  1. fortune2k

    fortune2k Thread Starter Member

    Joined:
    Mar 18, 2009
    19
    0
    Hi im trying to make a program to the following specifications:

    0) initialise a temporary numerical value to 00H, base value to 10H and count value to 00H
    1) Read in a character
    2) Apply series of checks against ‘0’-‘9’ and ‘A’-‘F’ and convert the digit to a numerical value as described above
    3) multiply our temporary numerical value by the base (10H) – remember that mul multiplies the src with what is in AL or AX so we need to make sure that the temporary numerical value is moved into AX before we multiply and since AL contains our digit numerical value we will need to push AX to save it for use later - use version b) of mul i.e. src is 16 bit, result in high word in DX (we will discard this) and low word in AX (which we will use). We can safely multiply by the base the first time round as the temporary numerical value will start out as initilised to 0.
    5) pop AX so we can get the new numerical digit value and add it to the temporary numerical value
    6) increment count of digits converted and go back to 1)

    Note – you will also need to echo the digit characters input, but you should ignore i.e. not echo, the non-hex digit termination character.


    Using the above write the code to input a hex number of up to a word in size as a subroutine called GetHex in Stdio.asm.
    The return numerical value should be in DX and the count of the number of digits converted should be in CX.

    You can use the subroutine Getch to read the characters. You should echo the character typed in using Putch, but since Putch expects the character to be output in DL, you may need to save DX onto the stack before using Putch and pop DX after using Putch. Do not echo the termination character – simply discard it.

    You can check this works by calling GetHex from your main code and then checking the result using debug.




    and well my problem is that im finding it hard to convert the number to hex. I have got the read character , echo character and the if statements to ensure that the character is a hex number valid.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. MOV BL, 10h ; sets the base value to 10h
    4.  
    5.     Number:              
    6.         sub al, 30h
    7.         mov ch,al                
    8.         JGE Continue ;jump to Continue , if true
    9.        
    10.     Letter:
    11.         sub al, 31h
    12.         mul,BL ; bl*AL  so 16*al
    13.         JGE Continue ;jump to Continue , if true    
    14.  
    15.     Continue:
    16.         inc cx ; CX counts the amount of converted
    17.  
    the code above atm gets the decimal value for instance if the character 'A' has been input it would get to the Letter label and then have 31h taking from it (41-31=10) so it converts the character to decimal. if '1' is input it goes to the number label and takes 30h from it (31h-30=1). :) what i am stuck with is then converting the decimal values back into hex because im meant to allow up to 4 hex characters to be input soo i could have '1F2A' how could i convert that to hexadecimal because i know you use powers and stuff to convert dec to hex.. I have been trying for about 5 hours to get this to work i just cant get my head around it . can you guys help me please

    thanks soo much
     
    #1
  2. thatoneguy

    thatoneguy AAC Fanatic!

    Joined:
    Feb 19, 2009
    6,356
    711
    You should mention "PC" in the title, since you have all the BIOS Interrupt functions to work with.

    Doing this without the BIOS would be... interesting.. :)

    P.S. Why didn't you just ask this right away instead of the 0-9 check?
     
    #2
  3. fortune2k

    fortune2k Thread Starter Member

    Joined:
    Mar 18, 2009
    19
    0
    i check if its 0-9 because then i can convert

    im just stuck on this part

    multiply our temporary numerical value by the base (10H) – remember that mul multiplies the src with what is in AL or AX so we need to make sure that the temporary numerical value is moved into AX before we multiply and since AL contains our digit numerical value we will need to push AX to save it for use later - use version b) of mul i.e. src is 16 bit, result in high word in DX (we will discard this) and low word in AX (which we will use). We can safely multiply by the base the first time round as the temporary numerical value will start out as initilised to 0.


    i have no idea how i would do that . anyone have any ideas?
     
    #3
  4. thatoneguy

    thatoneguy AAC Fanatic!

    Joined:
    Feb 19, 2009
    6,356
    711
    Do shift operations for each hex digit?

    AND 0x000F
    x 10
    if overflow, add to next digit,
    repeat....
     
    #4
Loading...