I have a startup code for ARM926ej-s which supports ISA ARMv5TEJ. Startup code looks like below, but i switched some commands/data with comments for clarity. Lines which are still unclear to me are marked with a comment “@???????????????????????????????????????????????????????“.
I wonder, why do we need to substract “#4” from value (location of “arm926ejs_reset“) stored in register “r3“? And then load it into a stack pointer 4 lines later, where we are setting the stacks for the first processor mode which is fast interrupt mode.
__start:
arm926ejs_reset:
@here there is image header data needed by ISROM
arm926ejs_reset_handler:
@here we disable MMU, I-cache, D-cache, flush them... and prepare the mcpu.
LDR r5, =inVirtMem
@here we enable MMU.
MOV pc, r5
NOP
NOP
NOP
inVirtMem:
ADR r3, arm926ejs_reset @load location of "arm926ejs_reset" label in r3
MOV r1, #IF_MASK
SUB r3, r3, #4 @???????????????????????????????????????????????????????
ORR r0, r1, #MODE_FIQ
MSR cpsr_cxsf, r0
MOV sp, r3 @???????????????????????????????????????????????????????????
SUB r3, r3, #FIQ_STACK_SIZE
...
I found similar startup codes as yours on the net which have their code well commented. In this start up code, you can see on
Line 186thatr3is pointing to theMMUbase address.MMUspans ahead of its base address. i.e ifMMU_BASE_ADDRESS=0x400,MMUentries will be at0x400,0x4040x408etc. Now inLine 217r3is being subtracted by 4 and then assigned toSPonLine 222. If we don’t subtract 4, our first entry of stack ( observe stack is growing in the reverse direction ofMMUlayout ) will overwrite on the firstMMUentry! Hence the subtraction.Likewise in your example if we don’t subtract 4, your stack will overwrite on the reset handler itself. Your program will go for a toss.
One more example is this start up code where they are going to end of
IRAMand subtracting 4. End of IRAM implies just outside theIRAMand hence the subtraction to come inside theIRAMObserve that stack is growing upwards in this particular example( or towards lower addresses ).( ARM stacks can grow in either ways )