 In this video, we're going to show how to translate a conventional if-then-else statement as the one that is present in programming languages like Java, but we're going to translate it into AVR assembly code. The first thing we need to know about the if-then-else statement is its syntax and semantics. So the syntax is very simple. We have the keyword if followed by a parenthesis. And inside this parenthesis, we are supposed to write a Boolean expression. This Boolean expression, when evaluated, has to return only two possible values, either true or false. And for the sake of simplicity in this video, we are going to consider that true is going to be equal to different from zero, and false we're going to represent it by a value which is zero. After this expression, we have the possibility of enclosing in braces one block of code, which we are not going to write the details about. We're just going to call it the then block, meaning that an arbitrary set of additional statements can be inside that block. And also we have the possibility of closing this brace, write the keyword else and open another block, which similarly we're going to call it the else block. This is the syntax of a conventional if-then-else statement in a programming language. The semantics, what it means is that after evaluating this Boolean expression, if the result is true or in our case it's a value different from zero, we know that the then block it's going to be executed and the else block it's going to be ignored. And vice versa, if the Boolean expression returns the value false when it is evaluated, this block is then ignored and only the else block will be executed. So how do we go about translating this to assembly code? So let's assume first that we are in the middle of an assembly program, some previous instruction, and let's assume that we have here some specific instructions. I'm referring to now machine instructions, instructions that calculate the evaluation of the Boolean expression and therefore we have here a Boolean result. Now how many instructions do we need for this? It's difficult to calculate in advance, especially because this Boolean expression here can be arbitrarily complex, but let's assume that all these instructions produce this result and for the sake of this example, let's assume that the result is stored in register R18. The next instruction that we would like to write to translate this to assembly code would be compare the result of this expression R18 to the constant 0. When we do this comparison, what we are doing here is trying to detect or trying to decide if this expression has been returning the value true or false. And this is perhaps the most important part. Afterwards, we're going to do a conditional branch. Branch is equal to a label which we're going to call else. So what is happening here is I'm comparing the result of this expression with 0. If it is 0, it means that the result is false. If it is false, it means that it has to skip this block, so I'm going to jump conditionally to the block where the else code is present. Otherwise, if the comparison of these two numbers is different from 0, this jump is not going to occur. And therefore, I'm going to execute immediately some other instructions which we're not going to detail because we haven't detailed them here, but they implement the dem block. Otherwise, if the result is equal to 0, what we want to do is jump to a location in the code which we denote with a label, and it's going to contain the instructions that implement the else block. There's only one little tiny detail, additional detail that we need, which is that if the Boolean condition has evaluated to true, and therefore this comparison says that it's different from 0, and therefore this branch is not taken, after executing the dem block, we cannot let the microprocessor proceed and execute the else block, we need to jump this block. So we need an additional label here, let's put it the name done, and an additional instruction which is an unconditional jump to that location. Now as we can see with this structure, we have replicated in assembly code the high level statement if then else. Let's see a little bit more complex example. Let's assume a bit more complex condition. For example, if x is bigger or equal than 15 and x is different from y, then we have the dem block here, else we have the generic else block here. Now the main difference with the example we just discussed here is that we complicated the Boolean expression. This is just to highlight that these instructions here can be as complex as required by this expression. But not only that, we're going also to highlight how machines actually evaluate this expression. So the first assumption we're going to make is that both x and y are integers, let's say 8 bits in the ABR architecture and stored in memory. And we're going to assume they are stored with labels equal to x and y as well. So the first instruction that we would execute to start evaluating this expression would be those required to evaluate this sub expression here, x, bigger or equal than 15. So the first instruction would be load from memory into the register R18, arbitrarily chosen, the value of the position in memory labeled x. So this would bring the value of this variable into register R18. The next thing I would do is compare this value I just loaded with the constant 15. And this can be done with instruction CPI, compare immediate. Now an interesting thing happens here. After comparing x or R18 in this case with 15, if this condition is false, since it is a sub expression of a conjunction, a false condition here would already tell me immediately that I need to execute the else block. Now this condition being false means that I would branch if R18 is lower than 15. So BRLT would be the ideal instruction to put in there. And it would make it, it would make me jump to the else block. So here I have one portion already of this implementation. If this condition is false, I know that the code should jump and execute the else block. If this condition is true, however, I'm still not certain that I should execute this block or this block because this is part of a conjunction and I need to evaluate the other sub expression. Now x is already in register R18. What I need to do is now bring from memory, again with an LDS instruction. Let's put it in register R19, variable Y. So I'm doing the same as in this instruction, bringing the value from memory. And what I need to do now is compare these two registers, R18 and R19, which is equivalent to comparing X and Y. Now the other interesting thing here is the following. If this condition now is false, again I need to jump to the else block. This condition being false means that I need to branch if this comparison returns an equal result. So in this case, BREQ would be the ideal conditional branch instruction, and it should jump also to the block else. And if this condition is true, if X is equal to Y, then I am in the situation in which both of them are true. And therefore, immediately after this instruction, I would include as many instructions as needed to implement the then block. And the remaining code here will follow exactly this pattern we just described, which is I'm just put an inconditional branch after I'm done. And immediately afterwards, my label else with my block else block. And then finally, my label done. So again, what we have implemented here is these instructions are basically calculating the value of the expression. When I'm comparing R18 with 15, I am basically evaluating this sub expression. When I'm comparing R18 and R19, I'm basically evaluating this sub expression. The fact that it's a conjunction here allows me to sequence that these instructions specifically this way, if this expression would be different, then I would need a different set of instructions. But eventually, at this point, what I know, at this point in the code, is that these two sub expressions are true, the conjunction is true, then block is executed, jumping conditionally to done. Otherwise, if any of them fail, I know I can jump directly to the else block as these two instructions are saying, execute the else block and finish.