Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADC instruction in ASM 8086

When I use ADC for exmaple:

AL = 01 and BL = 02, and CF = 1

when I make this:

ADC AL,BL 

Will AL be 3 or 4? (with the CF addition or without?)

like image 450
Tal Avatar asked Apr 22 '10 04:04

Tal


2 Answers

Few things about the 8086 ADC instruction:

Syntax: adc dest, src
dest: memory or register
src:  memory, register, or immediate
Action: dest = dest + src + CF

Clearly the action says the Carry Flag (CF) will be included in the addition so the result will be 4 not 3.

like image 81
codaddict Avatar answered Oct 29 '22 06:10

codaddict


It is no different than adding in base 10.

 99
+11

9+1 is zero carry the 1
9+1+carry is 1 carry the 1

The result of the above decimal math is 10 with a carry of 1, or 110 if you want to think of it that way.

For binary start with a one bit adder, here is a truth table:

000 0 0
001 0 1
010 0 1
011 1 0
100 0 1
101 1 0
110 1 0
111 1 1

the left column of three bits are the input combinations, two operands and carry in, the second column is carry out and the third column is the result

so 1+1 with no carry is 110 in the left column and the result is 0 carry the 1.

Not any different than the decimal math above just much simpler, when you add up a column in decimal, operand a, operand b, carry. The result is the answer modulo 10 and the carry is the result/10. copy the carry to the top of the next column and repeat for ever. as demonstrated with 99+11 or 999+111, etc.

For the simpler two bit add with no carry the result is the xor of the inputs and the carry out is the and of the two inputs. You could implement add with carry using two add without carry adders chained or do it directly. The result is set when there are an odd number of onces or odd parity, which is two xors r = a xor b xor carry in. The carry out I am struggling with at the moment perhaps someone can help.

so an 8 bit 0xFF + 0xFF with carry set will give

        1
 11111111
+11111111 

This shows 0xff + 0xff with a "carry the one" coming in before you start.

look at it one column at a time from the right just like decimal math

1+1+1 = 1 carry the 1
next column
1+1+1 = 1 carry the 1
...

this continues and you end up with 0xFF with the carry bit set

So if you had only an 8 bit add with carry you could add up two numbers as wide as you have memory.

Lets look at a 16 bit add:

 0x1234
+0xABCD

You could just do the math with a 16 bit add, 0xBE01.

or with an 8 bit adder:

clear the carry bit
add with carry 0x34+0xCD result 0x01 carry set
add with carry 0x12+0xAB result 0xBE carry clear

so the answer is 0xBE01

Or using a 4 bit adder if all you have is a 4 bit alu

clear the carry bit
add with carry 0x4+0xD = 0x1 carry bit set
add with carry 0x3+0xC = 0x0 carry bit set
add with carry 0x2+0xB = 0xE carry bit clear
add with carry 0x1+0xA = 0xB carry bit clear

again the result 0xBE01 carry bit clear

we could do this with single bits as well or a 3 bit adder, so long as it is binary it is trivial.

All useful processors must have some way to add the carry bit so that you can widen the alu. Sometimes there are separate add and adc, some the adc is an extra step or the most painful would be an add without carry and use a branch if carry clear with an add immediate under it.

This is also why shifts or rotates rotate through the carry bit, so you can do a bit shift wider than the width of a register/memory location.

binary multiplication is painfully simple compared to decimal, but I will spare you that one and let you think about it.

Yes, you could have and should have written a program to try this out. And still can, I could be intentionally leading you down a path of misinformation.

like image 43
old_timer Avatar answered Oct 29 '22 04:10

old_timer