CS201- Lecture 7 IA32 Data Access and Operations Part II

Slides:



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

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
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.
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.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
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.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
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:
Assembly Programming II CSE 351 Spring 2017
Y86-64 Instructions 8-byte integer operations (no floating point!)  “word” (it is considered a long word for historical reasons, because the original.
Computer Architecture
Machine-Level Programming I: Basics /18-213: Introduction to Computer Systems 5th Lecture, Jan. 28, 2016 Instructors: Seth Copen Goldstein & Franz.
Instructor: Phil Gibbons
The Stack & Procedures CSE 351 Spring 2017
x86 Programming I CSE 351 Autumn 2016
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:
Assembly Programming III CSE 351 Spring 2017
Lecture 5 Machine Programming
Processor Architecture: The Y86-64 Instruction Set Architecture
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Processor Architecture: The Y86-64 Instruction Set Architecture
Assembly Programming II CSE 410 Winter 2017
x86-64 Programming I CSE 351 Autumn 2017
Machine-Level Programming IV: Data
Authors: Randal E. Bryant and David R. O’Hallaron
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 Winter 2018
x86-64 Programming II CSE 351 Summer 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming I CSE 351 Winter 2018
x86-64 Programming II CSE 351 Autumn 2018
x86-64 Programming I CSE 351 Autumn 2018
Get To Know Your Compiler
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.
Instructor: Fatma CORUT ERGİN
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.
Credits and Disclaimers
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
Presentation transcript:

CS201- Lecture 7 IA32 Data Access and Operations Part II Raoul Rivas Portland state university

Announcements CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Address Computation Examples movq 8(%rdx),%rax movq (%rdx,%rcx),%rax movq (%rdx,%rcx,4),%rax %rdx 0xf000 %rcx 0x0100 Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080 Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Compute Effective Address leaq Src, Dst Src is address mode expression Set Dst to address denoted by expression Uses Computing addresses without a memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y k = 1, 2, 4, or 8 Example Converted to ASM by compiler: long m12(long x) { return x*12; } leaq (%rdi,%rdi,2), %rax # t <- x+x*2 salq $2, %rax # return t<<2 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Basic Arithmetic Operations Two Operand Instructions: Format Computation addq Src,Dest Dest = Dest + Src subq Src,Dest Dest = Dest  Src imulq 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! Notice the accumulator format What’s the C equivalent of this accumulator operators? CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Unary Operators One Operand Instructions incq Dest Dest = Dest + 1 decq Dest Dest = Dest  1 negq Dest Dest =  Dest notq Dest Dest = ~Dest CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Translating from C long easyOp(long x, long y) { long rval; x = x + 2; y = y * x; rval = y; return rval; } Register Use(s) %rdi Argument x %rsi Argument y CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Translating from C long easyOp(long x, long y) { long rval; x = x + 2; y = y * x; rval = y; return rval; } easyOp: addq $2, %rdi imulq %rdi, %rsi movq %rsi, %rax ret Register Use(s) %rdi Argument x %rsi Argument y %rax rval If you find accumulator arithmetic tricky try writing some C programs in this way CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

38 bit single accumulator register Accumulators Can we do the same in Accumulator and in Non- Accumulator architectures? Yes. They are Turing Equivalent Computer P can simulate computer Q and computer Q can simulate computer P Any program that runs on P can be rewritten to run in Q IBM 701 38 bit single accumulator register CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Arithmetic Expression Example 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; } arith: movq %rdi, %rax addq %rsi, %rax addq %rdx, %rax movq %rdi, %rcx addq $4, %rcx imulq $48, %rsi addq %rsi,%rcx imulq %rcx, %rax ret Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Arithmetic Expression Optimization 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; } 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 Interesting Instructions leaq: address computation salq: shift imulq: multiplication But, only used once Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Arithmetic Expression Optimization 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 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Arithmetic tricks Shift to replace multiplication salq $4, %rdx Use of LEA to replace complex arithmetic leaq (%rsi,%rsi,2), %rdx XOR to replace initialization to zero xorq %rax, %rax CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

What’s Next? At this point we can write some simple Assembly functions Read and Write from Memory Variables, Pointers, Arrays, Structures Logic and Arithmetic Operations What’s missing? Comparisons Control Statements If/Else, While, Do/While Function Calls CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Summary LEA instruction is used to compute addresses without a memory reference LEA is also used to optimize certain patterns of arithmetic computations Arithmetic Operations use an accumulator based notation CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)