All processors provide a set of flags that are used to convey important information about the result of processor operations. For example, a frequently used flag is the zero flag which indicates whether the result of the last computation resulted in a value of zero. Understanding what these flags represent and how to use them is critical in writing anything beyond the most pasic programs- if/else statements, loops, and more complex structures all depend on these flags.
In today's class you will:
ssh username@hopper.slu.edu
wget http://cs.slu.edu/~dferry/courses/csci2400/asm/flags.s
print_zf
print_cf
print_of
print_sf
I've also modified print_all
so that the above four
functions are called. However, be warned that the print_sf function
is not finished yet! That's your job.
Call the print_all
function and take a look at the new
additions.
movl $1, %eax
subl $1, %eax
call print_all
Flags are said to be ON or OFF. They're also said to be SET or UNSET or equivalently 1 or 0. What will be the state of the zero flag in the program printout above? Test it and copy-paste the results into your text file.
Suppose that X is an immediate value in the code below:
movl $0xFFFFF000, %eax
addl X, %eax
What values of X would result in the carry flag being set?
What is the maximum 32-bit signed integer, given in hexadecimal? (Hint: Which bits of a signed integer contribute positive value? Which bits contribute negative value?)
The last part of this exercise is to finish a function that prints the
value of the sign flag, called print_sf
.
You can find the partial function around line 200
of flags.s
. In doing so, feel free to heavily borrow from the
function print_zf
, which prints the zero flag, right above it.
Unfortunately, you can't access these flags directly. You can only access an entire 32-bit wide register full of all flags called the EFLAGS register. Then, you can use a bitwise-AND to select just the bit you want, and then use a right shift to move that bit into the least significant place so that, when printed, the value is either zero or one. The Sign Flag is in the 8th bit (0x80). So, to be explicit:
pushf
.
pop
instruction.
Once your function is complete you can call it directly or it will automatically
be called as a part of the print_all
function.