# subtraction in 8086

Discussion in 'Programmer's Corner' started by cssc, Nov 6, 2014.

1. ### cssc Thread Starter New Member

Oct 19, 2014
26
1
if i subtract 7986h from 0976h in a calculator, i got the answer as ffff8ff0
but when i did it in 8086 program, my answer was only 8ff0 (ofcourse with carry and sign flag set)
where can i find the upper 4 bits of the actual answer?

2. ### MrChips Moderator

Oct 2, 2009
12,658
3,461
The 8086 registers are 16 bits long. That's all you get.

3. ### cssc Thread Starter New Member

Oct 19, 2014
26
1
so, can't i get the higher bits in some other register,as in case of multiplication.?

4. ### MrChips Moderator

Oct 2, 2009
12,658
3,461
The answer 8FF0h is the correct answer. The MSB says that the answer is a negative number.

5. ### Papabravo Expert

Feb 24, 2006
10,340
1,850
You can do multiword subtraction but not in a single operation. The result depends on how you set the problem up.
Code (Text):
1.
2. 0976h --> 0000 0976h
3. 7986h --> 0000 7986h
4. The first subtraction produces 8ff0h as you have already observed
5. The second operation is a subtract with borrow
6. so 0 - 0 - borrow = ffffh
7. and those two parts produce the 32-bit result
8.
See the description of sbb
http://ece425web.groups.et.byu.net/stable/labs/8086InstructionSet.html#sbb

Apr 28, 2012
3,578
463
use a 80386?

7. ### WBahn Moderator

Mar 31, 2012
18,096
4,920
But then the next question would be: Isn't a "better" answer ffffffff8ff0h, so what register can I find the next 16 bits in? And the 16 beyond that?

When doing addition and subtraction with N-bit operands, you need at most N+1 bits to fully represent the result. You get this additional bit of information via the carry bit, so it is pointless to expend additional silicon to put one bit of information into an N-bit register. But with multiplication and division, you need 2N bits to fully represent the result, so here is DOES make sense to use a second N-bit register (not to mention the fact that a lot of silicon was added to support the operations to begin with).

panic mode likes this.
8. ### MrChips Moderator

Oct 2, 2009
12,658
3,461
This problem will exist regardless of the number of bits available in the register. Before you go performing arithmetic with fixed-sized registers you need to study the limits of the register.

What is the range of values that can be represented in an n-bit register?

What are the consequences when a math operation such as ADD, SUBTRACT and MULTIPLY exceed the range of the register?

Sep 20, 2014
52
1
Its been a while and I no nothing in comparison to chips but i believe when you exceed it is an overflow and so that changes your bits completely you now are starting back at the bottom of that register.

Sep 20, 2014
52
1
Whats embarrassing to me is I converted your hex values cssc to decimal and subtracted them in decimal getting (28688) in base 10 then converted back to base 16 and found that I got a much different value than you did, your "ffff8ff0".

11. ### WBahn Moderator

Mar 31, 2012
18,096
4,920
It would really help if you showed your steps, because what little information you do give makes no sense.

7986h is a large positive number relative to 0976h, which is a smaller positive number. Subtracting 7986h from 0976h therefore results in a negative number.

7986h = 31110
0976h = 2422

2422 - 31110 = -28688

-28688 = 8ff0h

If you got 7010h, it's because you forgot the most significant bit of all -- the sign bit!