x86 Assembly: Part Two-- Flags

CS 301 Lecture, Dr. Lawlor, 2005/09/26

The one thing you need to be able to do HW3 is the ability to collect input from the user.  I've added a "read_input" subroutine to NetRun that should allow you to enter input.  To use it,
  1. Say "call read_input" as the FIRST LINE in your assembly program.  This routine may trash (i.e., change) all other registers, so be sure to call it before doing anything else!  read_input, like any subroutine, returns its value in the %eax register.
  2. Use %eax and do whatever computation you need to do.
  3. In the NetRun GUI, click "Input Data": "Enabled" to display the input data edit box.
  4. Enter the program's input data--just numbers--in the input data edit box.
  5. Hit "Run!".  The program will read the input you've typed in.
You can call "read_input" from C or C++, too--it takes no parameters, and returns the integer read in.


The "EFLAGS" register on x86 stores a bunch of flags, as shown on page 37 of the Intel arch manual Volume 1.  The important flags include:
You've also got to be aware of which instructions set which flags.  For example, the "cmp", "and" (bitwise AND), "sub", and "add" instructions set all the flags; "inc" (increment by 1) and "dec" (decrement by 1) set everything but CF; while "mov" and all the jump instructions don't mess with the flags.  It's easy to accidentally overwrite flags you care about if there's too much stuff between the time the flag is set and the time it's read!

You can actually look at the flags with the "lahf" instruction, which copies the important bits of EFLAGS into %ah (bits 8-16 of eax get  EFLAGS(SF:ZF:0:AF:0:PF:1:CF)):
call read_input
cmp $-3,%eax
mov $0,%eax

Note there's NO way to get at the flags, or to directly call the flag-using instructions in C!  None!