Thread: [Tutorial] The Concept of ASM
View Single Post
Old August 9th, 2013 (6:27 PM). Edited October 25th, 2013 by karatekid552.
karatekid552's Avatar
karatekid552 karatekid552 is offline
What happens if I push it?....
Join Date: Nov 2012
Location: Do you really want to know? Really?
Gender: Male
Nature: Bold
Posts: 1,766
Send a message via AIM to karatekid552 Send a message via Windows Live Messenger to karatekid552 Send a message via Skype™ to karatekid552
Originally Posted by Kurapika View Post
Does the SP have a limit?
What does f.e. sub SP, #0x4 or add SP, SP, #0x4 mean? Do they work like pop and push?
This is a very good question. Let me explain:

The stack pointer always points to the location of the last pushed item. But, let's say we need to access an item we pushed two pushes ago, but don't want to disturb the other pushed items. sub SP, #0x4 or add SP, SP, #0x4 is how you would do that. When an item is pushed, the SP is DECREASED by 4. (Just enforcing that we don't make the mistake that pushing adds. The stack is actually upside-down if you think about it.) So, let's look at some code:

push {r1}
push {r2}
push {r3}
mov r1, #0x0
mov r2, #0x0
mov r3, #0x0
add sp, #0x8
mov r1, [sp]
sub sp, #0x8

So, what is on r1?

The original value of r1!

So, all of them are still stored in the stack, but now we have recovered the value of r1 without poping. This is useful if you push {r4, lr}. but then later need to recover r4, but don't want to pop r14. Do you understand?

Edit: Just realized I made a mistake here. When you are pushing a list of resgisters, like {r4, lr}, it is actually going to place them on the stack in reverse order. So, r4 would be on top and lr below it. So, in the last example, lr would be recovered, not r4. I realized this later when I remembered that since you always pop {pc} last, it must have been pushed first.

Paired with Simba
Reply With Quote