Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.

Similar presentations


Presentation on theme: "Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005."— Presentation transcript:

1 Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005

2 Conditional Branches Branching on a conditional expression will be done in two parts. Evaluate the expression, leaving either true (1) or false (0) on the stack. Test the value on the stack and branch on true. It would be more efficient to test the original expression and branch immediately. However, by separating the two tests, it will be simpler to write the code.

3 Conditional Branches Recall that the if statement is of the form IF ( expr ) stmt where expr is either zero (false) or nonzero (true). Thus, we must compare expr to 0. First, we generate code for a CMPNE tree that will leave 1 on the stack if expr is nonzero.

4 The CMPNE Tree CMPNE INT NUM 0 e INT Integer Expression Floating-point Expression CMPNE DOUBLE CAST DOUBLE e DOUBLE NUM 0

5 Integer Comparisons To compare two integers, we must Pop them off the stack into registers. Compare the registers. Push 0 or 1 onto the stack. There is only one compare instruction: cmp. It sets various flags, depending on the result of the comparison.

6 Flags CF, ZF, SF, and OF The relevant flags are CF – carry flag ZF – zero flag SF – sign flag OF – overflow flag

7 Conditional Jumps We then do a condition jump. A conditional jump checks the flags to decide whether or not to jump. MnemonicMeaningFlags jejump ==ZF = 1 jnejump !=ZF = 0 jljump < SF  OF jgejump >=SF = OF jgjump >ZF = 0 and SF = OF jlejump <=ZF = 1 or SF = OF

8 Integer Comparison The code for the integer comparison !=. mov $1,%ecx # Move true to ecx pop %eax # Pop right operand pop %edx # Pop left operand cmp %eax,%edx # Compare operands jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

9 Floating-Point Comparisons When we compare two floating-point numbers, they are already on the FPU stack, in st(0) and st(1). The instruction fucompp Compares st(0) to st(1). Sets condition codes C0 and C3 in the FPU status word. Pops both operands off the stack.

10 Flags C0 and C3 The result of the comparison is determined by C0 and C3. ResultC0C3 st(0) > st(1)00 st(0) < st(1)10 st(0) = st(1)01

11 Floating-Point Comparisons To perform a conditional jump based on a floating-point comparison, we must first move the FPU status word to the EFLAGS register. The instruction fnstsw %ax stores the FPU status word in ax (16 bits). The instruction sahf stores ah in the EFLAGS regsiter.

12 Floating-Point Comparisons The sahf instruction maps C0  CF. C3  ZF. Thus, we want to use conditional jumps that check CF and ZF.

13 Conditional Jumps MnemonicMeaningFlags jejump equalZF = 1 jnejump not equalZF = 0 jbjump belowCF = 1 jaejump above or equalCF = 0 jajump aboveCF = 0 and ZF = 0 jbejump below or equalCF = 1 or ZF = 1

14 Floating-Point Comparisons The code for the floating-point comparison !=. mov $1,%ecx # Move true to ecx fucompp # Pop left operand fnstsw %ax # Store status word sahf # Move ah to eflags jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

15 Floating-Point Comparisons One little complication is that fucompp compares st(0) and st(1) in the reverse order from what we would expect. Therefore, the meanings if are reversed and the meanings of = are reversed. So we reverse the roles of ja and jb, and jae and jbe.

16 Jump Trees There are JUMP trees and JUMPT trees. JUMP BLABEL 6 JUMPT BLABEL 6 CMPNE e.mode NUM 0 e e.mode

17 JUMP Trees A JUMP tree generates the following code. jmp B6 jmp L8

18 JUMPT Trees A JUMPT tree must pop a boolean value off the stack and jump only if it is 1. pop %eax cmp $1,%eax je B6 pop %eax cmp $1,%eax je L6


Download ppt "Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005."

Similar presentations


Ads by Google