In this lesson we're going to look at how the compiler acts on conditions.
Let's go over what we said about the P.C. in the previous lesson.
As you remember we said a piece these times for the program counter and that it stores the address of
the next instruction to be executed.
Yeah we are going to look at how that happens.
Also remember our previous program worked in an incremental linear waterfall manner.
Here we are going to implement conditions and see what branch and happiness in the disassembly view
and see how the compiler works with that show.
We're going to start by modifying our current code which looks like this currently.
Remember we started by just increment and with our decrement and just with a plus plus contour and here
we have eight of them.
Meaning we add in one to counter eight times if it comes eight.
After that as it's implied.
So instead of typing this out eight times let's just put it in a loop and see how the compiler would
deal with this loop although the results would be the same.
Let's observe in the disassembly view how the compiler works with that we can start by just wiping all
of this off.
This is eight of them and we could just use a while loop like we have here and see while the counter
is less than eight and then we can put what we want which is plus plus counter
so this should give us the same resource and let's see.
So let's just exit the debugger and run this code by compiling it first and then back to the debug view.
Okay.
So let's step in and see first of all let's just check whether the Locos view gives us the value eight
at the end.
OK.
It doesn't give us eight.
It ends at seven.
This is because as it says here not in scope the variable is out of scope.
We can just cut this from here and put it here and then it should be scope.
Now let's exit this and then come back and then go back to debug.
Okay.
Now let's try this step.
See now we have the value eight which means it gives us the same resource over here.
You can see that.
Let's change it to decimal form here.
We have eight here.
Now let's go back to the top and see how it works its way to the answer eight in the disassembly view.
So let's observe what goes on in the disassembly view.
First of all we need to get it I wrote back to the beginning of the main function and we have to do
that by exiting the debugger and coming back and I will explain why we have to do that in a second.
Let's exit with this button here and then click the same button.
Okay to come in here.
So as we can see currently it's pointing here.
This the instruction that's going to be executed.
We already dealt with this.
So we're going to step.
Line by line and see what goes on into disassembly.
You would click to step with step.
It's executed here.
The next instruction to be executed will be to branch into this while fun this while loop.
Remember this phase while loop is the program's loop.
This is what keeps the program running all the time.
In fact the while loop we are interested in is the second one here.
That's the one that keeps adding the value one to the variable counter the next instruction will be
to branch to this while loop and the B here stands for branch.
And then this address is where the wire loop is.
So let's step in now with enter the programs while loop the next instruction will be to branch into
the while loop of our variable counter.
So let's go.
Yes.
Now we are in our while loop and we can see so far counter is there was 0 0.
We are here because we've executed this already counts here.
In fact like we did previously this just changed it to a decimal so few.
So let's step again which step counter is 1.
What happened was like we had in the previous lesson RS zero of course are zero plus one does Holcomb
counter became one.
Remember in the previous lesson anytime we stepped we had our share of plus one.
So after you had kept incrementing mean in the first one gave us one the second one gave us to the third
one gave us three.
That's what went on.
It was always our zero answer a one after you answer one.
Now let's take a look at this one here.
Let's step and see rotted and having eight different add s up codes to perform the additions over here
we have just one at s up code and this one address code works in conjunction with a C and P and b L2
up codes to perform all the eight additions when we increment the value is first added as we see here.
And then to see MP which is to compare instruction this new forward and how the campaign structure works
is that it uses the means of subtraction to make its decision as we see here.
Counter has the value to and over here our loop says the counter should keep increasing as long as the
value is less than eight.
So what compares to acids.
It takes two and then minus eight.
And at this point the answer compare gets it's minus six because two minus eight is a negative six.
And when this happens the AP S.R. register we spoke about it's updated.
Remember we said the AP is our house the N C C and C bit the N stand for the negative bit dizzy for
the zero bit C for Curry bit and V for overflow bit.
So the negative bit of the AP s our register is updated because the results from the C MP is a negative
number.
So after that branch which is BLT this BLT here you see is a variant of the branch instruction.
This BLT is moving forward and the way this P L 2 works is that it goes to the AP s register and it
checks the various bits and it makes its decision based on that and what happens is when in the AP s
I register D The and it is set or enabled the BLT modifies the P.C. the program counter to branch to
a different location rather than allowing the P.C. to work in its own orderly manner.
And as we said the result of this C MP modified the end bit.
Therefore this keeps happening until we hit the number eight and when we hit the number eight will return
to the beginning of the loop.