ARM Cortex M4 assembly questions
Hello everyone,
I'm just starting to learn assembly for the ARM architecture. I'm also in general a beginner with the assembly language I have just some minor experience with the x86 assembly but that's about it.
I'm following a course on Udemy and I have this code which explains how to create an assembly project without the use of the startup.s file generated by Keil.
This is why I'm doing stuff like setting up the vector table and stack initialization in the main.s file.
Stack_Size EQU 0x00000100 ;Define stack size of 256 bytes
;Here we are just telling the processor to reserve a section of RAM called STACK for us, nothing else.
AREA STACK,NOINIT,READWRITE,ALIGN=3
; Why do we need the StackMem label for this?
StackMem SPACE Stack_Size ;the SPACE directive reserves a zeroed block of memory. Fill 0x100 bytes with 0.(starting at address of StackMem label?????)
AREA RESET,DATA, READONLY ;Reserve a section in RAM called RESET
EXPORT __Vectors
__Vectors
DCD StackMem +Stack_Size ;Allocates 1 word(32b) containing Stack_Size + the address of the label StackMem. Stack pointer when stack is empty.
DCD Reset_Handler ;Allocates 1 word after StackMem + Stack_Size
ALIGN ;sets the current location to the next word(4 byte) boundary
AREA SimpleProject,CODE,READONLY,ALIGN=2
ENTRY ;Does this mean that after executing Reset_Handler routine we should arrive here????
EXPORT Reset_Handler ; Why do we export the Reset_Handler here why don't we export it after the Reset_Handler label???????
Reset_Handler
MOV R5,#0x1234
MOV R3,#0x1234
ADD R6,R5,R3
STOP B STOP
ENDThe questions are also in the code as comments but I will ask them in text here also. Please ignore the comments from the code, they are just for me. I'm sorry if some of them make no sense for someone more knowledgeable :) . Here are the questions:
1. Why do we need this line?
AREA STACK,NOINIT,READWRITE,ALIGN=3
I understand what it does that we are reserving 256 bytes for an area called STACK but I do not understand how is that used in our program since we are not using
the STACK(capital case) name anywhere else in the program. I have read the documentation on AREA directive but I still do not know why that line is needed.
2. On this line:
StackMem SPACE Stack_Size
we are filling the 256 bytes reserved for the stack with 0. Why do we need the StackMem label there why can't we just do SPACE Stack_Size without it?
3. What does this line:
DCD StackMem +Stack_Size
do exactly? Does it allocate 1 word at the address of StackMem label which contains the value 256 which is the number of bytes reserved for the stack?
And when we do right after the above line:
DCD Reset_Handler
does this mean we are allocating 1 word which will contain the address of the label Reset_Handler declared below?
4. The 'Entry" derective means that after executing the Reset_Handler procedure which we declare below the code will start from there?
5. Why do we export Reset_Handler before we actually declare the Reset_Handler label below?
Thank you for reading! Please help me understand these things and again, I apologize if these questions make no sense but I'm a total beginner when it comes to ARM assembly.
