Any but the most trivial programs require flow control to control how code executes. In assembly this is accomplished through the use of conditional jumps that select code to execute based on the status of the processor flags from the previous class.
In today's class you will:
cmpl
and testl
je
, jne
, and more
ssh username@hopper.slu.edu
wget http://cs.slu.edu/~dferry/courses/csci2400/asm/flow.s
In order to create conditionally executed code, we first have to understand how program execution can be modified. Assembly programming doesn't have high-level constructs like if-else statements or for loops. The only tool available to modify the way a program executes is jumps, which would be considered goto statements in higher level languages.
To use a jump, you need to define a jump target (or label). For example, consider the following simple program:
main: pushl %ebp movl %esp, %ebp movl $0, %eax target: call print_eax incl %eax jmp target leave ret
This program starts executing at the start of main, and continues executing
through the target:
statement. That target:
statement
does not affect execution at all- it simply gives us a way to refer to that specific
section of code. Thus, the program skips directly from executing the movl
instruction to executing the call
instruction. However, when the program
later gets to the jmp
instruction, the program then jumps back up to the
target:
line and begins executing again from there.
cmpl b, a
This is a useful all-purpose instruction for comparing two signed or unsigned integers. Under the hood, this instruction computes the result of a - b without storing the result. Then, If a == b then ZF is set to 1. If a < b then SF is set to 1, and if b < a then SF is set to 0.
Write a short bit of code to test all three situtations where operand A is
less than, equal to, or greater than operand B. Use the print_all
function to confirm your results.
je
is a jump if equal instruction. In the following code the program will
jump to label_1
if the contents of EAX and EBX are equal.
cmpl %eax, %ebx je label_1 js label_2 #execute section 3 jmp next label_1: #execute section 1 jmp next label_2: #execute section 2 next: #continue executing
What C-style construct does the above code correspond to? Write a bit of pseudocode giving the high-level behavior of this program. Ask the instructor if you're unsure about the operation.
compare
. Finish writing this function so that, when called,
it prints the message:
Testing A=%d and B=%d
Followed by one of the messages:
A is greater than B
B is greater than A
the arguments are equal
as is appropriate.
testl b, a
instruction is also used to compare values. This
instruction computes the bitwise-AND of operand A and operand B. If this value is
zero then it sets ZF to one, and otherwise ZF to zero. You can then use the
jz
(jump if zero) and jnz
(jump if not zero) instructions
to control your program.
One special use of testl
is as a loop index. The instruction
testl %eax, %eax
will set the zero flag only if the register %eax is zero. Use the testl
,
decl
, and jz
instructions to create a loop that prints the values from 100 to
1 in order from highest to lowest. Feel free to use a function such as print_eax
to accomplish this.