This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Cant intialise data value from data area readwrite

I have just started writing assembly code. I am using the simulator in Keil uvision5 to the run the code below. I dont understand why I can't see the src data value of 14 in register r1 when I ldr the data value from the address stored in the r0 register. I have tried to research the solution myself but cant find an exact reason why this happens. Any help would be gratefully appreciated.


  THUMB
  AREA RESET, CODE, READONLY
  EXPORT  __Vectors
  EXPORT Reset_Handler
__Vectors
  DCD 0x20001000
  DCD Reset_Handler

  AREA Code, CODE, READONLY
  ENTRY
Reset_Handler
        LDR     r0, =src
        LDR     r1, [r0]

terminate  ; sit in an endless loop
  B terminate

    ; data area that can be modified (readwrite)
  AREA Data, DATA, READWRITE
src     DCD     14
        END

  • This is due to the way you have the linker configured (scatter file), and that statics bound for RAM are placed into the back end of the FLASH/ROM portion with the expectation that start up code (either the C run time library, or your own) will copy the statics and zero other regions. When the processor resets the RAM is assumed to contain garbage, and must be loaded with the default/expected values.

    Your code does not provide any copying function, the processor starts and goes directly to your code. The routine __main normally does this before calling your own main() function.

    So either place your constants in FLASH, or take more ownership of copying them too RAM.

    You might want to search on the linker documentation, and the concepts related to startup from ROM.

  • Thank you for your prompt and detailed answer. I have researched the topics that you have said for me to look at in your answer but I am not quite understanding this. I understand what you are telling me to do and why I can't initialise my constants I just don't know how to go about doing it. My research has not helped me understand how to do this I feel more confused!
    From what you are saying I don't have uvision configured correctly in particular the linker. When I go into project options and the linker tab under scatter file there is no file. Also how would you copy constants to RAM in code I dont understand what code you would write to do this.
    Does my whole problem stem from the incorrectly configured linker or do I also need to add code in once I get the linker correctly configured.

  • Thank you very much for all your help. I now understand what I have to do to initialise the data from reading the links you provided.

    Thank you once again for your patience and help.

  •   THUMB
      AREA RESET, CODE, READONLY
      EXPORT  __Vectors
    __Vectors
      DCD 0x20001000
      DCD Reset_Handler
    
      AREA Code, CODE, READONLY
      ENTRY
    Reset_Handler PROC
      EXPORT Reset_Handler
    
      IMPORT ||Lib$$Request$$armlib|| ; Pull in library
      IMPORT __scatterload ; Scatter Loader
    
            BL      __scatterload ; Initialize statics in RAM
                      ;  jumps to __main_after_scatterload
    
            ENDP ; Reset_Handler
    
    
    __main_after_scatterload PROC
      EXPORT __main_after_scatterload
    
            LDR     r0, =src
            LDR     r1, [r0]
    
            ldr     r1, =123
    
            mov     r1, #456
    
            LDR     r0, =foo
            LDR     r1, [r0]
    
            B       .  ; sit in an endless loop
    
            ENDP ; __main_after_scatterload
    
    
    foo     DCD     1234 ; In ROM
    
        ; data area that can be modified (readwrite)
      AREA Data, DATA, READWRITE
    src     DCD     14
    
            END
    

  • Thanks that code works great. Problem solved.