assemblyarduinoavr

data segment of AVR store nonsense after compiling


So i'm trying to change the first element of an array that i'm storing in sram and i checked if the data after compile is equal to the value of pre-initialaztion and it wasn't equal. so this my code:

.data
.org  0x0100
system_log: .space 20, 0x44
            .byte 10,13,0

.text
.org 0x4000

loop:
  rjmp loop

print_buff:
    
    LDI   XL, lo8(system_log)
    LDI   XH, hi8(system_log)         ;Z points to string message
    ld r0, X
    mov r18, r0
    rjmp print_msg

I'm printing the first value in array and i expect it to be equal to D but it gives me an A

I don't know what is going on but here we go and by the way i'm using Arduino with Atmega328P chip


Solution

  • LDI XH, hi8(system_log) ;Z points to string message

    That code is wrong. You are loading the X register, not the Z register which you are using in print_msg as of your other question Updating an element in array in AVR Assembly

    Moreover, in that print_msg, you are using LPM as that function reads from flash memory, wherea in order to read from RAM you must use LD or LDD. What you need is

    Plus, the .data section has to be initialized at startup. As you are using the GNU tools, you can just pull in the code from libgcc by means of

    .global __do_copy_data
    

    Or, when you are using something like -nostdlib -nodefaultlibs -nostartfiles, you'll have to do it by hand.