Hello, this is Dr. Miller and this
is Episode eleven point seven of
Assembly.
Today, we're going to talk about
pushing and popping in arm,
so push in pop.
So as we've talked about in
86, push and pop allow us to
save registers onto the stack.
The stack is pointed to a big
called Espie, 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?
So generally, this will
depend on the application, binary
interface or Abai
and specified down here.
And we can see that there are a
variety of registers that we should
know about. So
to sum it up, R zero.
There are three or four arguments.
So if you need to pass an argument
to a function, typically it will go
in. One of the registers are
zero through R three.
So those four registers down here
and then our fourth through all our
eight, our colleague 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 the platform.
So when in doubt.
Right. I would save that an
hour, 10 and 11 our colleagues
saved and then our
12 through our 15 or our special
registers. So our 12 is the
instruction pointer.
R 13 is a stack pointer.
Our 14 is the link register.
An 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 and 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'll actually reorganize
them if you don't
and then save the ones that you want
to keep. So, for example,
you could say push hour one hour
three, hour five and link register.
You put it inside these curly
brackets and then you list each one
that you want to save.
OMGPOP, 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 L.R., push our zero,
push our one.
Right. And either one of those
would work.
And then when we were pushing one
register right there,
assembler actually show us this,
that if you say push L.R.,
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 pharming vexing.
But I just wanted to show you what
those are.
Here we do it, the same for R zero
and R one, we can see that it's
again pushing storing to a store
registered to the data at Stack
Pointer and then it's got an
offset of four and then
popping is similar.
All right. So we got load register
one stack pointer and then we
are going to add four
when we're all done.
But if we end up doing more than one
register, it actually uses the push
up code in order to push those on to
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 L.R., that's
the same Estoril clasp,
minus four bang.
And so we can we should be able to
see that the link
register, which is right here, it's
seven six E,
we're going to see that end up being
pushed onto the stack.
Seven, six E eight.
The link register matches here
it is now on top of the stack.
The stack was at 120.
So I'll go back one slide
into the top of the stack was that
one 120 and now it's at
one C, 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
in the stack pointer gets modified.
And so here now we're going to do
push R zero and R one.
So we should see this drop down to
in the values of R zero and R one
are going to go on top of the stack.
So now we sit at the top of the
stack, right, so we have our seven
six E that has been pushed down,
right. And we have
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 podium counter
points to the next instruction that
is listed on their.
So today, we talked about pushing
registers and popping registers,
and again, going forward, we're
going to look at arm indexing modes.
So how do you say registers
L.R. are one,
are zero, are three, and orate
onto the stack.
So we could do a push or zero
through our three, I guess, and
then added an extra register
R8 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 KSTU, and you can find me
on Twitter at Millhiser 30.