department of computer science university of rochester · cmpq %rsi, %rdi jle .l4!9 carnegie mellon...

176
CSC 252: Computer Organization Spring 2020: Lecture 7 Instructor: Yuhao Zhu Department of Computer Science University of Rochester

Upload: others

Post on 19-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

CSC 252: Computer Organization Spring 2020: Lecture 7

Instructor: Yuhao Zhu

Department of Computer ScienceUniversity of Rochester

Page 2: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

Carnegie Mellon

!2

Announcement• Programming assignment 2 is out

• Details: https://www.cs.rochester.edu/courses/252/spring2020/labs/assignment2.html

• Due on Feb. 14, 11:59 PM • You (may still) have 3 slip days

Today

Due

Page 3: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

Carnegie Mellon

!3

Announcement• Programming assignment 2 is out

• Details: https://www.cs.rochester.edu/courses/252/spring2020/labs/assignment2.html

• Due on Feb. 14, 11:59 PM • You (may still) have 3 slip days

• Read the instructions before getting started!!!• You get 1/4 point off for every wrong answer • Maxed out at 10

• Request one bomb per group using one person’s email and ID. Email Shuang and Sudhanshu who you are working with.

Page 4: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

Carnegie Mellon

!4

Announcement• Grades for lab1 are posted.• If you think there are some problems

• Take a deep breath • Tell yourself that the teaching staff like you, not the opposite • Email/go to Shuang or Sudhanshu’s office hours and explain to

them why you should get more points, and they will fix it for you

Page 5: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

Carnegie Mellon

!5

Announcement• Office hour temporarily moved to 3-4pm this Friday• Programming assignment 2 is in x86 assembly language.

Seek help from TAs.• TAs are best positioned to answer your questions about

programming assignments!!!• Programming assignments do NOT repeat the lecture

materials. They ask you to synthesize what you have learned from the lectures and work out something new.

Page 6: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

Page 7: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

Page 8: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

movq 8(%rdi), %rdx

Page 9: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

movq 8(%rdi), %rdx

addq 8(%rdi), %rdx

Page 10: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

movq 8(%rdi), %rdx

addq 8(%rdi), %rdx

Accessing memory and doing computation in one instruction. Allowed in x86, but not all ISAs allow that (e.g., ARM).

Page 11: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

movq 8(%rdi), %rdx

movq (%rdi), (%rdx)

addq 8(%rdi), %rdx

Page 12: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

Illegal in x86 (and almost all other ISAs). Could make microarchitecture implementation inefficient/inelegant.

!6

Data Movementmovq (%rdi), %rdx

• Semantics:• Move (really, copy) data store in memory location whose

address is the value stored in %rdi to register %rdx

movq %rdx, (%rdi)

movq 8(%rdi), %rdx

movq (%rdi), (%rdx)

addq 8(%rdi), %rdx

Page 13: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!7

Carnegie Mellon

Today: Control Instructions• Control: Conditional branches (if… else…)• Control: Loops (for, while)• Control: Switch Statements (case… switch…)

Page 14: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!8

Carnegie Mellon

Conditional Branch Examplelong absdiff (long x, long y){ long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value

Page 15: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!8

Carnegie Mellon

Conditional Branch Examplelong absdiff (long x, long y){ long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value

Labels are symbolic names used to refer to instruction addresses.

Page 16: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

Page 17: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

Jump to label if less than or equal to

Page 18: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jle:• Treat the data in %rdi

and %rsi as signed values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

Jump to label if less than or equal to

Page 19: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jle:• Treat the data in %rdi

and %rsi as signed values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:

Jump to label if less than or equal to

Page 20: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jle:• Treat the data in %rdi

and %rsi as signed values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes (a.k.a., status flags)

Jump to label if less than or equal to

Page 21: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jle:• Treat the data in %rdi

and %rsi as signed values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes (a.k.a., status flags)

• jle reads and checks the status flags

Jump to label if less than or equal to

Page 22: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jle .L4

!9

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jle:• Treat the data in %rdi

and %rsi as signed values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes (a.k.a., status flags)

• jle reads and checks the status flags

• If condition met, modify the Program Counter to point to the address of the instruction with a label .L4

Jump to label if less than or equal to

Page 23: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

Page 24: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• Essentially, how do we know %rdi <= %rsi?

Page 25: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi

Page 26: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0

Page 27: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0

ZF Zero Flag (result is zero)

Page 28: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

ZF Zero Flag (result is zero)

Page 29: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or

ZF Zero Flag (result is zero)

Page 30: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or

ZF Zero Flag (result is zero) 001 -) 010

111

1 -) 2

-1

No Overflow

Page 31: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or

ZF Zero Flag (result is zero) 001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

Page 32: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or• %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero) 001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

Page 33: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF SF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or• %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)

001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

Page 34: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!10

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF SF OF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or• %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)OF Overflow Flag (results overflow)

001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

Page 35: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!11

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

0

SF

0

OF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or • %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)OF Overflow Flag (results overflow)cmpq 0xFF, 0x80

11111111 10000000

Page 36: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!11

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

0

SF

0

OF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or • %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)OF Overflow Flag (results overflow)cmpq 0xFF, 0x80

11111111 10000000

10000000 -) 11111111

10000001

-128 -) -1

-127

Page 37: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!11

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

0

SF

0

OF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or • %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)OF Overflow Flag (results overflow)

1

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

10000001

-128 -) -1

-127

Page 38: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!11

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

0

SF

0

OF

cmpq %rsi, %rdi• Essentially, how do we know %rdi <= %rsi?• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if and only if: %rdi - %rsi < 0 (is it correct??)

• %rdi - %rsi < 0 and the result doesn’t overflow, or • %rdi - %rsi > 0 and the result does overflow

ZF Zero Flag (result is zero)SF Sign Flag (result is negative)OF Overflow Flag (results overflow)

1

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

10000001

-128 -) -1

-127

• %rdi <= %rsi if and only if• ZF is set, or • SF is set but OF is not set, or • SF is not set, but OF is set

• or simply: ZF | (SF ^ OF)

Page 39: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!12

Carnegie Mellon

Conditional Branch Examplelong absdiff (long x, long y){ long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value 0

ZF

0

SF

0

OF

Page 40: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!12

Carnegie Mellon

Conditional Branch Examplelong absdiff (long x, long y){ long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value 0

ZF

0

SF

0

OF

cmpq sets ZF, SF, OFjle checks ZF | (SF ^ OF)

Page 41: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits

Page 42: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)

Page 43: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or

Page 44: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or• If A > 0 & B < 0, but the result < 0

Page 45: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or• If A > 0 & B < 0, but the result < 0

001 -) 010

111

1 -) 2

-1

No Overflow

Page 46: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or• If A > 0 & B < 0, but the result < 0

001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

Page 47: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or• If A > 0 & B < 0, but the result < 0

001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

3 -) -4

-1

011 -) 100

111

Page 48: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!13

How Does the Hardware Check Overflow?

• ZF and SF are easily set by just examining the bits• How about OF? How do we know A-B leads to

overflow (A and B are treated as signed)• If A < 0 & B > 0, but the result > 0, or• If A > 0 & B < 0, but the result < 0• So again, just have to check the bits

001 -) 010

111

1 -) 2

-1

No Overflow

Overflow -3 -) 3

2

101 -) 011

010

3 -) -4

-1

011 -) 100

111

Page 49: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!14

Carnegie Mellon

Conditional Branch Exampleunsigned long absdiff (unsigned long x, unsigned long y){ unsigned long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value 0

ZF

0

SF

0

OF

Page 50: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!14

Carnegie Mellon

Conditional Branch Exampleunsigned long absdiff (unsigned long x, unsigned long y){ unsigned long result; if (x > y) result = x-y; else result = y-x; return result;}

absdiff: cmpq %rsi,%rdi # x:y jle .L4 movq %rdi,%rax subq %rsi,%rax ret .L4: # x <= y movq %rsi,%rax subq %rdi,%rax retRegister Use(s)

%rdi x

%rsi y

%rax Return value 0

ZF

0

SF

0

OF

jbe .L4

Page 51: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

Page 52: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

Jump to label if below or equal to

Page 53: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jbe:• Treat the data in %rdi

and %rsi as unsigned values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

Jump to label if below or equal to

Page 54: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jbe:• Treat the data in %rdi

and %rsi as unsigned values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:

Jump to label if below or equal to

Page 55: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jbe:• Treat the data in %rdi

and %rsi as unsigned values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes

Jump to label if below or equal to

Page 56: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jbe:• Treat the data in %rdi

and %rsi as unsigned values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes• jbe reads and checks the

condition codes

Jump to label if below or equal to

Page 57: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

cmpq %rsi, %rdi jbe .L4

!15

Carnegie Mellon

Conditional Jump Instruction

• Semantics of jbe:• Treat the data in %rdi

and %rsi as unsigned values.

• If %rdi is less than or equal to %rsi, jump to the part of the code with a label .L4

• Under the hood:• cmpq instruction sets the

condition codes• jbe reads and checks the

condition codes• If condition met, modify the

Program Counter to point to the address of the instruction with a label .L4

Jump to label if below or equal to

Page 58: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

Page 59: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• How do we know %rdi <= %rsi? This time for unsigned values

Page 60: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi

Page 61: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?cmpq %rsi, %rdi

• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0

Page 62: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0

ZF Zero Flag (result is zero)

Page 63: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)

Page 64: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero) 001 -) 111

C010

17

Page 65: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned) 001

-) 111

C010

CF

17

Page 66: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if a carry is generated during subtraction• Why don’t we look at the SF and OF as in the signed case?

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned) 001

-) 111

C010

CF

17

Page 67: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!16

Carnegie Mellon

How Should cmpq Set Condition Codes?

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi• %rdi == %rsi if and only if %rdi - %rsi == 0• %rdi < %rsi if a carry is generated during subtraction• Why don’t we look at the SF and OF as in the signed case?

• Checking unsigned overflow is much harder

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned) 001

-) 111

C010

CF

17

Page 68: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!17

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned)

0

CF

cmpq 0xFF, 0x8011111111 10000000

Page 69: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!17

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned)

0

CF

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001

128255

Page 70: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!17

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned)

0

CF

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001

128255

1

Page 71: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!17

Carnegie Mellon

How Should cmpq Set Condition Codes?

0

ZF

cmpq %rsi, %rdi• How do we know %rdi <= %rsi? This time for unsigned values• Calculate %rdi - %rsi • %rdi == %rsi if and only if %rdi - %rsi == 0 • %rdi < %rsi if a carry is generated during subtraction

ZF Zero Flag (result is zero)CF Carry Flag (for unsigned)

0

CF

1

• %rdi <= %rsi (as unsigned) if and only if:

• ZF is set, or • CF is set

• or simply: ZF | CF• This is what jbe checks

Page 72: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

Page 73: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously

Page 74: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously

ZF Zero FlagCF Carry Flag SF Sign FlagOF Overflow Flag (for signed)

0

ZF

0

CF

0

SF

0

OF

Page 75: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously

ZF Zero FlagCF Carry Flag SF Sign FlagOF Overflow Flag (for signed)

0

ZF

0

CF

0

SF

0

OF

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001

Page 76: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously

ZF Zero FlagCF Carry Flag SF Sign FlagOF Overflow Flag (for signed)

0

ZF

0

CF

0

SF

0

OF

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001 1 1

Page 77: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously• ZF, SF, and OF are used when comparing signed value (e.g., jle)

ZF Zero FlagCF Carry Flag SF Sign FlagOF Overflow Flag (for signed)

0

ZF

0

CF

0

SF

0

OF

cmpq %rsi,%rdi jle .L4

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001 1 1

Page 78: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!18

Carnegie Mellon

Putting It All Together

• cmpq sets all 4 condition codes simultaneously• ZF, SF, and OF are used when comparing signed value (e.g., jle)• ZF, CF are used when comparing unsigned value (e.g., jbe)

ZF Zero FlagCF Carry Flag SF Sign FlagOF Overflow Flag (for signed)

0

ZF

0

CF

0

SF

0

OF

cmpq %rsi,%rdi jle .L4

cmpq %rsi,%rdi jbe .L4

cmpq 0xFF, 0x8011111111 10000000

10000000 -) 11111111

c10000001 1 1

Page 79: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!19

CPU

Condition Codes Hold Test Results

PCRegister

FileMemory

Code Data Stack Heap

Addresses

Data

InstructionsCondition Codes

AssemblyProgrammer’sPerspective

of a Computer ALU

• Condition Codes• Hold the status of most recent test • 4 common condition codes in x86-64 • A set of special registers (more often: bits in one single register) • Sometimes also called: Status Register, Flag Register

CF ZF SF OF

CF Carry FlagZF Zero Flag

SF Sign FlagOF Overflow Flag (for signed)

Page 80: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!20

Carnegie Mellon

Jump InstructionsInstruction Jump Condition Descriptionjmp 1 Unconditionalje ZF Equal / Zerojne ~ZF Not Equal / Not Zerojs SF Negativejns ~SF Nonnegativejg ~(SF^OF)&~ZF Greater (Signed)jge ~(SF^OF) Greater or Equal (Signed)jl (SF^OF) Less (Signed)jle (SF^OF)|ZF Less or Equal (Signed)ja ~CF&~ZF Above (unsigned)jae ~CF Above or Equal (unsigned)jb CF Below (unsigned)jbe CF|ZF Below or Equal (unsigned)

• Jump to different part of code (designated by a label) depending on condition codes

Page 81: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!20

Carnegie Mellon

Jump InstructionsInstruction Jump Condition Descriptionjmp 1 Unconditionalje ZF Equal / Zerojne ~ZF Not Equal / Not Zerojs SF Negativejns ~SF Nonnegativejg ~(SF^OF)&~ZF Greater (Signed)jge ~(SF^OF) Greater or Equal (Signed)jl (SF^OF) Less (Signed)jle (SF^OF)|ZF Less or Equal (Signed)ja ~CF&~ZF Above (unsigned)jae ~CF Above or Equal (unsigned)jb CF Below (unsigned)jbe CF|ZF Below or Equal (unsigned)

Page 82: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codesaddq %rax, %rbx

Page 83: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

addq %rax, %rbx

Page 84: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)

addq %rax, %rbx

Page 85: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0

addq %rax, %rbx

Page 86: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0

addq %rax, %rbx

Page 87: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers

addq %rax, %rbx

Page 88: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or

addq %rax, %rbx

Page 89: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

Page 90: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

0

ZF

0

CF

0

SF

0

OF

addq 0xFF, 0x80 10000000 +) 11111111

c01111111

Page 91: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

0

ZF

0

CF

0

SF

0

OF

1

addq 0xFF, 0x80 10000000 +) 11111111

c01111111

Page 92: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

0

ZF

0

CF

0

SF

0

OF

1

addq 0xFF, 0x80 10000000 +) 11111111

c01111111

1

Page 93: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

0

ZF

0

CF

0

SF

0

OF

1

addq 0xFF, 0x80

1jle .L4

Page 94: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!21

Carnegie Mellon

Implicit Set Condition Codes

• Arithmetic instructions implicitly set condition codes (think of it as side effect)

• CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)• ZF set if %rax + %rbx == 0• SF set if %rax + %rbx < 0• OF set if %rax + %rbx overflows when %rax and %rbx are treated as

signed numbers• %rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or• %rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)

addq %rax, %rbx

0

ZF

0

CF

0

SF

0

OF

1

addq 0xFF, 0x80

1jle .L4

if((x+y)<0) { …

}

Page 95: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!22

Carnegie Mellon

Today: Control Instructions• Control: Conditional branches (if… else…)• Control: Loops (for, while)• Control: Switch Statements (case… switch…)

Page 96: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!23

Carnegie Mellon

do-while versionlong pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}

“Do-While” Loop Example

• Popcount: Count number of 1’s in argument x

Page 97: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!23

Carnegie Mellon

do-while versionlong pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}

goto Versionlong pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

“Do-While” Loop Example

• Popcount: Count number of 1’s in argument x

Page 98: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assemblylong pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

Page 99: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 100: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 101: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 102: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 103: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 104: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 105: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 106: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 107: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 108: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!24

Carnegie Mellon

“Do-While” Loop Assembly

Register

Use(s)

%rdi Argument x

%rax result

long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result;}

movl $0, %rax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %rdx # t = x & 0x1 addq %rdx, %rax # result += t shrq $1, %rdi # x >>= 1 jne .L2 # if (x) goto loop ret

Page 109: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> .L1: <body>

if (A < B) goto .L1

<after>

!25

Carnegie Mellon

General “Do-While” Translationdo-while version goto Version

<before>; do {

body; } while (A < B); <after>;

Page 110: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> .L1: <body>

if (A < B) goto .L1

<after>

!25

Carnegie Mellon

General “Do-While” Translationdo-while version goto Version

Replace with a conditional jump

instruction

<before>; do {

body; } while (A < B); <after>;

Page 111: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> .L1: <body>

if (A < B) goto .L1

<after>

!25

Carnegie Mellon

General “Do-While” Translationdo-while version goto Version

<before> .L1: <body> cmpq B, A jl .L1 <after>

<before>; do {

body; } while (A < B); <after>;

AssemblyVersion

Page 112: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!26

Carnegie Mellon

General “While” Translationwhile version

<before>; while (A < B) {

body; } <after>;

Page 113: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> goto .L2 .L1: <body> .L2: if (A < B) goto .L1 <after>

!26

Carnegie Mellon

General “While” Translationwhile version goto Version

<before>; while (A < B) {

body; } <after>;

Page 114: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> goto .L2 .L1: <body> .L2: if (A < B) goto .L1 <after>

!26

Carnegie Mellon

General “While” Translationwhile version goto Version

<before>; while (A < B) {

body; } <after>;

<before> jmp .L2 .L1: <body> .L2: cmpq A, B jg .L1 <after>

AssemblyVersion

Page 115: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> goto .L2 .L1: <body> .L2: if (A < B) goto .L1 <after>

!26

Carnegie Mellon

General “While” Translationwhile version goto Version

<before>; while (A < B) {

body; } <after>;

<before> jmp .L2 .L1: <body> .L2: cmpq A, B jg .L1 <after>

AssemblyVersion

Page 116: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

<before> goto .L2 .L1: <body> .L2: if (A < B) goto .L1 <after>

!26

Carnegie Mellon

General “While” Translationwhile version goto Version

<before>; while (A < B) {

body; } <after>;

<before> jmp .L2 .L1: <body> .L2: cmpq A, B jg .L1 <after>

AssemblyVersion

Page 117: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!27

Carnegie Mellon

long pcount_while (unsigned long x) {

long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;

}

“While” Loop Example

while version

Page 118: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!27

Carnegie Mellon

long pcount_while (unsigned long x) {

long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;

}

long pcount_goto_jtm (unsigned long x) { long result = 0; goto test; loop: result += x & 0x1; x >>= 1; test: if(x) goto loop; return result;}

“While” Loop Example

while version goto Version

Page 119: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

Page 120: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

//assume unsigned int is 4 byteslong pcount_for (unsigned int x){ size_t i; long result = 0; for (i = 0; i < 32; i++) { result += (x >> i) & 0x1; } return result;

}

Page 121: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

//assume unsigned int is 4 byteslong pcount_for (unsigned int x){ size_t i; long result = 0; for (i = 0; i < 32; i++) { result += (x >> i) & 0x1; } return result;

}

i = 0init

Page 122: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

//assume unsigned int is 4 byteslong pcount_for (unsigned int x){ size_t i; long result = 0; for (i = 0; i < 32; i++) { result += (x >> i) & 0x1; } return result;

}

i = 0init

i < WSIZEtest

Page 123: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

//assume unsigned int is 4 byteslong pcount_for (unsigned int x){ size_t i; long result = 0; for (i = 0; i < 32; i++) { result += (x >> i) & 0x1; } return result;

}

i = 0init

i < WSIZEtest

i++update

Page 124: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!28

“For” Loop Example

for (init; test; update){ body }

//assume unsigned int is 4 byteslong pcount_for (unsigned int x){ size_t i; long result = 0; for (i = 0; i < 32; i++) { result += (x >> i) & 0x1; } return result;

}

i = 0init

i < WSIZEtest

i++update

{ result += (x >> i) & 0x1;}

body

Page 125: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!29

Convert “For” Loop to “While” LoopFor Version

before; for (init; test; update) { body; } after

Page 126: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!29

Convert “For” Loop to “While” LoopFor Version While Version

before; for (init; test; update) { body; } after

before; init; while (test) { body; update; } after;

Page 127: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!29

Convert “For” Loop to “While” LoopFor Version While Version

before; for (init; test; update) { body; } after

before init jmp .L2 .L1: body update .L2: cmpq A, B jg .L1 after

Assembly Version

before; init; while (test) { body; update; } after;

Page 128: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!30

Carnegie Mellon

Today: Control Instructions• Control: Conditional branches (if… else…)• Control: Loops (for, while)• Control: Switch Statements (case… switch…)

Page 129: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!31

Switch Statement Examplelong switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

Page 130: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!31

Switch Statement Example

Fall-through case

long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

Page 131: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!31

Switch Statement Example

Multiple case labels

Fall-through case

long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

Page 132: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!31

Switch Statement Example

Multiple case labels

Fall-through case

For missing cases, fall back to default

long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

Page 133: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!31

Switch Statement Example

Multiple case labels

Fall-through case

For missing cases, fall back to default

Converting to a cascade of if-else

statements is simple, but cumbersome with

too many cases.

long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; }

Page 134: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!32

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Page 135: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!32

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Code Block0

Targ0:

Code Block1

Targ1:

Code Block2

Targ2:

Code Blockn–1

Targn-1:

• • •

Jump Targets

Page 136: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!32

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Targ0

Targ1

Targ2

Targn-1

• • •

JTab:

Jump Table

Code Block0

Targ0:

Code Block1

Targ1:

Code Block2

Targ2:

Code Blockn–1

Targn-1:

• • •

Jump Targets

Page 137: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!32

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Targ0

Targ1

Targ2

Targn-1

• • •

JTab:

Jump Table

Code Block0

Targ0:

Code Block1

Targ1:

Code Block2

Targ2:

Code Blockn–1

Targn-1:

• • •

Jump Targets

Page 138: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!32

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Targ0

Targ1

Targ2

Targn-1

• • •

JTab:

Jump Table

Code Block0

Targ0:

Code Block1

Targ1:

Code Block2

Targ2:

Code Blockn–1

Targn-1:

• • •

Jump Targets

• Each code block starts from a unique address (Targ0, Targ1, …)

• Jump table stores all the target address• Use the case value to index into the

jump table to find where to jump to

Page 139: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 140: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 141: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 142: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 143: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 144: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 145: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 146: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!33

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Jump Table in Assembly switch(x) { case 1: // .L1 w = y*z; break; case 2: // .L2 w = y/z; /* Fall Through */ case 3: // .L3 w += z; break; case 5: case 6: // .L5 w -= z; break; default: // .LD w = 2; }

Page 147: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!34

Assembly Directives (Pseudo-Ops)

• Directives:• Not real instructions, but assist

assembler. Think of them as messages to help the assembler in the assembly process.

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 148: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!34

Assembly Directives (Pseudo-Ops)• .quad: tells the assembler to set

aside the next 8 bytes in memory and initialize with the value of the operand (a label here, which itself is an address)

• Directives:• Not real instructions, but assist

assembler. Think of them as messages to help the assembler in the assembly process.

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 149: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!34

Assembly Directives (Pseudo-Ops)• .quad: tells the assembler to set

aside the next 8 bytes in memory and initialize with the value of the operand (a label here, which itself is an address)

• .align: tells the assembler that addresses of the the following data will be aligned to 8 bytes

• Directives:• Not real instructions, but assist

assembler. Think of them as messages to help the assembler in the assembly process.

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 150: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!34

Assembly Directives (Pseudo-Ops)• .quad: tells the assembler to set

aside the next 8 bytes in memory and initialize with the value of the operand (a label here, which itself is an address)

• .align: tells the assembler that addresses of the the following data will be aligned to 8 bytes

• .section: denotes different parts of the object file• Directives:

• Not real instructions, but assist assembler. Think of them as messages to help the assembler in the assembly process.

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 151: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!34

Assembly Directives (Pseudo-Ops)• .quad: tells the assembler to set

aside the next 8 bytes in memory and initialize with the value of the operand (a label here, which itself is an address)

• .align: tells the assembler that addresses of the the following data will be aligned to 8 bytes

• .section: denotes different parts of the object file

• .rodata: read-only data section• Directives:• Not real instructions, but assist

assembler. Think of them as messages to help the assembler in the assembly process.

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 152: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!35

Jump Table and Jump Targets

.L1: # Case 1 movq %rsi, %rax imulq %rdx, %rax jmp .done .L2: # Case 2 movq %rsi, %rax cqto idivq %rcx .L3: # Case 3 addq %rcx, %rax jmp .done .L5: # Case 5,6 subq %rdx, %rax jmp .done .LD: # Default movl $2, %eax jmp .done

Jump Table Jump Targets.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

jmp .L3 will go to .L3 and start

executing from there

Page 153: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!36

Code Blocks (x == 1)

Page 154: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!36

Code Blocks (x == 1)switch(x) { case 1: // .L1 w = y*z; break; … }

Page 155: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!36

Code Blocks (x == 1)switch(x) { case 1: // .L1 w = y*z; break; … }

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

Page 156: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!36

Code Blocks (x == 1)

.L1: movq %rsi, %rax # y imulq %rdx, %rax # y*z jmp .done

switch(x) { case 1: // .L1 w = y*z; break; … }

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

Page 157: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!36

Code Blocks (x == 1)

.L1: movq %rsi, %rax # y imulq %rdx, %rax # y*z jmp .done

switch(x) { case 1: // .L1 w = y*z; break; … }

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

Page 158: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!37

Code Blocks (x == 2, x == 3)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

Page 159: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!37

Code Blocks (x == 2, x == 3)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { …case 2: // .L2

w = y/z; /* Fall Through */

case 3: // .L3 w += z; break; …

}

Page 160: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!37

Code Blocks (x == 2, x == 3)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { …case 2: // .L2

w = y/z; /* Fall Through */

case 3: // .L3 w += z; break; …

}

.L2: # Case 2 movq %rsi, %rax cqto idivq %rcx # y/z

.L3: # Case 3 addq %rcx, %rax # w += z jmp .done

Page 161: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!37

Code Blocks (x == 2, x == 3)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { …case 2: // .L2

w = y/z; /* Fall Through */

case 3: // .L3 w += z; break; …

}

.L2: # Case 2 movq %rsi, %rax cqto idivq %rcx # y/z

.L3: # Case 3 addq %rcx, %rax # w += z jmp .done

Page 162: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!38

Code Blocks (x == 5, x == 6, default)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

Page 163: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!38

Code Blocks (x == 5, x == 6, default)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { … case 5: // .L5case 6: // .L5 w -= z; break;default: // .LD w = 2; }

Page 164: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!38

Code Blocks (x == 5, x == 6, default)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { … case 5: // .L5case 6: // .L5 w -= z; break;default: // .LD w = 2; }

.L5: # Case 5,6 subq %rdx, %rax # w -= z jmp .done .LD: # Default: movl $2, %eax # 2 jmp .done

Page 165: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!38

Code Blocks (x == 5, x == 6, default)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { … case 5: // .L5case 6: // .L5 w -= z; break;default: // .LD w = 2; }

.L5: # Case 5,6 subq %rdx, %rax # w -= z jmp .done .LD: # Default: movl $2, %eax # 2 jmp .done

Page 166: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

.section .rodata .align 8.L4: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

!38

Code Blocks (x == 5, x == 6, default)

Register Use(s)%rdi Argument x%rsi Argument y%rdx Argument z%rax Return value

switch(x) { … case 5: // .L5case 6: // .L5 w -= z; break;default: // .LD w = 2; }

.L5: # Case 5,6 subq %rdx, %rax # w -= z jmp .done .LD: # Default: movl $2, %eax # 2 jmp .done

Page 167: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!39

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

Targ0

Targ1

Targ2

Targn-1

• • •

JTab:

Jump Table

Code Block0

Code Block1

Code Block2

Code Blockn–1

Targ0:

Targ1:

Targ2:

Targn-1:

• • •

Jump Targets

Page 168: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!39

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

• • •

Jump Table

Code Block0

Code Block1

Code Block2

Code Blockn–1

• • •

Jump Targets

.LD

.L1

.L2

.L5

.LD:

.L1:

.L2:

.L5:

.LJ:

Page 169: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!39

Implementing Switch Using Jump Table

switch(x) { case val_0: Block 0 case val_1: Block 1 …. case val_n-1: Block n–1 }

Switch Form

• • •

Jump Table

Code Block0

Code Block1

Code Block2

Code Blockn–1

• • •

Jump Targets

• The only thing left…• How do we jump to different

locations in the jump table depending on the case value?

.LD

.L1

.L2

.L5

.LD:

.L1:

.L2:

.L5:

.LJ:

Page 170: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump Instruction

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 171: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump InstructionThe address we want to jump to is stored at.LJ + 8 * x

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 172: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump InstructionThe address we want to jump to is stored at.LJ + 8 * x

# assume x in %rdi movq .LJ(,%rdi,8), %rax jmp *%rax

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 173: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump InstructionThe address we want to jump to is stored at.LJ + 8 * x

# assume x in %rdi movq .LJ(,%rdi,8), %rax jmp *%rax

• Indirect Jump: jmp *%rax• %rax specifies the address to

jump to (PC = %rax)

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 174: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump InstructionThe address we want to jump to is stored at.LJ + 8 * x

# assume x in %rdi movq .LJ(,%rdi,8), %rax jmp *%rax

• Indirect Jump: jmp *%rax• %rax specifies the address to

jump to (PC = %rax)• Direct Jump (jmp .LJ), directly

specifies the jump address

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 175: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump InstructionThe address we want to jump to is stored at.LJ + 8 * x

# assume x in %rdi movq .LJ(,%rdi,8), %rax jmp *%rax

• Indirect Jump: jmp *%rax• %rax specifies the address to

jump to (PC = %rax)• Direct Jump (jmp .LJ), directly

specifies the jump address• Indirect Jump specifies where the

jump address is located

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

Page 176: Department of Computer Science University of Rochester · cmpq %rsi, %rdi jle .L4!9 Carnegie Mellon Conditional Jump Instruction •Semantics of jle: • Treat the data in %rdi and

!40

Indirect Jump Instruction

jmp *.LJ(,%rdi,8)

The address we want to jump to is stored at.LJ + 8 * x

# assume x in %rdi movq .LJ(,%rdi,8), %rax jmp *%rax

• Indirect Jump: jmp *%rax• %rax specifies the address to

jump to (PC = %rax)• Direct Jump (jmp .LJ), directly

specifies the jump address• Indirect Jump specifies where the

jump address is located

.section .rodata .align 8.LJ: .quad .LD # x = 0 .quad .L1 # x = 1 .quad .L2 # x = 2 .quad .L3 # x = 3 .quad .LD # x = 4 .quad .L5 # x = 5 .quad .L5 # x = 6

An equivalent syntax in x86: