I am using Eclipse IDE to develop CPP code for Arduino MEGA2560 target.I checked the 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:939361 !!! 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:
__SP_H__ = 0x3e__SP_L__ = 0x3d__SREG__ = 0x3f__RAMPZ__ = 0x3b__tmp_reg__ = 0__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)!
The compiler details: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
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?