2
\$\begingroup\$

As is well known, the Microchip PIC parts implement subtraction by the "2’s complement method", generating a carry bit when the sum of (the first parameter, and the two's complement of the other parameter) generates a carry.

So mostly, if the answer is a "positive number", there is a carry bit. And you get a carry bit from

movlw 33
sublw 33

because 33 + 223 = 256.

What do you get if you subtract zero from zero?

movlw 0
sublw 0 '0 + 0 = 0, suggesting c=0.
 'unless someone is right, in which case c=1

Two's complement of zero is zero, add zero is zero, carry is zero. But the code I'm seeing, and some advice I'm receiving, is otherwise. This is very confusing for me.

And if you subtract zero from anything?

movlw 0
sublw 33 '33 + 0 = 33, suggesting c=0
 'unless someone else is right, in which case c=1

These cases aren't demonstrated in the documentation I have.

Is this a "special" Microchip implementation of 2's complement subtraction? Or is this normal? Please point me to any general documentation of the math method, and/or any Microchip documentation.

ps: PIC16C parts are just like PIC16F parts - only the hardware is different

asked Jul 26, 2017 at 3:21
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

The PIC sublw instruction subtracts the contents of the W register from the 8-bit literal parameter, which is a bit different from the subtract instructions in many other architectures.

movlw 0
sublw 0 

Means load W with 0x00. Subtract that from 0x00.

Subtraction is by complementing the W register and adding 1 (2's complement), and adding to the literal.

So you have 0xFF + 1 + 0x00 = 0x00 (C set)

movlw 0x00
sublw 0x33

is 0xFF + 1 + 0x33 = 0x33 (C set)

In general, the C bit (really a borrow rather than carry for subtraction) is set when the result is positive (including zero), as is normal in 2's complement subtraction.

For more, read the instruction descriptions in the Midrange Reference Manual. The instruction set summaries in the datasheets are just brief reminders.

answered Jul 26, 2017 at 3:41
\$\endgroup\$
3
  • \$\begingroup\$ The instruction descriptions in Section 29.5 of the Midrange Reference Manual do NOT give examples for w=0, which is why I asked. And I don't see anywhere a description of the algorithm. It would be nice. Your example (which is nice), does not seem to match the description "2's complement method", because you don't take the 2's complement: you have the ones complement, then you do a 3 parameter addition, which gives a different answer. Is there anyone other than Microchip that thinks that's a "2's complement method"? \$\endgroup\$ Commented Jul 26, 2017 at 4:01
  • 1
    \$\begingroup\$ @david See: en.wikipedia.org/wiki/Two%27s_complement and also en.wikipedia.org/wiki/Method_of_complements . It is documented. I've known about it since perhaps grade school. But, for computer usage in ALUs, certainly by the time I was in high school using Bell Lab's "Cardiac" (paper) computer. It's certainly a normal 2's complement method for subtraction and used by most 8-bit ALUs I've had my hands on during the last 4 decades. \$\endgroup\$ Commented Jul 26, 2017 at 5:25
  • \$\begingroup\$ @david This is completely standard, as jonk says. The only thing unusual is that sublw subtracts w from the literal. Maybe they (Microchip, as the General Instrument PIC1650 did not have this instruction) should have called it subwl. \$\endgroup\$ Commented Jul 26, 2017 at 18:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.