Push and Pop in the hand

I really don't understand how stack commands work or how stacks generally work.

Tell me if I have

PUSH R3 POP R3 

Line 1: Does this mean that the contents of R3 will be placed on top of the stack? Will the contents of the stack change if R3 changes?

Line 2: In the second line, will the contents at the top of the stack be moved from the stack to R3 OR - did the contents of R3 that was pushed onto the stack jump out of the stack?

Also, what pop / push does when the register is surrounded in brackets so

 POP {LR} 
+5
source share
2 answers

The specified stack operations are only shortcuts for memory operations using sp in the address part. For instance.

 PUSH {r3} POP {r3} 

are aliases for

 str r3, [sp, #-4]! ldr r3, [sp], #4 

The first operation says "save the contents of r3 to [sp - #4] and reduce sp by 4". Last one "load r3 from [sp] and increment sp by 4".

Instead of {r3} you can use any other registers or registers (for example, {r1,r2,r3,lr} ). The sets of registers are indicated in the bitmask of the machine code, so you cannot influence the order of storage / loading of registers.

+7
source

Minimal armv7 example

The best way to learn is to write minimal examples, run them on emulators, and watch what happens in all registers using GDB:

 /* Save sp before push. */ mov r0, sp /* Push. */ mov r1, #1 mov r2, #2 push {r1, r2} /* Save sp after push. */ mov r1, sp /* Restore. */ mov r3, #0 mov r4, #0 pop {r3, r4} cmp r3, #1 bne fail cmp r4, #2 bne fail /* Check that stack pointer moved down by 8 bytes * (2 registers x 4 bytes each). */ sub r0, r1 cmp r0, #8 bne fail 

Boilerplate to run the example on Ubuntu 16.04: https://github.com/cirosantilli/arm-assembly-cheat/blob/cd46bd4ab163b2ef1a7904c017afb0b980f74c88/push.S

The brackets are called "registration lists" in the handle notation. GNU GAS 2.26.1 does not accept push and pop commands without curly braces, even for single registers it presses {} , as in push r1 .

Also note:

+2
source

Source: https://habr.com/ru/post/1207543/


All Articles