#25 PIC16 genAddLit and genPlus


The attached (well, downloadable from the SF
website...) patch fixes several errors in genAddLit and

I left some DEBUGpic16_emitcodes in there to document
the changes. If you want to I can also post a version
without these comments...

Major fixes are:
genAddLit:526: no default case for adding 0x01LL to an int,
res = res + 0x0144 fails.

genAddLit:750: fixed destination operand and add logic

genPlus:1050: an uninitialized high byte was added to,
I have it cleared now before use

Additionally I changed some complicated Carry-bit
handling to the easier way using ADDWFC.

Apply patch with -p0 in src/pic16 (created using -cp
this time:-)).

Below is my listing of a test program that shows most
(all?) of the problems mentioned above - maybe it helps.

Raphael Neider


#include <pic18fregs.h>
#pragma stack 0x300 0x100

#define CHECK(v1, v2, code) if ((v1) != (v2)) {
PORTA=((v1)>>8); PORTB=((v1)&0x0FF); PORTC=((v2)>>8);
PORTD=((v2)&0x0FF); PORTE=code; }
#define CHECKL(v1, v2, code) if ((v1) != (v2)) {
PORTA=((v1)>>24); PORTB=((v1)>>16); PORTC=((v1)>>8);
PORTD=((v1)&0x0FF); PORTE=code; }

int main ()
volatile int res, val1;
volatile long lres, lval;

PORTE = 0x00;

// at 523ff (add lit 0x01LL)
val1 = 0x1234;
val1 = val1 + 0x0134;
CHECK(val1, 0x1234 + 0x0134, 1);

val1 = 0x1234;
val1 = val1 + 0x01EF;
CHECK(val1, 0x1234 + 0x01EF, 2);

// at 562ff (add lit 0xHH01)
val1 = 0x1234;
val1 = val1 + 0x1201;
CHECK(val1, 0x1234 + 0x1201, 3);

val1 = 0x12FF;
res = val1 + 0x1201;
CHECK(res, 0x12FF + 0x1201, 4);

// at 577ff (add lit 0xHHLL)
val1 = 0x1234;
val1 = val1 + 0x1266;
CHECK(val1, 0x1234 + 0x1266, 5);

val1 = 0x1234;
val1 = val1 + 0x12D9;
CHECK(val1, 0x1234 + 0x12D9, 6);

lval = 0x12345678L;
lval = lval + 0x45008900L;
CHECKL(lval, 0x12345678L + 0x45008900L, 0x10);

lval = 0x12345678L;
lval = lval + 0x45FF89FFL;
CHECKL(lval, 0x12345678L + 0x45FF89FFL, 0x11);

lval = 0x12345678L;
lres = lval + 0x45008900L;
CHECKL(lres, 0x12345678L + 0x45008900L, 0x12);

lval = 0x12345678L;
lres = lval + 0x45FF89FFL;
CHECKL(lres, 0x12345678L + 0x45FF89FFL, 0x13);

return 0;


  • Raphael Neider

    Raphael Neider - 2004-09-24

    The patch?

  • Vangelis Rokas

    Vangelis Rokas - 2004-10-01
    • assigned_to: nobody --> vrokas
    • status: open --> closed-accepted
  • Vangelis Rokas

    Vangelis Rokas - 2004-10-01

    Logged In: YES

    Patch applied successfully.
    SDCC v.2.4.5 #851


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks