13 hours 15 minutes
Hello. This is Dr Miller, And this is Episode 11.8 of Assembly.
Today, we're going to give an example using the stack and how to save registers.
So our Stack example.
So the problem that we've previous really solved is we wanted to write a function that would print off some numbers.
So we read end from the user, and then we'd print powers of two.
And so we're take a look at the previous example. And so what we're gonna do is we're gonna right a little function here that goes ahead and prince these off, and we're going to sort of switch some of these registers around so that there,
um, it makes a little bit more sense and is a little bit easier to understand.
So let's go there.
All right, So we have our previous example.
And so what we'd like to do is we're gonna change up these registers a little bit. So instead of using, um,
are nine for our constant we're going to use
This is our one.
And then our counter value is gonna be in r zero.
Let's put that in here.
And then, um, sorry. That's the max
max values and
are zero, and then our counter is going to be
And thus if we write a function that is going to overwrite thes values, we're gonna want to save them. So we're going to create our own function down here called Print.
And so basically inside of print, we're going to, um, use different registers.
So, for example, we're going to move
from our to tow our three. So are three is going to
have our last argument?
currently. So we're just gonna copy that into our to
will also move.
this, like this are one gets are too.
And then load register are zero is going to
get the format that we want a print
and then here will go ahead and call print off.
And we have all of our registers at the end. Very. Instead of return, we got branch and exchange our link register.
So what we need to do in here is we need to save our registers, so push our
well all the registers that we're going to use. So we end up using our
We're also going to use the link register,
so you can see we have a branch with links. So that's going to destroy the link register, which has where print function is going to be.
And so we need to save that. And then at the end, we'll do pop
R zero to R three
and the link register.
So that should be our function that we're going to use to print. Um, here, we're going to change up our registers. So here, we're going to compare our to
and are zero.
When we are done, we'll go there,
um, in here, we're gonna use move into our three is going to get our
value that we want to print are one.
And then we will,
um, do the shift on our to
Now, the stuff in here, we're going to, um,
get rid of because we're not gonna be printing anymore.
We will print in our new function. So they brand with link to our print function, which we've defined down here
instead of our seven we're using, are too.
So I wanted to use
are 01 and two inside of here so that we saw that we needed to save those because those are going to get obliterated
by the print A function,
and we'll take out some additional space
Double check here. So our to our one. Okay,
so we can see that we print off three powers of two or six powers of two or someone and so forth.
So today we looked at creating your own function and making sure that we saved the appropriate registers. So the ones that were using inside of our function that could be modified as well. As if you're calling a function with branch and link, you saved the link register.
So, looking forward, we're going to talk about arm indexing and possibly armor raise.
If you have questions, you can email me Miller, MJ, you and Kate. I e d u. And you can find me on Twitter at Milhouse 30