04 Conditionals and Control Flow


[MUSIC]. Hello again. Now we're going to see conditions in control flow. And if you recall control flow is the order in which executions instructions of your program execute in the processor. And the way you change the flow of controlloing your program in writing code in a high level language is using control statements like if. While for loops, and do of for loops and for for loops, and for loops as well, these are all ways in which you can control in which order how the, the control flow goes in your program. And a conditional branching instruction which is a branch is an instruction that just changes the control flow. And if you make the control flow conditional which makes it a conditional branch, it turns out that that is suf-, sufficient to implement most of the control flow constructs offered in the high, in high level languages like the ones I just told you about. and uncon, unconditional branches, which is branches that just execute no matter what, regardless of the condition, are used to implement things like break and continue. Breaks just breaks out of a lopp and continue goes back to the beginning of a loop. Those are unconditional, they happen ayways. Is, okay? and so we, we use unconditional branches for those. And in x86, we refer to branches as jumps because it jumps, you're executing, it jumps to a different part of your program. Okay? Either conditional or unconditional. So here are the jump instructions available in x86. They're in the from of, they start with the letter j for jump. Huh, and x is a various ways of doing jobs. For example if you do just "jmp," this means jump, that's an unconditional jump. And it's also going to take as a parameter, it's going to take as a parameter a instruction address that it's going to jump to. 'Kay. So here's another one. If you do "je" you mean jump if equal. Or zero. And that's going to be determined by this special condition code register called zf. Okay, so let me pick out an example here. if you use jl, it's going to be jump if last then. And so if you do a less than, if you do a comparison and it turns out that it's less than. this, the jump's going to happen. Okay, I'm going to, we're going to show examples of that later. And I encourage you to read in the book all of about how all of these instructions work. let's look at the, the processes of state again. Remember that we have the registers that we've been playing with already in our exit, in our assembly programs. These registers are special. Again, there's the stack pointer and the base pointer. And the instruction pointer tells what instructions are going to be executed next. And note that what do you think is going to happen when you execute a branch instruction? You're actually going to change the value of the instruction pointer because it might change. What instruction is going to be executed next. So branch instruction potentially changes the value of the instruction point. And finally the thing that is very important to note here is that we have this forest wall single bet which is the condition codes that are set by some instruction. We're going to use that to implement provisional branches. That's why in this previous here slide I just showed you, you can determine the condition of whether or not the jump is going to happen based on these condition codes. Let's see now how, how these condition codes are set. The first way we're going to see is implicit setting. This happens implicitly when you carry out some Operations. So these are the single bit condition registers that we're going to be talking about, and they are implicitly set when you execute some instructions, for example an addition. So this instruction in an it, it, it performs the arithmetic addition operation, but in addition to that, it also sets some condition codes depending what happens to the results. For example, in the case of adds, the CF is set if, if there's a carry out from the most significant bits, which is essentially an unsigned overflow, an unsigned overflow happening the addition. Okay? So the ZF condition is set if the result happens to be equal to zero. The s, the signal flag is set depending on the result. If the result is negative then sf is is going to be set, means it's a negative number. Okay. And now of happens if there is a two's complement overflow. If when you add the number will be bigger than what fits in the register. With what it, you're going to have this, this bit set. And also be careful of using the LEA instruction. That also computes some, some expression, some arithmetic expressions. you do not set these condition codes, okay? So be careful with that. That's the implicit setting. The other way, is to set the condition explicitly. Using compare instructions. Okay. So for example if you do comp l here. Which compares a 4 byte number. And you pass, you pass two all parenthesis of parameters. Source two and source one. Okay so and what it does essentially. It effectively computes A minus B but without setting the destination. It just computes this expression. So that he can set. The condition codes appropriately. So, here the CF is going to be sad if the carryout from the most significant bit is sad. Okay so it's used for unsigned comparisons. So, the Z f is set, the zero flag set so that if A equals B, why is that? Because if A minus B equals zero, it means that a equals b. So the set's going to be set. It's going to be useful, this is useful for an equality comparison. Now, if a - b is negative, we're going to to set a sign flag. So you know that, then that this also means that a is less than b. And also the overflow is set, if there's an a two's complement sign overflow. Okay? Great. Let's see now how the, to set condition codes with the test instruction. Okay, so the test instruction also set to the same set of also manipulates the same set of Single bit condition code registers. And it takes two operands as parameters, source two and source, and source one. And essentially it works like computing a bit-wise with b. 'Kay, a being source two and b being source one. Without setting the destination. And that's, that's useful to do to have operands to be a mask because tasks with masks using the twice and is very, very useful. Okay. So, in this case here tasks if the zf flag is set if a b twice. And with b, happens to be zero. And it, SF is set, if a bitwise and with b is less than zero. So, if the most significant bit is set to one, essentially. So, now let's look at this example here. We're doing, essentially, eax bitwise n with eax. So, this is interesting because, zf is going to be set, only if eax is zero. Oh is essentially just, just checking whether eax is positive, negative, or zero. If it's zero, the zf bit's going to be set. If it's negative, the sf bit's going to be set. So that's going to be useful when you have, conditions like f a less than zero. 'Kay, so this is going to be implemented with a test instruction. We can also read condition codes. Okay, in this, set of instructions of, called set in a bunch of options here. And what they do is they read the condition codes and put, and put the resulting value at zero or one, the whole by zero or one. And in a general purpose register so you can actually read and, and do computation with the contents of the condition codes register. So essentially you get value of condition code register and you store it into a general purpose register. And I encourage you to look at the book For examples of those. We're going to see one now, but it should read how each of these instructions work. So let's in example now of using the site instrucions to read condition codes. So, here we have, we have a function called GT, that just takes X and Y as a parameter, and then we want to return, whether x is greater than y. And here's the bud of the instruction. Here, what we are doing is getting y and storing it in eax. And now we are comparing y with x, because x is stored in 8 from ebp. Okay? And now we're doing set greater in al. What is set greater doing? Set greater is going to get in, we passing a l here, which is the low order bytes of the e a x register. And what they're doing is the following. here we are reading y and putting e a x, comparing x and y. Essentially comparing, doing a subtraction and just setting the condition codes. Then al now is going to be set to to 1 if x is greater than y, and it's going to be set to 0 otherwise. 'Kay. And this instruction here just zeroes, this, this instruction just zeroes the, this instruction here just zeroes the rest of eax, it makes this whole here, thing here zero. Okay. So, in the end, this is going to, hope, this is going to be, al's going to be set to 1, then if x squared is in y, it's going to be set to 0 otherwise.

Wyszukiwarka

Podobne podstrony:
Causes and control of filamentous growth in aerobic granular sludge sequencing batch reactors
04 Emotions and well being across cultures
2007 04 Go with the Flow
Control Flow Graphs as Malware Signatures
04 Shifting and Sign Extension
Power Converters And Control Renewable Energy Systems
06a?0 Displays Indicators and Controls
04?ta and C
04 Shifting and Sign Extension
Model Railroader Basic electricity and control
06?0 Displays, Indicators and Controls
Design and Control of an Hybrid Transmission
Control flow
CNSS Safeguarding and Control of COMSEC Material
FIDE Trainers Surveys 2013 04 01, Georg Mohr Bobby Fischer and the square d5

więcej podobnych podstron