13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 6.8 of Assembly.
Today we're gonna talk about saving the register state,
so saving registers.
So when you are in a function, you might have a set of registers that are doing something.
And before you call another function, you will want to maybe save those right so that you don't ruin them while you're in the function.
And so there are a couple of different approaches to this. So the first is to basically push all of the registers that are modified,
So you keep track of which registered. Did I modify? If I didn't modify it, then I don't need to save it.
You also could push all of the registers. So just push all the general purpose ones, and then you don't have to worry about it in the end.
And then we can also push the flags.
So saving registers wanted a time. So what you want to do is you want to figure out rich registers that you want to save onto the stack, and then you pick a certain order. So here I Didier X and then EEC X and then ebx that order could be anyone that you choose.
You do your normal function stuff, so you modify the registers and then when you pop them off, one of the things to remember is that you need to go in the opposite order that you push them
right because they stack is a last in first out.
And so if the last thing that I pushed his e b X, then the first thing that I need to restore is ebx and then EEC X and then e x.
And so when you enter your function, you do your prologue as normal. And then you would save any registers that you need.
And then you do the function stuff, and at the end you'd restore them, and then you can exit using the epilogue.
Now, the second approach is to basically just save all of the registers. And so you can use a command like push a d.
So that stands for push and then a is for all and then d means d words. So we're using the extended registers
and so this will save all of them and it saves them in a weird order. It so yea x then e c x than e d x than e b X
then the yes p when it started. Um, E v P yes, I and e D i.
And then the opposite command for this is pop 80 right? So it goes in the reverse order. So Edie eyes first. Yes, I e bp ebx e d x, Easy X
and then find the X. And then e s p is restored back to the state that it was before this call was issued.
And then if you're worrying about flags So if you maybe set a flag using a command like a compare or something like that,
you could basically save all of them. And it uses the double word
and that you can use push f d to save all of the flags onto the stack and then pop FD to restore them from the stack.
And this is if you had had flags and you were worried about them being modified in the function. Or if you're near function, you are going to modify them, and you
don't want to mess up somebody else.
Now keep in mind all of the saving is taking extra space and taking extra time.
So it does take extra cycles for it to save the data to the stack. And the stack is stored in Ram and Ram is slower than the CPU. And so there comes a performance penalty when you save all of these registers.
So let's do some examples.
So first, we're going to save some of the registers, and then we're gonna save all of them and our 1st 1 we're actually going to go back to an example we've previously done
so. This is a function that we looked at previously,
and we can see this is the mod function. And so if I'm going to this function, I need to look in my body and see what registers do. I modify.
So it looks like I modify E, c, X and E x and E. D X. And then I do a divide, and that is also going to modify yea X and E. D X.
And then I save my result.
And so here the results of my function is going to be stored in E. X. So I don't want to save that in the restoring, because then all of the work that I didn't. My function will be useless,
but I do want to save maybe e c x and e d. X. So I come in here picking order that I want to and say push E c X
And then at the end, I want to restore those so pop and then the reverse order e d x
pop e c x
and now I function should still work as normal. But now it hasn't modified those registers, right? It saved them and then restored them.
And so it should still work,
All right. And then the second example,
we're going to use the push a d. Now
it's kind of interesting, because inside of our function, we did use Push A and Pop, which are pneumonic Same new Monix, right? So push all of those registers.
Let's just look at what it does.
So if we give ourselves some values here,
so move one into yaks New
two and TVX
four into dcx
just picking registers that are going to get saved.
And then what I want to do is I want to save all of my registers so I can say push
a D and I'm gonna give this a label.
So that way, when I'm debugging Aiken just just set a break point for that label,
and then I could do some extra work so I can say something like movie X
20 Movie v X
30 movie, see Ex
Move. Yes, I
And then I'll go on all restore my registers So, pop e d.
And then we're just gonna run that a bugger so that we can kind of see what's gonna happen. So g d B
And then because I put that label and I can just say break and then here. So it's a nice, convenient way to do that.
So then I can start running type are.
And so we've issued all of the commands to modify e x e b excess. We can see that the values of our registers have been set. Right, So we set e x ebx ccx
e d x e v p yes, I and e d. I also different separate values
and then we're gonna issue the push function, so we should see all of those values Go here into this stack
step my instruction. So now we can say see yea x, I think Ebx e c x e d x.
You got a stack pointer, E b P
um, yes, Size 10. And Edie eyes 20.
So you can see that those are saved onto the stack
and now we'll just go through and we're modifying all of these registers.
All right, so we got to the point where a modified each one of them, right? So now they're completely different values than we had before, and I left sort of gsp alone.
And then when I do the pop A,
we can see that our registers got reset back to what they were right. So we were able to save them, and it saved them in a particular order.
And then we were able to restore them using the pop function.
And then I can finish the program, which doesn't do anything.
So today we talked about saving registers in a function and either doing that by saving all of the registers or doing it by saving the ones that we modified. And generally the approach would be to save the ones you modify. If you know which ones are gonna be modified.
And then if you have flags, we learned how to save those flags in a function.
So looking forward, we're gonna have ah, more complicated example that uses the stack and parameters and then saves registers that we're modifying.
So here's our quiz. So what is the command that will save all of the general purpose registers?
Push a D.
What is the command that will restore the flags again? Assuming that you have saved them
pop F D.
So if you have questions, you can contact me at Miller MJ at you and Kate I e to you, and you can find me on Twitter at Milhouse 30.