13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 4.1 of Assembly.
Today we're gonna talk about control structures and looping
So control control structures allow us to create a system that is Turing complete. And that means that we have the ability to do selection.
And both high level and assembly level language contain the ability in order to do selection.
So how do we do it? We use a couple different commands. One is compare. So competitive takes register one and register to, and it does subtraction on them.
And so we take register one minus, register to, and then we go ahead and set the flags. So flags say what happened in the previous operation that occurred? And so if the Kerry fly gets set, that means it had to carry when it did subtraction. The overflow flag means that
we didn't have enough room and so we had to overflow into another location.
The zero flag means that
the result was zero. So register one and registered to are equal in that case
and then for our operations, it doesn't matter the order, but one of them has to be a register
so either this one or this one or both can be a register and the other one can be either a memory address which we'll talk about later, or an immediate or ah, hard coded value.
So if register one and registered to are equal, then the zero flag will get set. And we can use that in order to tell if two things are equal.
If register one is greater than registered to than the zero flag is zero and they carry flag is zero.
If register one is less than registered to than the zero flag is zero and the carry flag is one meaning that I carry
flag got set because borrow had to occur.
So along with conditional XYZ, the ability to do jump it jumping around or branching using those flags got set
and we have two different kinds of jumps. So one is called the unconditional jump jump location. So whenever that instruction occurs, is just gonna go somewhere else.
And then we have the conditional jump. So Onley jump on a flag is set, so if a flag is set, then you get a jump on the location. Otherwise you're gonna execute the next instruction that comes after the current instruction.
We've got a bunch of different ones right in. Each of these relates to the flags. So, for example, jump zero jumped, not zero jump, overflow, jump, not overflow. And we got signed carry and then parody.
So here's a simple example that just checks to see what the user did. So if you look here, we can see that it says call reading. So that's gonna read a number from the user.
And then it's gonna do a comparison. Compare yaks to 10 again. We put an immediate in here, and then we have a register.
And so then the flags they're gonna get set. And so zero means that if I took yea X and I minus 10 right, that they're equal,
You can see that this jump zero is going to go down here to this 10 label.
So it says if it zero it's gonna jump down here and then execute this instruction down here.
Otherwise, if it's not zero, it's gonna execute this movie X other,
and then it will jump down here to the print Notice this This is an unconditional jump, meaning always do that jump
and up here we have a couple of strings that were declared. So we have 10. You entered 10 or other incorrect exclamation point.
We can see here that there's a tan in here that's a new line and then a zero byte in order to end our string. And so those are things that we have to do.
So either way, right, it's gonna movie the either e x, the value of other or e X, a value of 10. And then they both come down here in to do the print string,
right? So someone do move, move 10 in T A X, and then this is the next instruction after that.
And so this will allow the user to enter a number. It'll check of its 10 around and then it'll print off the result. Either you enter 10 or incorrect,
so I think you should go ahead and use your virtual machine or your Lennox machine and enter the code and see if you can get it to run.
So looping had allows us to do something over and over again, and there's a specific command called loop and so we can loop to some label which looks at E. C. X
right, and it jumps if it's not equal to zero. So if he see X is not zero, then it goes ahead and does a jump.
But a deck Ament ccx every time. So this allows you to
go through multiple times
and in the next lecture will look at an example using the loop or one of these two commands.
So we also have loop equal or loop zero. These are the same commands, right? Cause we have the zero flag, and if it zero, then that's what we use. Right? So jumps of E X is not is equal to zero and a zero flag is one or loop, not equal or loop, not zero.
And so here's an example using the loop command. So here we have the sort of formulaic version of that, and then we could translate that directly and assembly. So we're moving into yea X, the number zero we're moving into e c X the number 10 and then we're gonna add e X and easy X so e c X is 10.
Add that T a X, which is zero. So now yea x has 10
and then it's gonna do loop, so it's gonna check is E C X zero. Ready? Does a deck Ament on it checks to see if it zero. If it's not a jumps back up here, right, Does the ad then does the loop again, right? So we do this over and over and over again and tell
right? You see, X is gonna be 10 and then nine and then eight and 7654321
zero. Then it will exit the loop.
So that's an example of how the loop Command can be used.
So we're gonna do a real example. So we're going to read a number from the user and kind of on similar lines. We're gonna print all of the numbers up to that number,
So if the user enters seven, we're gonna print 1234567 And I'll put new lines in just so that we can kind of see how it works.
so I've already created my project.
So the first thing that we're gonna want to do is read the number from the user.
Now that number is stored in the X and I'd actually like to save it somewhere else because I want to use E X in my
compare. Er yeah, he X and my comparison and I want to be able to print it off and the only way to print it off really easily is Thebe print int, which requires the value that we're gonna print to be any X. And so I'm going to copy this into EBX.
So now I have a copy of my number and then I'm gonna set yea x 20
And then in order to do my comparison or to be able to decide when I'm done, I'm going to go ahead and compare
yea x and ebx.
And so if they're equal, then I'm done. So I will do jump equal.
Done now done is a label, which is what we need for jumping. So I'll go ahead and down here and
put a label called Done
and all a label is is a string of characters followed by a colon to say the instructions after this starting on this line are called that label.
All right, so if I'm not done, then what I want to do is I want to print the number that's in the X, So call
and then I'll print a new line.
So I printed
extra key there. I printed my imager than I printed off a new line. And now what I'd like to do so e X is my number that I'm printing. So I want to make that one bigger or I can say
And then what I want to do is I want to jump back up here. And so I'm gonna say, Jump,
I'll just call it to compare
and I'll put a label here,
so that way we'll jump up back up to the top now yay! X is one bigger. And so it does a comparison of the a X and EBX. Until they are equal,
go ahead and write and quit,
so find them into the number seven
it prints 036
So, prince, all the numbers up to that number, but not including that
you could change it to do it. A couple different versions of this
So, for example, if you wanted to start it at the number one
right, we could go ahead and do that
and we can do a couple of different versions of this, right? So I'll let you try and guess what they are.
You can try some examples. So, for example, there's also jump above or jump below jump greater than or equal to. There's a whole bunch. And so since I'm comparing e x t e b x, I maybe want to see if they are.
Let's see, I want to see if what will try one,
See if that works.
So jump. If this one is greater than so maybe
See if that works
and you can always run it
and it looks like it did. Right? So a kept going until the first. The number we're printing was greater than instead of greater than or equal to.
And so we're going from the numbers from 1 to 7 is what we're printing off.
All right, So in summer we talked about control structures and looping, and we gave some examples of how to use both the loop construct and how to build our own loop. Using the compares and the jumps.
Looking forward, we're gonna do a full on loop example using the loop command, and then we'll talk about rotation and shifting.
If you have questions, you can contact me at Miller MJ at you and Kate. I e d u. And you can find me on Twitter at Milhouse 30.