Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... ·...
Transcript of Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... ·...
![Page 1: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/1.jpg)
Decision Structures - Code GenerationLecture 30
Intel Manual, Vol. 1, Sections 3.4.3, 8.1.4Intel Manual, Vol. 2, Chapter 3, jcc
Robb T. Koether
Hampden-Sydney College
Mon, Apr 6, 2015
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 1 / 32
![Page 2: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/2.jpg)
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 2 / 32
![Page 3: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/3.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 3 / 32
![Page 4: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/4.jpg)
Conditional Branches
Branching on a conditional expression will be done in two parts.Evaluate the expression, leaving either true (1) or false (0) on thestack.Test the value on the stack and branch on true.
It would be more efficient to test the original expression andbranch immediately.However, by separating the two tests, it will be simpler to write thecode.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 4 / 32
![Page 5: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/5.jpg)
Conditional Branches
Recall that the if statement is of the formif ( 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 thestack if expr is nonzero and 0 on the stack if expr is zero.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 5 / 32
![Page 6: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/6.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 6 / 32
![Page 7: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/7.jpg)
The CMPNE Tree
CMPNEINT
NUM0
eINT
CMPNEDOUBLE
eDOUBLE
NUM0.0
Integer Expression Floating-point Expression
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 7 / 32
![Page 8: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/8.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 8 / 32
![Page 9: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/9.jpg)
Integer Comparisons
To compare two integers, we mustPop 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.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 9 / 32
![Page 10: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/10.jpg)
Flags CF, ZF, SF, and OF
The relevant flags areCF - carry flagZF - zero flagSF - sign flagOF - overflow flag
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 10 / 32
![Page 11: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/11.jpg)
Conditional Jumps
We then do a conditional jump.A conditional jump checks the flags to decide whether or not tojump.
Mnemonic Meaning Flagsje jump == ZF = 1
jne jump != ZF = 0jl jump < SF 6= OF
jge jump >= SF = OFjg jump > ZF = 0 and SF = OFjle jump <= ZF = 1 or SF 6= OF
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 11 / 32
![Page 12: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/12.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔
pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 13: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/13.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ p
a 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 14: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/14.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔
pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 15: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/15.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ p
a ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 16: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/16.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔
qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 17: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/17.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ q
a < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 18: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/18.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔
qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 19: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/19.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ q
a ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 20: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/20.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔
p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 21: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/21.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 22: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/22.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔
(p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 23: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/23.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “SF =OF.”Then
a = b ⇔ pa 6= b ⇔ pa ≥ b ⇔ qa < b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 12 / 32
![Page 24: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/24.jpg)
Integer Comparison
Integer Comparison for !=mov $1,%ecx # Move true to ecxpop %eax # Pop right operandpop %edx # Pop left operandcmp %eax,%edx # Compare operandsjne L0n # Jump if left != rightdec %ecx # Change ecx to false
L0n:push %ecx # Push T/F result
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 13 / 32
![Page 25: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/25.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 14 / 32
![Page 26: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/26.jpg)
Floating-Point Comparisons
When we compare two floating-point numbers, they are alreadyon the FPU stack, in st(0) and st(1).On older x87 FPUs (earlier than P6), we use the instructionfucompp
Compares st(0) to st(1).Sets condition codes C0 and C3 in the FPU status word.Pops both operands off the stack.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 15 / 32
![Page 27: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/27.jpg)
Flags C0 and C3
The result of the comparison is determined by C0 and C3.
Result C0 C3st(0) > st(1) 0 0st(0) < st(1) 1 0st(0) = st(1) 0 1
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 16 / 32
![Page 28: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/28.jpg)
Floating-Point Comparisons
To perform a conditional jump based on a floating-pointcomparison, we must first move the FPU status word to theEFLAGS register.The instruction
fnstsw %ax
stores the FPU status word in ax (16 bits).The instruction
sahf
stores ah (8 bit) in the EFLAGS register.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 17 / 32
![Page 29: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/29.jpg)
Floating-Point Comparisons
On newer x87 FPRs (P6 or later), we use the instruction fcomip.Compares st(0) to st(1).Sets flags ZF, SF, and OF in the x86 status word.Pops one operand off the stack.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 18 / 32
![Page 30: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/30.jpg)
Flags C0 and C3
The result of the comparison is determined by C0 and C3.
Result C0 C3st(0) > st(1) 0 0st(0) < st(1) 1 0st(0) = st(1) 0 1
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 19 / 32
![Page 31: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/31.jpg)
Floating-Point Comparisons
To perform a conditional jump based on a floating-pointcomparison, we must first move the FPU status word to theEFLAGS register.The instruction
fnstsw %ax
stores the FPU status word in ax (16 bits).The instruction
sahf
stores ah (8 bit) in the EFLAGS register.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 20 / 32
![Page 32: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/32.jpg)
Floating-Point Comparisons
The sahf instruction mapsC0→ CF.C3→ ZF.
Thus, we want to use conditional jumps that check CF and ZF.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 21 / 32
![Page 33: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/33.jpg)
Conditional Jumps
Mnemonic Meaning Flagsje jump == ZF = 1
jne jump != ZF = 0jb jump < CF = 1
jae jump >= CF = 0ja jump > ZF = 0 and CF = 0
jbe jump <= ZF = 1 or CF = 1
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 22 / 32
![Page 34: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/34.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔
pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 35: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/35.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ p
a 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 36: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/36.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔
pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 37: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/37.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ p
a < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 38: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/38.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔
qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 39: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/39.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ q
a ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 40: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/40.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔
qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 41: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/41.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ q
a ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 42: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/42.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔
p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 43: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/43.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 44: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/44.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔
(p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 45: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/45.jpg)
Conditional Jumps
Verify the previous table.Let a and b be numbers.Let p be the statement “ZF = 1” and let q be the statement “CF =1.”Then
a = b ⇔ pa 6= b ⇔ pa < b ⇔ qa ≥ b ⇔ qa ≤ b ⇔ p ∨ q
a > b ⇔ (p ∨ q) = p ∧ q
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 23 / 32
![Page 46: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/46.jpg)
Floating-Point Comparisons
Floating-Point Comparison for !=mov $1,%ecx # Move true to ecxfucompp # Compare and pop operandsfnstsw %ax # Store status wordsahf # Move ah to eflagsjne L0n # Jump if lft != rgtdec %ecx # Change ecx to false
L0n:push %ecx # Push T/F result
Earlier than P6
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 24 / 32
![Page 47: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/47.jpg)
Floating-Point Comparisons
Floating-Point Comparison for !=mov $1,%ecx # Move true to ecxfcomip # Compare and pop left operandfstpl -8(%esp) # Pop FPU stackjne L0n # Jump if lft != rgtdec %ecx # Change ecx to false
L0n:push %ecx # Push T/F result
P6 or later
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 25 / 32
![Page 48: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/48.jpg)
Floating-Point Comparisons
One little complication is that fucompp and fcomip comparest(0) and st(1) in the reverse order from what we wouldexpect.Therefore, the meanings of < and > are reversed and themeanings of <= and >= are reversed.So we reverse the roles of ja and jb, and jae and jbe.
ARRGGGHHHHH!!!
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 26 / 32
![Page 49: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/49.jpg)
Floating-Point Comparisons
One little complication is that fucompp and fcomip comparest(0) and st(1) in the reverse order from what we wouldexpect.Therefore, the meanings of < and > are reversed and themeanings of <= and >= are reversed.So we reverse the roles of ja and jb, and jae and jbe.ARRGGGHHHHH!!!
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 26 / 32
![Page 50: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/50.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 27 / 32
![Page 51: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/51.jpg)
Jump Trees
JUMP
BLABEL6
JUMPT
BLABEL6
CMPNEe.mode
NUM0
JUMP Syntax Tree
JUMPT Syntax Tree
ee.mode
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 28 / 32
![Page 52: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/52.jpg)
JUMP Trees
A JUMP tree generates the following code.
Code for JUMPjmp B6
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 29 / 32
![Page 53: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/53.jpg)
JUMPT Trees
A JUMPT tree must pop a boolean value off the stack and jumponly if it is 1.
Code for JUMPTpop %eax # Pop boolean valuecmp $1,%eax # Compare to trueje B6 # Jump if true
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 30 / 32
![Page 54: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/54.jpg)
Outline
1 Conditional Branches
2 The CMPNE TreeInteger ComparisonsFloating-Point Comparisons
3 Jump Trees
4 Assignment
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 31 / 32
![Page 55: Decision Structures - Code Generationpeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - Code Generation Lecture 30 Intel Manual, Vol. 1, Sections 3.4.3,](https://reader035.fdocuments.us/reader035/viewer/2022071009/5fc725795ef0ae68894ebe92/html5/thumbnails/55.jpg)
Assignment
HomeworkRead the Intel Manual, Vol. 1, Sections 3.4.3, 8.1.4.Read about the instructions fcomip (and fucompp, fnstsw, andsahf).Read the Intel Manual, Vol. 2, jcc instructions.
Robb T. Koether (Hampden-Sydney College) Decision Structures - Code Generation Mon, Apr 6, 2015 32 / 32