Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does AAA work in 8086 instruction set?

Tags:

assembly

masm

There is some info regarding the algorithm how the instruction works:

if low nibble of AL > 9 or AF = 1 then:
    AL = AL + 6
    AH = AH + 1
    AF = 1
    CF = 1
else
    AF = 0
    CF = 0
in both cases:
    clear the high nibble of AL. 

Example:
  MOV AX, 15   ; AH = 00, AL = 0Fh
  AAA          ; AH = 01, AL = 05
  RET

But the problem i am facing is when I replace 15 in the above example with numbers like 00FF and 00FA the value in AH gets incremented by 02 instead of 01 !!

Why are these changes ??

like image 303
viswas Avatar asked Sep 22 '13 15:09

viswas


1 Answers

Here is the detailed explanation of DAA and AAA :

The aaa (ASCII adjust after addition) and daa (decimal adjust for addition) instructions support BCD arithmetic. BCD values are decimal integer coded in binary form with one decimal digit (0..9) per nibble. ASCII (numeric) values contain a single decimal digit per byte, the H.O. nibble of the byte should contain zero.

The aaa and daa instructions modify the result of a binary addition to correct it for ASCII or decimal arithmetic. For example, to add two BCD values, you would add them as though they were binary numbers and then execute the daa instruction afterwards to correct the results. Likewise, you can use the aaa instruction to adjust the result of an ASCII addition after executing an add instruction. Please note that these two instructions assume that the add operands were proper decimal or ASCII values. If you add binary (non-decimal or non-ASCII) values together and try to adjust them with these instructions, you will not produce correct results.

The choice of the name "ASCII arithmetic" is unfortunate, since these values are not true ASCII characters. A name like "unpacked BCD" would be more appropriate. However, Intel uses the name ASCII, so this text will do so as well to avoid confusion. However, you will often hear the term "unpacked BCD" to describe this data type.

Aaa (which you generally execute after an add, adc, or xadd instruction) checks the value in al for BCD overflow. It works according to the following basic algorithm:

if ( (al and 0Fh) > 9 or (AuxC =1) ) then

    if (8088 or 8086) then 
            al := al + 6
    else 
            ax := ax + 6
    endif

    ah := ah + 1
    AuxC := 1               ;Set auxilliary carry
    Carry := 1              ; and carry flags.

else

    AuxC := 0               ;Clear auxilliary carry
    Carry := 0              ; and carry flags.
endif
al := al and 0Fh

The aaa instruction is mainly useful for adding strings of digits where there is exactly one decimal digit per byte in a string of numbers. This text will not deal with BCD or ASCII numeric strings, so you can safely ignore this instruction for now. Of course, you can use the aaa instruction any time you need to use the algorithm above, but that would probably be a rare situation.

The daa instruction functions like aaa except it handles packed BCD (binary code decimal) values rather than the one digit per byte unpacked values aaa handles. As for aaa, daa's main purpose is to add strings of BCD digits (with two digits per byte). The algorithm for daa is

if ( (AL and 0Fh) > 9 or (AuxC = 1)) then

    al := al + 6
    AuxC := 1               ;Set Auxilliary carry.

endif
if ( (al > 9Fh) or (Carry = 1)) then

    al := al + 60h
    Carry := 1;             ;Set carry flag.

endif

So AAA works only in case of one decimal digit per byte which is not the case you mentioned above.

like image 173
humpty Avatar answered Sep 30 '22 08:09

humpty