 In this video, we're going to explain how to translate a for statement to AVR assembly code. So the first thing we need to know is the syntax and the semantics of the statement. The syntax is the following. We use the keyword for and then in parentheses, we'd write three blocks, which we're going to call in it, which will contain certain instructions separated by semicolons. The following one, we're going to call it Boolean expression. And the third one, we're going to call it final. Now the interesting property is that the Boolean expression can only return two possible values, either true or false. And for this example, we're going to assume that false is going to be encoded as zero and true is going to be encoded as different from zero. Now the for statement allows us to write arbitrary coding between curly braces. And typically, this code is referred as the body of the loop. And then we close the curly braces. Now the meaning of this statement is the following. The code included as part of the init block is executed only once before the rest of the statement. And then the statement is repeated, evaluating the Boolean expression. If the Boolean expression is true, the body is evaluated. And at the end of the evaluation of the body, the final block is then evaluated. So we have to be careful because despite the fact that the final block is here in this corner of this parentheses, in fact, it is as if it were placed in this final position right after the body. Now the assembly instructions that we're going to use to implement this structure here will start, obviously, by a collection of instructions as complex as needed to evaluate the init block. So this block over here will require a certain number of instructions depending on its complexity. Then the next block is going to start with the label eval and we are going to include here the instructions required to evaluate the Boolean expression. Remember that we can only get two possible results, true or false. We're going to assume in this example that the result is stored in R18. So after evaluating this Boolean expression, we compare CPI, the value of register 18, with zero. If these two values are identical, it means this condition is false. If it is false, then we can put a conditional branch P R E Q to a label that we're going to call done, which will be placed at the end of the whole structure. Otherwise, if this comparison is not zero, it means that this expression is true. It needs to be followed then by all the instructions required to implement the body of the loop, whatever it takes. The two final things that we need are the following. After evaluating the body, what we said is that these final statement needs to be evaluated. So we're going to put another block of code here as many instructions as needed to execute the final block. And finally, after we are done evaluating this final block at the end of the body, we need to go back and evaluate the Boolean expression again. So we're going to put an unconditional branch, jump to the label eval, and the label done, which is the destination of this branch will be at the end of the overall structure. Let's illustrate this structure with an example. Let's suppose we have this fairly common construction in a programming language from I equals zero, let's say I less than 15, I plus plus. And let's assume that I is an 8-bit integer, which is a store in memory, in allocation with the name I. Let's suppose we have an arbitrarily complex body here, which we don't care about. We only care about the surrounding code. And let's see how do we translate these to assembly code. So the first statement we need to execute is this one in which we set the variable I, which is in memory, to the value zero. So this could be implemented. One possibility is to first clear some specific register, let's say R18, and then store that value in the location of variable I. So these two instructions would be the equivalent of I equals zero. Afterwards we said that we put the label eval and then the code to implement the evaluation of this expression. In this case, we can proceed to bring again I from memory, store it in R18, and then compare that register 18 with the value 15. Now if this condition is false, we know we have to skip the body and terminate. And this is the same as choosing a conditional branch. This condition being false means that I is greater or equal than 15, therefore we can choose the conditional branch PRGE and jump to the label done. Immediately afterwards, we place instructions that would be executed if this condition is true. If this condition is true, this branch is not taken. We end up executing here all the instructions required to implement the body of the loop. And finally, we need to translate the statement that is part of the final block. In this case would be bringing again the value from memory. We load R18 with I. We increase in one unit R18 and then we store it back in memory. And this would be the instructions required to implement this final block. And as we said here now, we just insert an unconditional jump to the label eval, which is here, followed by the label done, which will be the destination of this branch. Now when we look at these instructions, we can think immediately of a few optimizations like for example we are bringing R18 to memory to be storing I and then we load it back again. There could be a little bit of an optimization we can do here, but we have to be careful because this instruction not only is executed after this one, but it's also the destination of this jump. So we don't necessarily need to have I storing R18, although we go back, we see that this instruction here, which is the same as this one, is guaranteeing us that we are bringing the value of I into R18. Therefore it could be an optimization to remove this instruction and leave it as using R18 because if we come from this place it is already loaded and if we come from this jump it is also loaded by this instruction over here. These are the type of the optimizations that are done by compilers.