I am trying to write simple program using ARM 7 Assembly, but I cannot initialize memory region with simple data. Here is an example
AREA RESET, CODE, READWRITE ENTRY LDR r0, =SortArray ;load start address LDR r1, =SortArrayEnd ; load end address STR r15, [r0] LDR r4, [r0] SUB r2, r2, #1 ; r2 contains (LENGTH-1) MOV r6, #0 ; r6 sum set to 0 STOP B STOP LTORG AREA my_data, DATA, READWRITE ALIGN SortArray DCD 1,5,20,32,13,66,3,5,23,64,112,66,22 SortArrayEnd END
This doesn't loads data in memory, in debug mode SortArray in r0 register points to 0x40000000 and SortArrayEnd points to 0x40000034, but there is no data
Memory map for data in separate area - i.stack.imgur.com/JNAK2.png
But as you may have noticed there is instruction for storing data in the memory STR r15, [r0] and it works perfectly data is stored there.
Next example works but as you can see I placed data inside code area and made it READWRITE that is probably bad idea. But in this case I can see my data in the memory.
AREA RESET, CODE, READWRITE SortArray DCD 1,5,20,32,13,66,3,5,23,64,112,66,22 SortArrayEnd ENTRY LDR r0, =SortArray ;load start address LDR r1, =SortArrayEnd ; load end address STR r15, [r0] LDR r4, [r0] SUB r2, r2, #1 ; r2 contains (LENGTH-1) MOV r6, #0 ; r6 sum set to 0 STOP B STOP LTORG END
Here is my memory map it starts from 0x00000000
Memory Map WIth Data placed in code Area - i.stack.imgur.com/Vfkwf.png
Please help to find the problem, I have spent several days trying to figure out what is wrong.
I am using uVision 4 and device is LCP2148.
You must store the initial values in flash - RAM can't keep the content when you power off the unit.
Same with C programs - they have startup code that copies any initial values from flash to RAM and then zero-fills the rest of the RAM.
Thank you for answer, could you please provide correct example how should I use variables. For example how can store something like static variables in Asseembly.
Thank you.
Have your code call __main, and name your entry point for code after the static initialization called main. Look at the startup_foo.s for your processor/board for an example of how to start things before handing off to user code
Or manage the load region yourself by examining what __main is doing
Thanks for answer as you can see I am using RESET so it is called directly without initialization stuff (stack configuration and so on). So I just need this work just for study purpose. Please suggest what should I try, how should I store this variables correctly, as far as I know object files (linux) contains different sections including section with static variables, how can I achieve the same result here ?
When the processor starts the RAM holds random content, you must copy whatever you need to it from ROM. You need to implement an equivalent to memcpy and memset to do the work of setting up the RAM.
Using the runtime library to do your donkey work should look something like this
; ... AREA STACK, NOINIT, READWRITE, ALIGN=3 Stack_Mem SPACE 1024 Stack_Top ; ... AREA RESET, CODE, READONLY ARM Vectors LDR PC, Reset_Addr ; ... Reset_Addr DCD Reset_Handler ; ... ; Reset Handler EXPORT Reset_Handler Reset_Handler LDR R0, =Stack_Top MOV SP, R0 IMPORT __main LDR R0, =__main BX R0 EXPORT main main ; Your code ; ... AREA |.data|, DATA, READWRITE ALIGN foo DCD 1,2,3,4,5,6 END