Assume we use 3 bits to subtract 1 from 0:
0b000
- 0b001
------
0b111
I have the following 2 questions:
1- Where is the borrowed 1 into the MSB coming from? When we do 0b000 - 0b001
, the MSB of the first operand is a 0 so there is nothing to borrow from, hence in this case the carry flags get set and a 1
gets borrowed into the MSB of the first operand (which makes the output negative). Where is the 1 coming from? it just comes form thin air and a flag gets set? is it a math rule just to "borrow" a 1 in this case and make the output negative?
There is this exact same question I found but the selected answer doesn't really answer the question: Subtracting 1 from 0 in 8 bit binary
2- I've read there are two ways to do subtraction in hardware. Either turn a - b
into a + (-b)
and use the adders, or use the borrow method.
This answer talks about using the adders: https://stackoverflow.com/a/5793952
This answers talks about an actual subtractor
being used in x86 as flags needs to be set properly and the previous method won't work: https://stackoverflow.com/a/62219965/14940626
So which one is it? does x86 use the latter and MIPS (as an example) uses the former method?