Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 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)

19 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.

20 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.

21 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; }


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

Similar presentations


Ads by Google