Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Decimal to Hex

First off, this is homework.

I'm trying to read a 5 digit number into the register bx. The number is assumed to be no greater than 65535 (16 bits). Below is how I am attempting to do so.

However, when I attempt to print the number, I am only printing the very last digit that was entered. Which leads me to guess that when I add another number to bx it is overwriting the previous number, but I am unable to see the problem. Any help would be appreciated, I'm almost certain that it is something small I'm overlooking :-/

mov cx,0x05 ; loop 5 times
    mov bx,0    ; clear the register we are going to store our result in
    mov dx,10   ; set our divisor to 10

read:
    mov ah,0x01     ; read a character function
    int 0x21        ; store the character in al
    sub al,0x30     ; convert ascii number to its decimal equivalent
    and ax,0x000F   ; set higher bits of ax to 0, so we are left with the decimal
    push ax         ; store the number on the stack, this is the single digit that was typed
    ; at this point we have read the char, converted it to decimal, and pushed it onto the stack
    mov ax,bx       ; move our total into ax
    mul dx          ; multiply our total by 10, to shift it right 1
    pop bx          ; pop our single digit into bx
    add bx,ax       ; add our total to bx
    loop read       ; read another char
like image 601
Adam Avatar asked Apr 03 '11 23:04

Adam


People also ask

What is FFFF hex in decimal?

My book says the hexadecimal notation FFFF equals 65535 in decimal value.

How are decimals represented in hex?

The first nine numbers (0 to 9) are the same ones commonly used in the decimal system. The next six two-digit numbers (10 to 15) are represented by the letters A through F. This is how the hex system uses the numbers from 0 to 9 and the capital letters A to F to represent the equivalent decimal number.


1 Answers

When using the MUL opcode, there are three different results:

  • 8 bit - results are stored in ax
  • 16 bit - results are stored in dx:ax
  • 32 bit - results are stored in edx:eax

So when you perform your multiplication, the instruction overwrites dx with zero in your case. This means that each subsequent use of the mul opcode is multiplying by zero.

like image 73
Sparafusile Avatar answered Oct 08 '22 18:10

Sparafusile