Samira Khan
University of Virginia Feb 2, 2017
Machine Language CS 3330 Samira Khan University of Virginia Feb - - PowerPoint PPT Presentation
Machine Language CS 3330 Samira Khan University of Virginia Feb 2, 2017 AG AGENDA Logistics Review of Abstractions Machine Language 2 Lo Logistics Feedback Not clear Hard to hear Use microphone Good feedback
University of Virginia Feb 2, 2017
2
3
4
5
6
7
8
9
10
11
12
13
14
15
recent arithmetic or logical operation
CPU PC Registers Memory
Code Data Stack Addresses Data Instructions
Condition Codes
16
17
0000000000400595 <sumstore>: 400595: 53 push %rbx 400596: 48 89 d3 mov %rdx,%rbx 400599: e8 f2 ff ff ff callq 400590 <plus> 40059e: 48 89 03 mov %rax,(%rbx) 4005a1: 5b pop %rbx 4005a2: c3 retq
18
19
20
21
22
23
24
%eax %ebx %ecx %edx %esi %edi %esp %ebp %r8d %r9d %r10d %r11d %r12d %r13d %r14d %r15d
25
26
27
Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Carnegie Mellon
%rdx 0xf000 %rcx 0x0100
28
Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Carnegie Mellon
Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100
29
Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Carnegie Mellon
Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100
30
Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Carnegie Mellon
Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100
31
Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Carnegie Mellon
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 %rdx 0xf000 %rcx 0x0100
32
33
34
Carnegie Mellon
35
Carnegie Mellon
%rip
Registers Current stack top Instruction pointer CF ZF SF OF Condition codes
%rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp
36
Carnegie Mellon
Carry Flag (for unsigned)
Sign Flag (for signed)
Zero Flag
Overflow Flag (for signed)
Example: addq Src,Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)
37
Carnegie Mellon
38
Carnegie Mellon
39
Carnegie Mellon
40
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }
Register Use(s) %rdi Argument x %rsi Argument y %rax Return value
41
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }
long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }
42
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }
long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }
43
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }
long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }
44
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }
long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }
45
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret
46
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret
47
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret
48
Carnegie Mellon
long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret
49
Carnegie Mellon
val = Test ? Then_Expr : Else_Expr;
ntest = !Test; if (ntest) goto Else; val = Then_Expr; goto Done; Else: val = Else_Expr; Done: . . .
val = x>y ? x-y : y-x;
50
Carnegie Mellon
51
Carnegie Mellon
long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }
52
Carnegie Mellon
long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }
long pcount_goto (unsigned long x) { long result = 0; .loop result += x & 0x1; x >>= 1; if(x) goto loop; return result; }
53
Carnegie Mellon
long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }
long pcount_goto (unsigned long x) { long result = 0; .loop result += x & 0x1; x >>= 1; if(x) goto loop; return result; }
54
Carnegie Mellon
movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result
55
Carnegie Mellon
movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result
56
Carnegie Mellon
movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result
57
Carnegie Mellon
movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result
58
Carnegie Mellon
{ Statement1; Statement2; … Statementn; }
59
Carnegie Mellon
60
Carnegie Mellon
61
Carnegie Mellon
62
Carnegie Mellon
#define WSIZE 8*sizeof(int) long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
63
Carnegie Mellon
64
Carnegie Mel
long pcount_for_while (unsigned long x) { size_t i; long result = 0; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
65
Carnegie Mel
long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
66
Carnegie Mel
long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
67
Carnegie Mel
long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
68
Carnegie Mel
long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; i++; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
69
Carnegie Mellon
70
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
71
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
72
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
73
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
74
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
75
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
76
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
77
Carnegie Mellon
long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; i++; } if (i < WSIZE) goto loop; done: return result; }
i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }
78
Carnegie Mellon
79
University of Virginia Feb 2, 2017
Carnegie Mellon
long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; }
long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; }
81