Stack: Difference between revisions

1,417 bytes added ,  2 years ago
m
→‎{{header|ARM Assembly}}: Fixed incorrect information and added peek and empty
m (→‎{{header|ARM Assembly}}: Fixed incorrect information and added peek and empty)
Line 722:
 
=={{header|ARM Assembly}}==
The stack is held in register 1413, or <code>r14r13</code> but more commonly referred to as <code>SP</code> for clarity.
 
Pushing and popping multiple values is very similar to [[68000 Assembly]].
Line 728:
LDMFD sp!,{r0-r12,pc} ;pop r0 thru r12, and the value that was in the link register is put into the program counter.
;This acts as a pop and return command all-in-one. (Most programs use bx lr to return.)</lang>
 
Like in 68000 Assembly, you are not limited to using <code>SP</code> as the source/destination for these commands; any register can fulfill that role. If you wish to have multiple stacks, then so be it.
 
The stack pointer will work with any operation the other registers can. As such, a peek can be done by using an <code>LDR</code> with the stack pointer as the address register:
 
<lang ARM Assembly>LDR r0,[sp] ;load the top of the stack into r0</lang>
 
A check if the stack is empty is also very simple, provided the initial value of the stack pointer was saved at the start of the program, or (more likely) was loaded from a nearby memory location.
 
<lang ARM Assembly>;this example uses VASM syntax which considers a "word" to be 16-bit regardless of the architecture
InitStackPointer: .long 0x3FFFFFFF ;other assemblers would call this a "word"
 
MOV R1,#InitStackPointer
LDR SP,[R1] ;set up the stack pointer
LDR R2,[R1] ;also load it into R2
;There's no point in checking since we haven't pushed/popped anything but just for demonstration purposes we'll check now
CMP SP,R2
BEQ StackIsEmpty</lang>
 
In THUMB mode, the <code>PUSH</code> and <code>POP</code> commands replace <code>STMFD</code> and <code>LDMFD</code>. They work in a similar fashion, but are limited to just the stack unlike the real <code>STMFD</code> and <code>LDMFD</code> commands which can use any register as the "stack pointer."
 
=={{header|Arturo}}==
1,489

edits