decision structures - code generationpeople.hsc.edu/faculty-staff/robbk/coms480/lectures... ·...

Post on 20-Aug-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

top related