Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS 105 “Tour of.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics Sep. 15, 2015.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Carnegie Mellon 1 Machine-Level Programming I: Basics Slides based on from those of Bryant and O’Hallaron.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics /18-213:
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics MCS-284: Computer.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CENG331 - Computer.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
x86 Data Access and Operations
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Today: Machine Programming I: Basics History of Intel.
Lecture 6 Machine Structure Topics Finish IEEE Floating Point multiplication, addition Lab 01 comments Assembly Language Intro February 1, 2016 CSCE 212.
Lecture 6 Machine Structure Topics Finish IEEE Floating Point multiplication, addition Lab 01 comments Assembly Language Intro February 1, 2016 CSCE 212.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming I: Basics CS140 – Computer.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
Today: Machine Programming I: Basics
Machine-Level Programming I: Basics
Samira Khan University of Virginia Feb 2, 2017
Machine-Level Programming I: Basics
Machine-Level Programming I:
CSCE 212 Computer Architecture
Assembly Programming II CSE 351 Spring 2017
Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5th Lecture, Jan. 28, 2016 Instructors: Seth Copen Goldstein & Franz.
Instructor: Phil Gibbons
x86 Programming I CSE 351 Autumn 2016
Machine-Level Programming III: Procedures
Machine-Level Programming II: Control
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming II: Control Flow
x86 Programming II CSE 351 Autumn 2016
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Assembly Programming III CSE 351 Spring 2017
Lecture 5 Machine Programming
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Assembly Programming II CSE 410 Winter 2017
x86-64 Programming I CSE 351 Autumn 2017
Authors: Randal E. Bryant and David R. O’Hallaron
Carnegie Mellon Ithaca College
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming I CSE 351 Winter 2018
x86-64 Programming II CSE 351 Summer 2018
x86-64 Programming I CSE 351 Winter 2018
x86-64 Programming II CSE 351 Autumn 2018
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming I CSE 351 Autumn 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Winter 2019
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
CS201- Lecture 8 IA32 Flow Control
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Carnegie Mellon Ithaca College
Machine-Level Programming XI: inline Assembly Comp 21000: Introduction to Computer Systems & Assembly Lang On-Line resources* See see
Lecture 5 Machine Programming
Instructor: Brian Railing
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming I CSE 351 Spring 2019
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective, 3rd ed.”, Randy Bryant & David O’Hallaron, 2015

Today Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches While loops

Some Arithmetic Operations Two Operand Instructions: Format Computation addq Src,Dest Dest = Dest + Src subq Src,Dest Dest = Dest  Src imuq Src,Dest Dest = Dest * Src salq Src,Dest Dest = Dest << Src Also called shlq sarq Src,Dest Dest = Dest >> Src Arithmetic shrq Src,Dest Dest = Dest >> Src Logical xorq Src,Dest Dest = Dest ^ Src andq Src,Dest Dest = Dest & Src orq Src,Dest Dest = Dest | Src Watch out for argument order! No distinction between signed and unsigned int (why?) No way to store result in different register in the instruction. Note the order: Dest is subtracted from

Some Arithmetic Operations One Operand Instructions incq Dest Dest = Dest + 1 decq Dest Dest = Dest  1 negq Dest Dest =  Dest notq Dest Dest = ~Dest See book for more instructions

Arithmetic Expression Example Carnegie Mellon Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax addq %rdx, %rax leaq (%rsi,%rsi,2),%rdx salq $4, %rdx leaq 4(%rdi,%rdx),%rcx imulq %rcx, %rax ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Interesting Instructions leaq: address computation salq: shift imulq: multiplication But, only used once this is similar to what you’d see with optimization level 1 With optimization level 0 you probably won’t see lea instructions

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Understanding Arithmetic Expression Example Carnegie Mellon Understanding Arithmetic Expression Example arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax t1, t2, rval t4 %rcx t5

Observations about arith Instructions in different order from C code Some expressions require multiple instructions Some instructions cover multiple expressions Get exact same code when compile: (x+y+z)*(x+4+48*y) long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } leaq (%rdi,%rsi), %rax addq %rdx, %rax leaq (%rsi,%rsi,2),%rdx salq $4, %rdx leaq 4(%rdi,%rdx),%rcx imulq %rcx, %rax ret

Another Example logical: push %rbp mov %rsp,%rbp Set mov %rdi,-0x28(%rbp) mov %rsi,-0x30(%rbp) mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax mov %rax,-0x8(%rbp) mov -0x8(%rbp),%rax pop %rbp retq Set Up int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Body Finish

Another Example logical: mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Register Use(s) %rdx Argument x %rax Argument y t1, t2, mask -0x18(%rbp) t2 -0x10(%rbp) mask

Another Example logical: mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Register Use(s) %rdx Argument x %rax Argument y t1, t2, mask -0x18(%rbp) t2 -0x10(%rbp) mask

Another Example Note that 0x11 = 17 logical: mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Register Use(s) %rdx Argument x %rax Argument y t1, t2, mask -0x18(%rbp) t2 -0x10(%rbp) mask Note that 0x11 = 17

Another Example logical: mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Register Use(s) %rdx Argument x %rax Argument y t1, t2, mask -0x18(%rbp) t2 -0x10(%rbp) mask 213 = 8192, 213 – 7 = 8185 = 0x1FF9

Note that t2 is stored in -0x18(%rbp) Another Example logical: mov -0x30(%rbp),%rax mov -0x28(%rbp),%rdx xor %rdx,%rax mov %rax,-0x20(%rbp) mov -0x20(%rbp),%rax sar $0x11,%rax mov %rax,-0x18(%rbp) movq $0x1ff9,-0x10(%rbp) mov -0x10(%rbp),%rax mov -0x18(%rbp),%rdx and %rdx,%rax int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } Register Use(s) %rdx Argument x %rax Argument y t1, t2, mask -0x18(%rbp) t2 -0x10(%rbp) mask Note that t2 is stored in -0x18(%rbp)

Special Arithmetic Operations Instruction Effect Description imulq S mulq S R[%rdx]:R[%rax]  S x R[%rax] Signed full multiply Unsigned full multiply cqto R[%rdx]:R[%rax]  SignExtend( R[%rax]) Convert to oct word idivq S R[%rdx] R[%rdx]:R[%rax] mod S; R[%rax] R[%rdx]:R[%rax] / S Signed divide divq S R[%rdx] R[%rdx]:R[%rax] / S Unsigned divide See explanation on next page.

iMull Note that there are two forms of imull, one with two operands and one with one operand. imulq S, D Does 64-bit multiplication 64 bit Result is stored in D There may be overflow imulq S Multiplies 64 bits from S times 64 bits in %rax Result is 128 bits and is stored in %rax and %rdx (always). High ordered bits are in %rdx, low order bits in %rax.

Understanding Assembly long mystery (long a, long b) { long t0 = ____________; long t1 = ____________; long t2 = ____________; return _________; } Complete this exercise on your handout sheet! int main() { long x = mystery(5, 6); return 0; }