[MUSIC] Hello again. Now that we know how to implement conditions in assembly, let's see how we implement loops, which is going to involve conditionals. So here I show you a simple example of a while loop that keeps executing the loop body. As long as the condition holds, and in this case the condition is, while sum is not equal to 0 it keeps executing, okay. and when sum is no longer not equal to 0, it, the, the loop is going to stop. So, the important thing to note here in our implementation of a loop in, in, in assembly is first, there's a con, unconditional jump here that jumps to the beginning of the loop. And second, there's another jump here that determines when the loop is done. When this, in other words, when this condition no longer is satisfied. So when that happens, we're going to jump to the end of the loop, because the loop is done. So we're going to jump past the loop. 'Kay, and how the compiler transforms implements, other loops should be straightforward. The only trick is where you put the conditional branch. At the top, you're going to put either at the top of the loop, or at the bottom of the loop. For example, if you're to implement four, i equals 0. It's going to be looping to i let's see y is greater than 100 or equal. And you're going to keep implement incrementing i. So before the loop starts, we're going to set i to zero. We're going to have a condition that checks while i is less than 100. And in the body of the loop, we'll keep incrementing i. And then we'll jump out of the loop, as long as this condition is no longer satisfied. So let's start with an example. Start with a do-while example. The way do, do-whiles work is you have here's, here's an example: we say do, and then the loop body and then we check our condition. So the loop, the, the first iteration of the loop is always executed, no matter what. Because you know, we're going to execute at least once before we evaluate the condition. If the condition is true we just going to jump back to the beginning of the loop. Now we can look this in the form of Goto's right? And it's pretty simple, we have a label at the beginning of the loop. We're going to execute the body of the loop. Evaluate the loop condition. If it's true, we jump back to the beginning of the loop. And if it's no longer true, this, this goto loop is not going to happen. So that means we're going to jump, get out of the loop. Note that we have what we call backwards branch, branching backwards to continue the loop. And only take this backward branch while the condition holds. So let's see how we compile this example. Here we have our loop using the Go To version, okay. And here's our assembly version on the right. This is just setting eax to 1, and loading x into edx. and this is, this .L11 is just a label for the beginning of the loop. Here we're executing the multiplying structure for this multiply here. We are decrementing x with this instruction. And now, we are comparing, whether edx how it compares to one. Okay. So, now if this compares and says edx is greater than one, we're going to execute the loop we're going to jump up to dot L 11, otherwise we're going to jump out of the loop, okay? That's pretty simple right? So here's how to think about "Do-While" translations in general here is the C code, we go to the Goto Version, we going to have a label the beginning. And we're going to have a lift that checks whether the tasks for the loop holds and if it holds, we just back to the beginning of the loop. Pretty simple, right. So let's see how whiles, while loops work now. There's, there's likely different. Know that now we have while at the top, this is no longer do while loop, it's a while loop which means that even if for the first iteration, we have to check whether it needs to be executed. Or not. Okay? So, what this is doing while x is greater than one, we're going to execute the body of the loop here. And now, the biggest difference here is this, that you might have noticed, is this go to middle here. What is middel doing? Well, middle is the middle of the loop and that's where the condition to jump back to the beginning of the loop is evaluated. Okay? So, we jump to the middle before executing the first intsr, the first iteration, precisely because we need to evaluate it to see even the, to see whether even a first iteration needs to be executed. And if so, it jumps to loop, and it keeps going. Okay. So do we think about this, the first situation jumps over the loop body because we don't want to execute it unless the expression holds. The expression for the loop evaluates. So let's look an example here. So similarly to the do while, we have The do while example. We have multiplication that's implementing this multiplication here. The decrement here which is implementing it's decrementing x. now, here this is where the comparison happens. We're comparing edx with, with one. And if it's greater than, we jump back to the beginning of the loop. The main difference here is that now we have. A jump to where the condition is, right here, where, where the condition check is. Alright, so that's how we implement while by just jumping over the, the body of the loop compared to do while. Now let's see how you implement of for loop. A for loop essentially has bounds in the number of iterations of the loop. And in this example here, we are doing we are implementing a function that raise, that takes x and p as parameters and computes x raised to the p power. It computes this value here, x Raised to p. Okay. So there's a clever algorithm here that exploits a bit representation of p. That encourage you to stop and take a look. It's, it's really great. and so it's pretty clever. And so the way it works is it takes as many steps as the number of bits in p. The number, the numbers of bits, bits necessary to represent P, okay? And then for every bit of P that's set to one, we're going to the multiplication and keep doing it again. And that's why it's called the Square and Multiply. I don't want to get into the details of how exactly it works and why it works, but encourage you to stop, take a look at the slides and and think about it, okay? Here's how we can think about our For example I just showed you. So here's the, the code that I have just showed you. And here is a general form of four loops. And the general form looks as follows. There's an init expression that's executed in the beginning of the, before the loop starts. Kay? So that's what we have here. And the test expression is the expression that determines whether the loop keeps going or not. And, so, in our example here, is whether P is not equal to zero. And then there's the update part, that happens at the end of the loop. So, in our example is P equals P shifted right by one. That's the update part of our, of our report. And the, obviously this is the body of. And one way to make you understand how, how for loops are translated is to translate them to while loops. Okay? So, here's a general form that I just showed you. You can think about an, a while version of that loop by executing the init expression at the beginning of the loop. We have a while loop that executes while the tasks expression ports. We execute the body of the loop, okay, and then at the end, right after the body you execute the update for what we call the induction variable, and then when we go from a while we can again do the goto version that we had before. Okay? So, and note that the main difference here is that now we have init, here, and we have update. That's the main difference between for, and a while. So, let's see how we apply this to the example that I just showed you in the previous slide. So here's the general version of the loop and the goto version, here's the code of our example and note that the Init here now appears in the beginning, even before you evaluate the expression, whether the loop keeps going or not. So now we have to go to middle here, the same thing we had in while. That's where we're going to jump to evaluate whether the condition holds, okay? And then if the condition holds, we jump to the beginning of the loop, to execute the body, which is what we have here. Okay? And now there's one extra step. So, we have the init here, the, the, sorry the update. the update for the induction variable now happens at the end of the body of the loop right there. So it looks, looks very similar to a y loop. See you next time.