Tue, 12 Oct 2021, 19:11 Re: Condition codes
A student writes: > Hey Dr.Patt, > Hope you are doing well. Why do instructions other than Branch set > condition codes? Like I am just confused what they would use them for and > how they even set them. > Thanks, > <<name withheld to protect the student confused about condition codes>> In carrying out the work of a program, it is often useful to execute one instruction next if some condition is satisfied, and a different instruction next if that condition is not satisfied. In the example we spent most of yesterday doing in class, we wanted to jump out of a loop if the character we were examining was the End_of_Text (EOT) character, and execute the loop again if the character we were examining was not EOT. Also, in the example yesterday, we wanted to execute one instruction to increment R2 (a counter) if the character we were examining was the same as the character someone typed, and not increment the counter if the character was different from what someone typed. The point is that in executing the instructions of a program, it is often the case that we create a value and then do two different things depending on that value we have created. Our decision as to which of the two paths we take is based on whether the value we create is negative, zero, or positive. In most ISAs, this important concept is handled by condition codes. That is, we create a value and at the end of the instruction that creates that value, we set the condition codes depending on whether that value is negative, zero, or positive. We then follow that instruction with a branch instruction which examines one or more of the condition codes and changes the PC depending on which instruction we want to execute next. Consider again the two examples mentioned above. With respect to checking if we have just fetched the End_of_Text character, the ADD instruction will produce 0 if the computer has fetched EOT. Since during the fetch phase of the ADD instruction, we have incremented the PC, if we check the Z bit and if it is 0, we know we have not fetched EOT, and so we do not change the PC and the next instruction executed is the start of our work with the character we have fetched. On the other hand, if Z = 1, we know we have fetched EOT so we do not want to do that work again, but rather we want to change the PC so we can go on to the next part of the program's execution. Does that help? Good luck on the exam. YNP