 In this video we're going to explain how to translate the while loop statement in a high-level programming language to AVR assembly code. Most important ingredient is first to know how do I write a while statement and what does it mean. The while statement is written as follows, the keyword while, and then in parenthesis we need to write a Boolean expression. This Boolean expression can only have two possible results, either true or false. And for this example we're going to assume that false is encoded as a zero and true is encoded as anything different from zero. After this Boolean expression in parenthesis what we can include is with curly brace what we typically know as the body of the loop. And we finish the curly brace. And the interpretation of this construction is that this curly brace is surrounding a chunk of code that is going to be executed repeatedly until this Boolean expression is no longer true. So the program will first read the Boolean expression, evaluate its value, and if it is true it will execute the body and go back to evaluate the expression. Now in assembly code this is going to translate generically as follows. First we're going to assume that we have some previous instructions. We are going to define a first label which we call eval. And this eval is going to point to the set of instructions which could be arbitrarily complex to evaluate the Boolean expression. The Boolean expression specify here. And again this could be arbitrarily complex and arbitrarily long. Let's assume also for the sake of this example that the result of the evaluation of that expression has been stored in R18. Register 18. So what we do next is compare register 18 with immediate value zero with instruction CPI. And what we do now is a conditional branch. We're going to branch if these two registers are equal. It means that this Boolean expression has evaluated to false. Therefore we need to branch if they are equal to another label which we are going to place at the bottom of this construction which we call done. Immediately afterwards this instruction if this branch is not taken it means that this expression evaluated to true. Therefore we need to insert here in the middle whatever code is necessary to translate the body of the loop. And finally as we said before as soon as this body has been executed what the loop has to do is go back to the evaluation and repeat the evaluation of the expression. So this is an unconditional jump that is going to bring us back to eval. And immediately after this instruction we can put the label done which is the destination of this jump. Now depending on the way we want to organize our code this is one possible implementation of the while loop. There is another one with equally the same functionality which starts by first placing the body of the loop or the instructions that implement the body followed by the eval label and the instructions to evaluate the Boolean expression. And then again we compare CPI R18-0 but in this case the conditional branch is going to jump if these two are different. It means the program has to go back and execute the body. Therefore the conditional branch is going to be BRNE and it's going to go to the label body. And if we want for the sake of similarity we can write the label done here. There is one little tiny detail though. If we imagine an assembly program executing one thing that is incorrect is when we reach this point we shouldn't execute the body. We should in fact execute first the instructions to evaluate the Boolean expression and then go back to the body. But this is very easy to fix with an unconditional jump here to eval. Now with this instruction here we replicate exactly the same behavior as we said here. This would be option number two. We jump to the evaluation and if the evaluation is true it means different from zero it means this branch will take us back to the body. So we execute the body and we execute again the instruction to evaluate the Boolean expression. Construction number two has the advantage that if we implement instead of a while statement at do while statement, the do while statement is exactly with the same structure except we remove the unconditional branch at the beginning. Let's now see an example. Let's suppose we have to translate the following loop with a little bit more complex condition. While x is different from three and x less than y we have here some code let's say body and we close the curly brace. So what would be the sequence of instructions to translate this loop structure into assembly code? And again we're going to assume that x and y are a bit integers and they are stored in memory in allocation with a label exactly with the same name. So the first thing we should do is to start evaluating this sub expression here. So we would do LDS bring from memory stored in R18 the value of x. Next thing we need to compare we need to evaluate this sub expression first. So we need to compare with the constant three. So the CPI R18 three. Now the interesting observation since this is a conjunction if these first sub expression is false we can conclude that we should jump out of the loop already. This sub expression being false is equivalent to say branch if they are equal to the place where we're done. So this conditional branch means if this value of x is equal to three this is false get out of the loop. Otherwise if this branch is not taken what we need to do is proceed to evaluate this sub expression. Now for this sub expression we need to bring from memory the variable y which we do with another LDS instruction. Let's put it for example in R19 y and now we proceed to compare R18 which has x with R19 which has y and in here the reasoning is similar. If this sub expression is false then we should get out of the loop and jump towards the end the same thing we did here. In this case this expression being false it means we need to branch if greater or equal if x is greater or equal than y which is the opposite that we're doing here then we jump to done. So this conditional branch will occur when x is greater or equal to y which is precisely what makes this expression false. If this branch is not taken it means that both sub expressions are true the conjunctions makes them true and therefore now we insert here the body of the loop. And finally as soon as we are done evaluating the body as we mentioned before we need to go back and keep evaluating the expression so we put jump to the label evolved which we are going to place at the beginning of these instructions. And the only thing missing is to stick the label done which is the destination of these two jumps in case any of these conditions is false. This sequence of instruction then implements the behavior for this Y loop in the AVR architecture.