0

How can I add 2 numbers that their value is on base 16 and make the result on "base 10" on assembler. For example:

"5h+5h=10h" - I know it's wrong, I just want it to be visually 10h

And not:

5h+5h=Ah

CODE:

MOV AX,5h
MOV BX,5h
ADD AX,BX

result: ax=Ah - Not the result that i want...

result: ax=10h - The result that i want.

I tried to figure it out with google but didn't find anything that can help me...

BananaBuisness
  • 339
  • 2
  • 18
  • 2
    Check out the DAA instruction http://x86.renejeschke.de/html/file_module_x86_id_69.html – BitBank Mar 04 '15 at 10:59
  • 1
    Sounds like you're talking about BCD (binary coded decimal) Arithmetic. You may find [this answer](http://stackoverflow.com/questions/2359527/assembler-why-bcd-exists) useful. – Component 10 Mar 04 '15 at 10:59
  • The addition operation is base-agnostic. This is a matter of representation, i.e. how to convert a string from/to a number – Niklas B. Mar 04 '15 at 11:09
  • What's the point? How would 5h+5h yield 10h? If that's some magic arithmetic you'll have to craft it yourself? – sharptooth Mar 04 '15 at 11:26
  • Can you please give me a link for Assembler code that do that practicly? – BananaBuisness Mar 04 '15 at 11:26
  • Look i did " "5h+5h=10h" " that mean that I just want it visualy do 10h and not 0Ah – BananaBuisness Mar 04 '15 at 11:31
  • the question really is do you want `BCD` or `hex to dec` conversion? what is the range of input operands? what about sign? the output should be hex number which is printed the same as decadic result would be (and the input values also ...)? how many digits ? no decimals I hope .... – Spektre Mar 04 '15 at 11:50
  • Well I'm building a calculator and it take number with max length of 5 chars in num... – BananaBuisness Mar 04 '15 at 19:19

2 Answers2

4

Here is the code you are looking for

MOV AX,5h
MOV BX,5h
ADD AX,BX
DAA

Now AX contains 10h

Fifoernik
  • 9,779
  • 1
  • 21
  • 27
1

Ok so i figure it out with @Fifoernik 's help so the problem is that if i want to do it with 16bit (for example 99h+1h) values i need to do it like this using DAA operan and CF flag

    pop ax
    pop bx
    add al,bl
    daa ; dec id values
    mov cl,al
    mov al,ah
    jc Carry; do i have carry 
    add al,bh
    daa ; do the magic thing
    JMP finito
    Carry:
    add al,1 ; add the carring...
    add al,bh
    daa ;can some one tell me what exactly daa does?
    finito:
    mov ch,al
    push cx
    ret

daa working only on AL so you'ill need to use the carry flag to add the carry like:

AH AL
 1 <----- carry
00 99 <-- DAA take care of the 99 and make it 0 when its A0h
00 01+
-- --
01 00 ---> result 100h
BananaBuisness
  • 339
  • 2
  • 18
  • Please note that the `ret` at the tenth line should be replaced with a `jmp` towards the 3 last lines! Now you do not always return the desired result in CX and on the stack. – Fifoernik Mar 05 '15 at 11:50