Optimizing code for one off variable

Hi,

Taking the code snippet below,

char* ptr  = (char*)0x0000;
short size = 0x100;
short sum  = 0x00;

do {
   sum += *ptr++;
} while (size--)

This will cause C51 to allocate "ptr" and "size" in RAM.

        MOV     R3,#00H
        MOV     R2,#00H
        MOV     R1,#00H
        MOV     DPTR,#pre?1251
        MOVX    @DPTR,PR0

        MOV     DPTR,#size?1252
        MOV     A,#01H
        MOVX    @DPTR,A
        INC     DPTR
        CLR     A
        MOVX    @DPTR,A

and everytime "size" is decremented, or "ptr" is incremented, the values are loaded back into the RAM.

Is there a way to force C51 to not use RAM but use the registers only as "ptr" and "size" will never be used anymore after that. What I mean is, is there a way to force C51 to compile to something like this,

mov dptr, #WORD0(00h)
mov r4, #01h ; r4:r5 = size
mov r5, #00h
mov r6, #00h ; r6:r7 = sum
mov r7, #00h

loop:
movx A, @dptr+#000h
addw 006h, A ; r6:r7 += A

inc  dptr
subw 004h, 001h ; r4:r5--
jnc  loop

; add code to store r6:r7 into RAM

Any advice would be greatly appreciated. Thanks in advance.

Parents
  • This will cause C51 to allocate "ptr" and "size" in RAM.

    No, it won't. Not unless you avoid turning on optimization or use a horribly wrong memory model.

    addw 006h, A ; r6:r7 += A
    

    Surely you're aware that no machine instruction like 'addw' exists in an 8051, right? And that, the 8051 being a so-called accumulator-based design, the target of all arithmetic machine instruction is the accumulator, not some memory address?

    Oh, and if you're looking for efficient code, why are you using generic pointers?

Reply
  • This will cause C51 to allocate "ptr" and "size" in RAM.

    No, it won't. Not unless you avoid turning on optimization or use a horribly wrong memory model.

    addw 006h, A ; r6:r7 += A
    

    Surely you're aware that no machine instruction like 'addw' exists in an 8051, right? And that, the 8051 being a so-called accumulator-based design, the target of all arithmetic machine instruction is the accumulator, not some memory address?

    Oh, and if you're looking for efficient code, why are you using generic pointers?

Children
More questions in this forum