Hello. This is Dr Miller, And this is Episode 12.9 of Assembly.
Today we're going to give an arm thumb. Example.
Our thumb example. So what's our problem? To be solved.
So we're going to create a program that prince a tree or triangle of Asterix. I'll actually leave it up to you to try and see if you can design an actual tree.
So we're gonna write a couple different functions. They're gonna print off our *** tricks for us.
Alright, so first of all, we'll start off with being able to print are different things. So we're going to create some strings.
I don't have one called star
for being a gastric, and we're gonna have one that's on 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 link register well done, and then we'll do a branch in exchange.
So the first thing we're gonna do is we're just gonna basically ask Caesar how maney we want them to read so
you could add a prompt a few woman.
So then we're going to write our thumb function. So we're gonna do branch with Link and Exchange
because we're going to go into thumb mode and we're going to call a function called Try.
So this will be able to build our triangle for us.
So the first thing we're gonna 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 gonna line on two byte boundaries
and then this is a film
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, um, are seven. So
what we're gonna do is we're gonna save R seven and R link register,
So if you ever compiled some code, you'll end up seeing that.
So just copying stack pointer to our seven and then subtracting from the stack pointer,
we'll just save 20 at this point.
Now we go to clean up our stack. We're going to move SP R seven
and we're gonna say are seven to save that one and then
we're gonna prop into the program counter. So we pushed the link register and then we prop into the program counter. And so let's return from our function.
It's just one way to do it. There's lots of different ways,
all right, so in this function, we're going to use our six a za counter,
and it's going to start at zero, and it's actually an account, the second part of what we're doing. If we recall from radiant that's in our zero and so r zero will have
the number that we want to do, and so this is going to count the opposite way. So we're gonna deck Ament
are zero until we get down to the bottom and we're gonna incriminate our six at the end.
So here is a top of our loop.
at the bottom, and then we're going to be doing some comparisons. So we will compare
are 0 to 0, so r zero is going to be going down.
So let's make a comment So
So if Arsene ours zero is the number zero So branch equal,
we're going to actually what is called us and said a top Let's call it t one.
It's a top one, cause we're gonna have two different functions
when it's done, we're going to go to done one, because we're going to do two different functions here.
Okay, 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 gonna use our zero. My other functions, I'm gonna push are zero.
I'm done doing my friend staff pop
Okay. And then we're gonna end up calling our star function, so we'll start here and do this. And then we'll, um, add some additional things so we can go ahead and load into our five
the address of star,
and then we're gonna do branch of link and then star
fun. So this is gonna be our function that's going to do stars. Now we're in thumb mode, and we're gonna write another thumb function so we don't need to exchange.
Oh, we want to do some other fun stuff, like move are 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 branches link
print F so we can print off our new line.
Um, once we're done with all of our funds stuff that we're doing, we restore our zero, and then we need to decker minute. So, sub
But then we want our six to go up. So add our six,
so now we'll get into our star function. So again we're going to tell it
we're still lining it, Teoh,
and this is also a thumb function. So this is gonna be called
and we will have similar things in this one that we had in the previous one. So again push
our seven our link register cause we got to save where we're going.
It's a check from our our stack
at the end of our function again doing the same thing.
We're gonna move SP r seven
Oh, and the program counter.
All right, so here is the fun stuff that we need to do. So again, we're gonna have and this one we're gonna have a top,
and at the bottom of it, we're gonna branch to t two. And they were to do a comparison.
and this one, we are going to,
so are zero is our counter that we had in our previous one. And we call this we want to. Basically, Decker meant that
Weaken Decker meant it because we've saved in our previous function.
So let's start that off. So move are for
So we'll copy R zero into our four,
so we can use it later. And movinto are three
zero. So we'll use these a little bit later.
So here will do a compare
we will branch if they are equal
to done right. So Well, this is where we'll exit.
So are four is going to get dec amended, and this is gonna 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
are five is gonna have what we're going to print, So that's gonna be a pointer to our printer. So we set that up here,
And so then inside of here, we can use that as okay, this is the thing we're gonna wanna print
because we're actually gonna print some different things based on what we want to do.
And then we want and push
both r three and R four
onto the stack so that we can save him because we're gonna call print off and that will probably destroy some stuff. So branch link in exchange.
So print f is not thumb. And so we have to do a Lincoln exchange in order to get to it.
And then here we can restore registers R three and R for
and then we want Teoh. Add are three
by one. Right, So are three started
started here at zero, and it's going to keep implementing until it gets to our four, which had how many stars we wanted to print.
No, I forgot to set my one there.
so this function should print are stars. And then when we're up here, we can go ahead and we will load the star that we went to print and then call our star fund and then go ahead and get a new line and go ahead and print that
so we'll see if that works.
looks like I tried to do a move instead of a load
so we can see it printed off four stars and then three and then two and then one. And if we put in bigger numbers
20 and settle, 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
and I'm branch with links star
So then we were on that one and say 10.
So we want to move into our zero
are six. And remember, 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 who 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 So now star is a space and the space is a star.
And if you have a mono spaced font,
then you get sort of a triangle out in the middle of nowhere, right? So you printed a bunch of spaces and then we printed one, and then we printed two and then three and then four.
So a lot of times we'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
endless, look for one of our function. So let's look for
so I'm gonna look here for this string right here,
So I got put quotes around. It's a stuff doesn't get broken around,
minus A is to print some text after so a 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 thumb instructions here. One clear exception is going to be our branch with Lincoln Exchange. So that's a wide instruction for to go to print F.
Um, and then we get down to the bottom of our function. So that's star fun.
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,
um, regular thumb instructions that we can have them. We can see that we can mix them in between, just depending on what we're doing.
All right. So today we created a thumb 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 Miller M J U NK doubt you to you, and you can find me on Twitter at Milhouse 30