Everybody welcome to the next video so we're gonna be talking about loops in assembly.
Now we're going to be using the loops built into Nazism as a start so we can get more familiar with
Nazism as a whole.
So what we've got here is similar to what our saving state video was on so we can see here we've got
our start where we push and push the registers and the flags we call our print method which prints our
Red Team nation and then we pop them back off the stack and reset the registers and the CPC flags.
Then we go ahead and exit we're going to make a few tweaks to this.
So we're going to set we're going to move start out of the way and we're going to name this something
else we'll call this we'll call it main right.
So what I want to do and start is move a value into AC X and we're going to do we'll do five right.
So the goal of this is I want to be able to loop and print this method five times.
So I want to go to print the string rotation five times one on each line.
Now easy X is also known as the counting registers.
So when you start doing things like loops or the keyword loop in ASM what that does is that's going
to be treated as easy x is whatever is an easy X is what you're gonna be counting down from.
Now when you use loop there's a few situations you need to keep in mind there is a Z flag or a zero
flag.
So when X is set to zero when it finally hits zero on the loop when loop method is called it will set
the Zero flag in the register or in the you flags register.
Now what's important about that is that means that the last time it will loop when it comes back through
and hits the loop call again it will see the Z flag or zero flag set and it will go ahead and skip it.
So what I want to do is show you how we take this same code into creating a loop.
So right now if we just ran this we would go to start it would move easy X and r 0 5 and easy X and
then since main we don't have anything right below here.
So we can actually we can leave this completely alone or we can do call me
either one is fine.
And the reason this works is because if we didn't use this it would just go to the next line in this
case main and then it would start going through this code.
We can leave this we can have it.
It's entirely up to you unless you had code in between start and mean you can just leave this out.
But if you had code in there you would need to do call and then the main method.
So this again is a function just like start is what we're doing here is removing 0 5 an easy X then
it's dumping down to push 80 push lefty calling the prints so we print it the first time we pop everything
back off and then we'd go ahead and return God and exit the application.
So what we want to do is loop at this point.
So this is just a simple as loop and then the call that we want print method.
So this is going to do this going to set our counter register to five go ahead and print it out.
It's the it's going to hit this line and then it's gonna check the Z flag if the flat if the Z flag
is set it will go ahead and skip down to this if it is not set it will go ahead and run this.
So this is simple it's similar to a jump not zero command which is something we're going to go over
the next video.
However just keep in mind for this particular case we're looking at the keyword loop.
So as long as zero flag is not set we'll go and print this out and then which will print it and then
we'll do our return and then we go back up to the top
I'm sorry this isn't print method.
This is going to be made because we want to loop not this down.
We want to loop just this part down the loop and then back up again.
So if we go ahead and compile this we can see we've got Red Team nation five times which is exactly
what we'd expect.
Now what I want you guys to do is play around with the loops try to do something a little bit different
maybe print out maybe do a loop inside of a loop go ahead and give that a try.
This is all we're going to do as far as loops are concerned just understand that loop to a particular
symbol but you have to make sure that you you do this in order since it's top down code when it comes
to assembly because it's going to compile and run directly down unless you specifically have it jump
somewhere else call somewhere else or loop to a different location.