Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about ADD on ASM 8086

Tags:

x86

assembly

add

I'm studying ASM 8086 theoretically on highschool. (that means that I study ASM 8086 on a notebook, and never got to run it over a computer).

And I don't understand - what will happen if I do this:

MOV AL, F2h
ADD AL, 20h

What will the computer do? (what will be the value of AL,AX, CF,ZF?)

and what will happen if I do this:

MOV AH,F2h
ADD AH,20h

Thank you !!

like image 414
Tal Avatar asked May 05 '10 09:05

Tal


2 Answers

MOV AL, F2h

Place the value 0xF2 in the AL (accumulator) register.

ADD AL, 20h

Adds the value 0x20 to the value contained in the AL register.

AL will be 0xF2 + 0x20. But AL is an 8 bits register, so the value will be 0x12, and not 0x112.

Same thing for AH, as it's also an 8 bits register.
To get the complete value, you will need to use the AX register, which is 16 bits.
AX is composed by AH (high) and AL (low), so you can access the high and low parts individually.

----------------EAX ----------------
                 ------- AX --------
|----------------|--------|--------|
|                |   AH   |   AL   |
|----------------|--------|--------|
     16 bits       8 bits   8 bits
like image 187
Macmade Avatar answered Oct 14 '22 06:10

Macmade


I would also recommend using D86 (which comes with A86) as it lets you type in 8086 instructions interactively so you can see what happens to all the registers and flags after each instruction.

This code (as other have pointed out):

MOV AL, F2h
ADD AL, 20h

will only affect the flags and the AL register. No other eight-bit register will be affected (even AH). AX will change though since it is made up of AH and AL, so if AH was 42h:

Code         AL   AH     AX
MOV AL,F2h   F2   42    42f2
ADD AL,20h   12   42    4212

The result of that particular operation will set the carry flag and the parity flag and clear the overflow, zero, sign and auxillary carry flags.

You may think that the overflow flag should be set, but the overflow flag treats the values as signed values (in this case -14 and 32) and the addition doesn't exceed the maximum signed value (127). The carry flag treats the values as unsigned values (242 and 32) and the addition exceeds the maximum unsigned value: 242 + 32 = 274 which is greater than 255 so the carry is set.

like image 39
Skizz Avatar answered Oct 14 '22 08:10

Skizz