https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95478

            Bug ID: 95478
           Summary: CPP stack pointer SP is 0x5d but assembly shows
                    __SP_L_ at 0x3d (target AVR MEGA2560)
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dov at keyscan dot com
  Target Milestone: ---

I am using Eclipse IDE to develop CPP code for Arduino MEGA2560 target.

The compiler details as appeared on the compiler output:
GNU C++14 (GCC) version 7.3.0 (avr)
compiled by GNU C version 4.8.2, GMP version 5.0.2, MPFR version 3.1.0, MPC
version 0.9, isl version none

The problem:
I checked the stack pointer SP in a CPP program compiled for Arduino Mega2560
target.
The SP address is 0x5D which is correct.
I inserted assembly line into the CPP program to check the address of the
__SP_L__ which I expected also to be 0x5D but found out that it is 0x3D which
is wrong for the Mega2560

This is the code that I ran:

```
    Serial.println( (unsigned int)&SP );
    Serial.println( (unsigned int)&SPL );
    asm("ldi r20,lo8(10)\n ldi r21,0\n ldi r22,__SP_L__\n          
    ldi r23,0\n ldi r24,lo8(Serial)\n ldi r25,hi8(Serial)\n 
    call _ZN5Print7printlnEji\n");
```

The above asm(...) line simply implements the call to Serial.println(...) line
with the assembler definition for the stack register.

The output is:
93
93
61   !!! the address of the stack register in the assembler
         is wrong  it should be 93 (decimal)

I check the assembly produced by the CPP compiler and found out that each file
includes at the top the following lines:

2 __SP_H__ = 0x3e
3 __SP_L__ = 0x3d
4 __SREG__ = 0x3f
5 __RAMPZ__ = 0x3b
6 __tmp_reg__ = 0
7 __zero_reg__ = 1

Which is wrong for the ATMega2560 , the stack pointer address should be 0x5D
and not 0x3D

The compiler command line contain the directives:
       " -mmcu=atmega2560 -DF_CPU=16000000UL "
So the GCC compliler knows the correct target CPU and indeed use the correct
address for the SP stack pointer register on the CPP level but the wrong
address on the asm level  (the __SP_L__)!

Am I missing something? 
It seems that the stack pointer is managed in the asm through the wrong
registers address, __SP_L__ = 0x3D while the push & pop cpu address is SP =
0x5D which is the correct address for the stack pointer register on the target
processor STMEGA2560

Any one know this problem?
Any solutions?

Reply via email to