# Example of subtraction with borrow?

Could someone post a snippet of assembly language code showing how subtraction with borrow works? I am trying to do 12 bit math on a
4 bit processor, but any example such as doing 16 bit math on an 8 bit processor will do. I don't care which processor either; I am just trying to find an example of someone using the carry bit as a borrow bit so I can work things out in my mind. The addition with carry makes perfect sense, but I don't "get it" when it comes down to doing subtraction with borrowing. An example would help a lot.
Thanks!
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
From: <http://www.8052.com/subb16.phtml
SUBB16_16: ;Step 1 of the process MOV A,R7 ;Move the low-byte into the accumulator CLR C ;Always clear carry before first subtraction SUBB A,R5 ;Subtract the second low-byte from the accumulator MOV R3,A ;Move the answer to the low-byte of the result
;Step 2 of the process MOV A,R6 ;Move the high-byte into the accumulator SUBB A,R4 ;Subtract the second high-byte from the accumulator MOV R2,A ;Move the answer to the low-byte of the result
;Return - answer now resides in R2, and R3. RET Calling sequence:
;Load the first value into R6 and R7 MOV R6,#22h MOV R7,#0DBh
;Load the first value into R4 and R5 MOV R4,#1Ah MOV R5,#0F9h
;Call the 16-bit subtraction routine LCALL SUBB16_16
--
- Alan Kilian <alank(at)timelogic.com>
Director of Bioinformatics, TimeLogic Corporation 763-449-7622
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
On Sun, 29 Feb 2004 07:16:11 -0800, Puzzled wrote:

Computers don't subtract (apart from the ones that don't add, anyway ;o).
Subtraction is performed by adding the negated value of the subtrahend, so: a - b == a + (-b)
To negate an integer (assuming a two's-complement representation), just invert all bits and add 1.
An example:
37 - 18 = 100101 - 010010 = 100101 + (101101 + 1) = 100101 + 101110 = 010011 = 19
--
Max

<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Max wrote:

and you implement that add 1 by having the carry set. For subtraction, that makes the carry bit a 'not borrow' bit for subtraction. Thus the only difference between subtraction and addition is the bit complement of the subtrahend, and either setting (subtract) or resetting (add) the carry for the very first of an extended add/subtract operation. I'm sure it is now crystal clear :-) but work out some examples for yourself.
This is fouled up where the chip designers have decided to invert it's purpose, as in the X86 processors.
--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
[...]

The important thing is to realize that there are two different philosophies for setting the carry bit after subtraction.
Z80, x86, and SPARC treat the carry flag as a "borrow flag" when doing a subtraction. If the subtrahend is greater than the minuend (treating both operands as unsigned), the carry flag is set because there is a borrow. A "branch if less unsigned" after a comparison instruction is equivalent to "branch if carry set" in this case.
On other processor families, such as ARM and PowerPC, the carry flag after a subtraction is set to the adder carry output after computing (~b + a + 1). Therefore, if the subtrahend is less or equal to the minuend (treating both operands as unsigned), the carry flag is set. "branch if less unsigned" after a comparison instruction is equal to "branch if carry clear" in this case.
-- Norbert
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
On Mon, 01 Mar 2004 02:50:13 GMT, Norbert Juffa wrote:

And that's only on two's-complement machines. The old Univac 1100 series mainframes used one's-complement coding (so it had +0 and -0), and a hardware subtractor that required an "end-around-borrow"!
--
Max

<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

And no carry status. Multi-precision arithmetic required some real coding gymnastics!
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
wrote:

Typically you set the "carry" bit for the first byte/nibble and then subtract. The carry is set low if there is a borrow.
multiple word subtraction in pseudo code:
sub:     set    carry    ; if processor has no subtract without borrow     load    A0    ; minuend first word (least significant)     subb    B0    ; subtract subtrahend first word with borrow     store     R0    ; result first word (least significant)     load    A1     subb    B1     store    R0 ... load     An     subb    Bn     store    rn     ; result last word (most significant)
In fact, what's happening is that the processor is adding on the 1's complement of the subtrahend to the minuend, plus the carry.
eg. 4-bit nibble subtraction of 8 bit 0x0A (10) from 0x02 (2)
set    carry     load    A0    ; 2     subb    B0    ; add ~0xA = 5 + carry     store    R0    ; result is 8 with no carry (=> borrow)     load    A1    ; 0     subb    B1    ; add ~0 = F (no carry)     store     R1    ; F (no carry)
Result: F8 = -8.
Best regards, Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
snipped-for-privacy@interlog.com Info for manufacturers: http://www.trexon.com
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

## Site Timeline

• ### Servo drive signals for HP 7978 tape drive controller

• - last updated thread in General Robotics Forum
• ### OT Nuclear U-Boats; how do they condense the steam?

• - the site's last updated thread. Posted in ⏚ Electrical Engineering

Polytechforum.com is a website by engineers for engineers. It is not affiliated with any of manufacturers or vendors discussed here. All logos and trade names are the property of their respective owners.