3

I'm trying to create a factorial method using THUMB instructions, and I'm basically there.

I just have one question about the PUSH/POP opcodes: if I stored the value of r0 in the stack using push (so push {r0}), can I later use pop {r1} to pull it out or do I need to specify the same register as it was in to begin with? Thanks for your help.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • All the instructions are separate. If you wish to use push/pop to copy the previous contents of r0 to r1 you can do it. – Aki Suihkonen Dec 03 '12 at 15:31
  • not specific to arm, any architecture from any vendor (x86, arm, mips, etc) that allows you to specify the register/memory being pushed and register/memory that is the destination of the pop, as an assembly programmer you are free to push and pop to/from wherever you like, if the hardware had a limitation then you would not have the freedom to choose the register and you would be told what register is used. – old_timer Dec 03 '12 at 18:58

1 Answers1

4

Yes, you can since push/pop actually expand to store/load multiple, which are generic instructions operating on registers and memory, so

push {r0}

is equivalent to

stmdb sp!, {r0}  @ or stmfd sp!, {r0} in alt notation

and

pop {r1}

is the same as

ldmia sp!, {r1}  @ or ldmfd sp!, {r1}
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 2
    There is one caveat, arguments to push/pop/ldm/stm are in ascending register order, not in the order specified. so if you do push{r0,r1} and then pop{r1,r0} intending to swap them, this will fail because pop{r1,r0} is identical to pop{r0,r1} – Variable Length Coder Dec 03 '12 at 19:09
  • Yes, with a list of registers you have to watch out for this. Thanks @VariableLengthCoder. – Nikolai Fetissov Dec 03 '12 at 19:14