CS2.201 - Computer Systems Organization | CSO Lecture 9 notes

with Prof. Avinash Sharma
Jun 11, 2021 - Friday
Written by: Yash Mehan

Condition Codes

Usually the program counter increments by 1 instruction. Now we wish to modify the flow of control based on certain conditions. There is a set of single bit flag registers which describe the status of a recent arithmetic operation. Whatever the conditions evaluate to, the result is updated in one of these flag registers; and based on content of these flag registers, the flow can be directed.

The FLAGS register has 16 bits, where each bit serves as the storage for the result of different conditions/operations. The successors of FLAGS is EFLAGS, which is 32 bits wide, and RFLAGS, which is 64 bits wide.

The commonly used flags are:

Example:

cmp

cmp can set all these four flags. It merely subtracts the operands passed to it and on the basis of the difference updates the flags, but doesn’t update/modify the content stored at the registers/locations which were passed as operands. By difference, we mean

cmp left_op, right_op # means right_op - left_op

Using Condition Codes

Accessing Condition Codes by set family

You just ran an instruction for comparing two operands and and you need to check the result. i.e. if one of the operands is $>, \geq, <, \leq, =, \neq$ the other operand. Or maybe if the instruction gave a negative output. D is a 1 byte location in memory, or a 1 byte register %al or %bh etc.

The following instructions interpret the bits to be in 2’s complement form

Example: consider two 3 bit numbers, b which has 100 and a which is 010. Interpreting them as signed, b is -4, and a is 2. So we should get a true for b <= a and b < a.

Doing b - a which is adding 100 and 110 we get 010, and 1 as an overflow. Now,

We can interpret 100 and 010 as unsigned and draw a similar analogy. Here the CF will be 0.

jmp

Two common encodings: Relative, where jmp is given how many bytes to skip to the next instruction, the other is absolute, which gives jmp a 4 byte long absolute address.