Hi Kevin

I would recommend you to search and study the instruction set of a

microprocessor

with the support you're trying to implement in your processor.

Many processors have two different add instruccions (with and without carry)

to support large integer arithmetic. The simplest I know of is the 8031

8-bit microcontroller

from Intel, Infineon, Dallas and many other manufacturers.

The first hit I found in google points to the page

http://www.rehn.org/YAM51/51set/instruction.shtml

It contains the description and some numeric examples for arithmetic

operations.

Of interest are:

ADD (A =A +x, carry is not used)

ADDC (A=A+x+carry)

SUBB (A = A-x-carry)

> > As your numbers are 32-bit wide, and overflow condition is a flag

> > about the result of the _whole_ v := v + i operation, it can be

determined

> > by the MSB bits only (that is, from columns 31 and 30 of the addition).

> Thank you! I think I used the wrong word "underflow". What I mean is:

> if "i" is negative, it might be that abs(lo(i)) is greater than lo(v).

> What happens then? An example:

> v = 1234 0100 hex

> i = 0000 0101 hex

> The operation for the lo bits will result in FFFF and the carry will

> be set. Then when I add the high bits, it will be 1235 when it should

> actually be 1233. How do I save that the first operation was not an

No.

I assume you're substracting the numbers, as i is positive and the

result you mention is v-i. Then, take into account that substraction

is performed by an adder in 2's complement arithmetic as follows:

v - i = v + 2's complement(i) = v + not(i) + 1

So v-i operation is converted to 12340100 + FFFFFEFE + 1 = 1233FFFF

The low part is 0100 + FEFF = FFFF,

the carry from the low part is _not_ set, so the high part is 1234 + FFFF =

1233

Quote:> overflow but a borrow? (I don't know how to call this. That's what I

> mistakenly called underflow).

Go to the mentioned page, you'll see the different descriptions for carry

(or borrow)

and the overflow.

Carry/borrow is the cy-16 out of the add operation (remember there's no

substraction

circuit). It is the overflow flag if and only if you're using unsigned

arithmetic

Overflow for signed arithmetic is cy-16 xor cy-15. When this xor gives you 1

means you've obtained a positive result adding two negative numbers, or a

negative result

adding two positives.

> > After the high halfword add, you compare the carry out to the carry out

of

> > the sign bit to the carry in of the sign bit. If they are different

then it

> > is overflow or underflow. The value of such bit tells you which one.

> So does that mean I have to modify my architecture and set TWO flags?

> A carry flag and a negative flag (if sign of last operation was

> negative), and then the Add-With-Carry instruction would look at both?

Never look at both.

Your ALU must provide two flags, carry and overflow, and two different add

instructions

x+y and x+y+carry. Of course, every add must update both flags.

If you also provide set-carry/clear-carry instructions, the pseudocode of

the 32-bit operations would be

for 32-bit additions:

add x-low, y-low

addc x-high, y-high

for 32-bit substractions:

set carry

addc x-low, not(y-low)

addc x-high, not(y-high)

When the whole operation is finished, check carry/borrow if the 32-bit

numbers are unsigned,

or the overflow flag if the 32-bit numbers are signed. But no both.

Don't check the flags after the low part.

Best regards

Francisco