Hello, this is Dr. Miller and this
is Episode eleven point eight 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 previously
solved is we wanted to write a
function that would print
off some numbers.
So we read in 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 going to do is
we're going to write a little function
here that goes ahead and prints
these off and we're going to sort
of switch some of these registers
around so that there
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
going to change up these registers a
little bit. So instead of using
our nine for our consent,
we're going to use
let's use our one
and then our counter value
is going to be in our zero.
Let's put that in here.
And then
sorry, that's the max
max value is in
our zero and then our
counter is going to be.
Our two.
OK.
And thus, if we write a function
that is going to overwrite these
values, we're going to 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
use different registers.
So, for example, we're going to
move
from R2 to our three.
So our three is going to.
We have our last argument.
Currently, so we're just going to
copy that into our two,
we'll also move.
Let's look at this hour, one
gets our two
and then load register
our zero is going to
get the format that we want to
print.
And then here, we'll go ahead and
call into.
And we have all of our registers
at the end, right, 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 ours
well, all the registers that we're
going to use.
So we end up using our.
Zero through
our three, 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
our print function is going to be.
And so we need to save that.
And then at the end we'll do pop
or zero through our
three
and the link register.
So that should be our function that
we're going to use to print
here, we're going to change up our
register, so here we're going to
compare our two.
And or zero,
when we are done, we'll go there
and here we're going to use move
into our three is going
to get our view
that we want to print our one
and then we will
do the shift on our two
here.
Now, the stuff in here
we're going to
get rid of because we're not going
to be printing anymore.
We will print in our new function so
the brain to link to our print
function, which we've defined down
here.
And then instead of
R7, we're using R-2,
so I want her to use our zero
one 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 afunction
and we'll take out some additional
space and
I'll double check here.
So our two are
one, OK?
So we can see that we print off
three powers of two or
six powers of two or so on and so
forth.
So today, we looked at
creating our own function and
making sure that we save the
appropriate registers and so the
ones that we're using inside of our
function, that could be modified
as well as if you're calling a
function of the branch and link, you
save the link register.
So looking forward, we're going to
talk about arm indexing and possibly
armories.
If you have questions, you can email
me millirems, you and Kate out to
you and you can find me on Twitter
at millions 30.