Hello, this is Dr. Miller, and this
is Episode twelve point nine of
Assembly.
Today, we're going to give an arm
thumb example, so
our thumb example, so what's our
problem to be solved?
So we're going to create a program
that prints a tree or triangle
of Asterix.
Leave it up to you to try and see if
you can design an actual tree.
So we're going to write a couple of
different functions.
They're going to print off our
Asterix for us.
All right, so first
of all, we'll start off with
being able to print out different
things, so we're going to create
some strings
so we'll have one called Star.
To be an asterisk and we're going to
have one that's an underscore.
And you can change these
depending on how you want it to
look, and we might actually even
change them when we get done.
All right. So in our main, we're
going to go ahead and push
our link register.
And we will pop our
register when we're all done and
then we'll do our branch exchange,
so the first thing we're going to do
is we're going to basically ask the
user how many we want them to read.
So.
You could add a prompt if you want.
So then we're going to write our
thumb function, so we're going to do
branch with link and exchange
because we're going to go into Thum
mode and we're going to call a
function called TRI.
So this will be able to build our
triangle for us.
So the first thing we're going to
have to do is we're going to have
to tell the compiler and assembler
that we're going to be creating a
thumb function. So.
So we're going to tell that we're
going to lean on to bite boundaries.
And this is a thumb
function,
we're I call it try,
and then we don't have access to
necessarily all the registers that
we would normally have.
And so when we're doing
this for our stack pointer,
we're actually going to use our
seven. So what
we're going to do is we're going to save
our seven and
our link register.
So if you ever compile some code,
you end up seeing that.
So just copying the stack pointer to
R7 and then subtracting from
the stack pointer
will just say 20 at this point.
Now we've got to clean up our stack.
We're going to move ASPEY
or seven
and then
Pop and we're going
to say R7 to save that one
and then we're going to pop into
the program counter.
So we pushed the link register
and then we pop into the program
counter. And so that's the return
from our function,
which is one way to do it.
There's lots of different ways.
All right. So in this function,
we're going to use R6 as a
counter.
And it's going to start at zero and
it's actually going to count the
second part of what we're doing,
if we recall from reading it,
that's in our zero and so our zero,
we'll have the
number that we want to do.
And so this is going to count the
opposite way. So we're going to
decrement our zero
until we get down to the bottom and
we're going to increment our six at
the end.
So here's a top of our loop.
We'll jump up to it
at the bottom and then we're going
to be doing some comparisons, so
we will compare.
Are zero two zero,
so zero zero is going to be going
down, so let's make a comment.
So our six
goes
up.
Are zero goes.
OK.
So if our sitars zero
is the number zero subbranch equal,
we're going to and actually just
call this instead of top, let's call
it T1, it's the top
one because we're going to have two
different functions.
When it's done, we're going to go to
one, you know, because we're going
to do two different functions here.
OK, so this will allow us to break
out of our loop and then we can go
back up to the top.
So now we can do the fun stuff.
So first of all, I'm going to use R
zero on my other functions.
I'm going to push R zero.
I'm done doing my fun stuff.
Pop
are zero.
OK, and then we're going to end
up calling our star function, so
we'll start here and do this
and then we'll add some additional
things so we can go ahead and load
into R5,
the address of
Star.
And then we're going to do a branch
of think and then star
fun.
So this is going to be our function
that's going to do stars.
Now we're into mode and we're going
to write another thumb function so
we don't need to exchange.
And then let's see.
We want to do some other fun stuff
like Move our zero.
So let's create a new
line here.
So that we can print off the end of
our line.
So when we're all done, we move that
into our zero and then branch with
link printf
so we can print off our new line
once we're done with all of
our fun stuff that we're doing, we
restore our zero and then we need to
decrement it.
So sub R
zero.
The number one,
but then we want our six
to go up, so add
our six,
the number one.
All right.
So now we'll get into our star
function, so again, we're going to
tell it.
We are still aligning it to.
And this is also a thumb function,
so this is going to be called
star fun.
And we will have similar things in
this one that we had in the previous
one. So again, push.
Or seven or Lync register
because we got to save where we're
going.
Subtract from our stack
at the end of our function, again,
doing the same thing.
So we're going to move Espie
or seven.
And then Pop or
seven.
And our program counter.
All right, so here's the fun stuff
that we need to do, so again, we're
going to have and this one, we're
going to have a top,
so we'll go to.
And then down here,
these two.
And at the bottom of it, we're going
to branch to 2:00 in the morning
to do a comparison.
So in
this one, we are going to
sow our zero is our counter that we
had in our previous one and we call
this we want to basically decrement
that.
We can decrement it because we've
saved it in our previous function.
So let's start that off.
So move our for
our zero.
So we'll copy our zero into our four
so we can use it later and move into
our three.
Zero.
So we'll use these a little bit
later, so here
we'll do a compare our
three and our four
and then we will
branch if they are equal
to done so well, this
is where we'll exit.
So our four is going to get
documented and this is going to
get blown away when we do our print
f so we need to make sure that we
keep track of that
and then move into our
zero.
Five, so
R5 is going to have
what we're going to print, so it's
going to be a pointer to our
printer, so we set that up here.
And so then inside of here, we can
use that as, OK, this is the thing
we're going to want to print because
we're actually going to print some
different things based on what we
want to do.
And then we want to push
both our three and our
four.
Onto the stack so that we can save
them because we're going to call
printf, and that will probably
destroy some stuff.
So branch link and exchange.
So printf is not
Thum. And so we have to do a link
and exchange in order to get to it.
And then here we can restore our
registers R three and
R four
from our print F.
And then we want to add
our three.
By one.
All right, so our three started
started here at zero, and it's going
to keep incrementing until it gets
to our four, which had how many
stars we wanted to print.
Oh, I forgot my one there.
All right.
So this function should print our
stars and then when we're up here,
we can go ahead and we will
load the star that we want to
print and then call
our star fun and then go ahead
and get a new line and go ahead and
print that.
So let's see if that works.
Oh, and looks
like I tried to do a move
instead of a load.
Up in the new line.
So we can see it printed off, for
starters, and then three and then
two and then one, and if we put
in bigger numbers,
10,
20, so it'll print those
off.
So then we can go in there and we'll
add a couple more things and then
we'll look at the code for doing
this. So here we're printing off the
stars, but we could also do the same
thing for our underscores.
So here we can say,
Lord, register our
five
under and then
branch with Linkstar
fun.
So then we were on that one and say
10 oh.
I think we see.
Oh, we need to
so we want to move into
R zero, R
six and remember where we're saving
our zero, so we're just copying our
six.
So I wanted to do that.
Many stars, not just one.
So now we print the same
number of characters, we either
print them as stars or underscores,
and then if you go through and you
change it just a little bit
and actually if you just invert
this. Now, Star is a space and
a space is a star.
And if you have a mono spaced
font.
Then you get sort of a triangle out
in the middle nowhere. Right, so you
printed a bunch of spaces and then
we printed one, and then we printed
two and three and four.
So a lot of times I'll have students
try and make this turn this into a
Christmas tree. So print the other
side and then figure out how to
print the bottom.
But if we want to look.
And let's look for one of our
functions, so let's look for.
So I'm going to look here for this
string right here.
So I got to put quotes around it so
stuff doesn't get broken around
and then minus
A is to print some text after.
So I'll print, let's say, 30 lines
after.
So if we go here and look at our
star fund, we can see that
generally we have some instructions
here, one clear exception
is going to be our branch with
Lincoln Exchange.
So that's a wide instruction for
to go to printf
and then we get down to the bottom
of our function.
So that's Staffan
that we did.
We can also look at.
Try.
And again, we can see most of these
we got our branch of links end
up being wide instructions, but the
rest of them end up being
regular thumb instructions that we
can have. And we can see that we can
mix them in between
just depending on what we're doing.
All right, so today we created a
film program and we printed off
some stars and tried to build the
tree, and you can extend that if
you'd like.
Looking forward, we'll talk about
conditional execution.
If you have questions, you can email
me millirems, you, Ankita, Eddie,
and you can find me on Twitter at
Milhouse 30.