Hello. This is Dr Miller, and this is Episode 11.7 of Assembly.
Today we're gonna talk about pushing and popping in arm.
So as we've talked about in X 86 pushing pop allows to save registers onto the stack.
The stack is pointed to a by registered called SP, not E S P.
And then, as we know, calling function can destroy registers, and then you have to save particular ones that you might need inside of your assembly code.
So the question is, what should you save?
this will depend on the application binary interface or a B I amid specified down here,
and we can see that there are a variety of registers that we should know about. So,
um, to sum it up right are zero through. Our three are for arguments. So if you need to pass an argument to a function, typically it will go in. One of the registers are zero through our three. So those four registers down here,
and then our four through all our eight r Cali saved registers. So the function you're calling if it's going to modify them than it needs to save them, so you generally wouldn't have to necessarily save them.
Are nine is kind of up in the air, depending on, um, the platform.
So when in doubt, right, I would say that
and our 10 and r 11 r Cali saved,
and then our 12 through our 15 or our special registers are 12 is the instruction pointer
are 13 is a stack pointer are 14 is the link register, and ar 15 is the program counter.
And so those are all special, so you typically wouldn't necessarily need to save those. There wouldn't be something that you would want to resume
when you're doing it
so generally right. If you're using our zero through our three, you'll want to save those possibly are nine,
But otherwise you should be OK.
So when we push him pop, we can give it a list of registers and then the assembler would prefer if you put them in order
by their numbers. So I think it will actually reorganize them if you don't
and then save the ones that you want to keep. So, for example, you could say push R one R three R five and link register you put inside these curly brackets and then you list each one that you want to save.
Pop. We can do the same thing, and so it should restore all of those registers.
You also have the ability to give it a consecutive list. So I can say push our one through our five and link register. Or in theory, I can do them one at a time. Push LR push are zero Push our one right And either one of those,
And then when we're pushing one register right there, assembler actually show us this that if you say push LR,
that's the same as a store l R. And then stack pointer minus four with right back. And we'll talk about
right back when we talk about arming, indexing. But I just wanted to show you what those are
here. We do it the same for our zero on our one. We can see that it's again pushing, storing to a store, register to the data add stack pointer
and then it's got an offside of four
and then popping is similar, right? So we got load register are one stack pointer on Then we are gonna add for
when we're all done.
But if we end up doing more than one register, it actually uses the push op code in order to push those onto the stack.
So here is an example showing that we're stepping through the code.
So again it's showing that if I have the command push LR, that's the same a store L RSP
And so we can We should be able to see that the link register, which is right here. It's 76 e.
We're going to see that end up being pushed onto the stack. 76 e right. The Link Register matches. Here it is now on top of the stack. The stack was at 1 20 So go back. One slide.
It's at the top of the stack was at 1 20 now it's at one. See? Right, So it's gone down by four.
We should see that No other registers other than the program counter is moving to the next one.
I'm in the stack, pointer gets modified, and so here. Now we're going to do push are zero in our one. So we should see this drop down to and the values of R zero and are one are going to go on top of the stack.
So now we see that the top of the stack. Right? So we have our 76 e that has been pushed down, right? And we have,
um this is our one, and this is our zero. So those are both being pushed onto the stack. We can see that those are the same. And now we're at the next instructions of the program. Counter
points to the next instruction that is listed on there.
So today we talked about pushing registers and popping registers
and go again, going forward. We're gonna look at arm indexing modes.
So how do you save registers? L R R one R zero R three and R eight under the stack
so we could do a push. Are zero through our three.
Well, I guess I next added the next register,
Um, are eight and link register. So again, you want to go in order when you're pushing them.
So if you have questions, you can contact me, Miller, MJ at you and Kate. I e to you. And you can find me on Twitter at Milhouse 30