Machine Control
Machine Control
14-513 18-613
Announcements
Lab 1 (datalab)
▪ Due Thurs, Sept. 17, 11:59pm ET
Written Assignment 1 peer grading
▪ Due Wed, Sept. 23, 11:59pm ET
Written Assignment 2 available on Canvas
▪ Due Wed, Sept. 23, 11:59pm ET
Lab 2 (bomblab) will be available at midnight via Autolab
▪ Due Tues, Sept. 29, 11:59 pm ET
Catching Up
Reviewing LEAQ (based on after-class questions)
Reviewing Arithmetic Expressions in ASM
C -> Assembly -> Machine Code
Uses
▪ Computing address/pointer WITHOUT ACCESSING MEMORY
E.g., translation of p = &x[i];
▪
▪ Compute arbitrary expressions of form: b+(s*i)+d, where s = 1, 2, 4, or 8
▪ [also w/o accessing memory]
Example
long m12(long x) Converted to ASM by compiler:
{ leaq (%rdi,%rdi,2), %rax # t = x+2*x
return x*12; salq $2, %rax # return t<<2
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5
Carnegie Mellon
Object Code
Code for sumstore
Assembler
0x0400595:
0x53
▪ Translates .s into .o
0x48 ▪ Binary encoding of each instruction
0x89 ▪ Nearly-complete image of executable code
0xd3
0xe8
▪ Missing linkages between code in different
0xf2 files
0xff Linker
0xff
0xff ▪ Resolves references between files
• Total of 14 bytes
0x48 ▪ Combines with static run-time libraries
0x89 • Each instruction
E.g., code for malloc, printf
▪
0x03 1, 3, or 5 bytes
0x5b • Starts at address
▪ Some libraries are dynamically linked
0xc3 0x0400595 ▪ Linking occurs when program begins
execution
Disassembler
objdump –d sum
▪ Useful tool for examining object code
▪ Analyzes bit pattern of series of instructions
▪ Produces approximate rendition of assembly code
▪ Can be run on either a.out (complete executable) or .o file
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
Carnegie Mellon
Alternate Disassembly
Disassembled
Alternate Disassembly
Disassembled
Object
Code
Dump of assembler code for function sumstore:
0x0400595: 0x0000000000400595 <+0>: push %rbx
0x53 0x0000000000400596 <+1>: mov %rdx,%rbx
0x48 0x0000000000400599 <+4>: callq 0x400590 <plus>
0x89 0x000000000040059e <+9>: mov %rax,(%rbx)
0xd3 0x00000000004005a1 <+12>:pop %rbx
0xe8 0x00000000004005a2 <+13>:retq
0xf2
0xff
0xff
0xff Within gdb Debugger
0x48 ▪ Disassemble procedure
0x89 gdb sum
0x03
0x5b disassemble sumstore
0xc3 ▪ Examine the 14 bytes starting at sumstore
x/14xb sumstore
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21
Carnegie Mellon
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ffReverse engineering
push forbidden by
$0xffffffff
30001005: 68Microsoft
90 10 00 End User License
30 push Agreement
$0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
ZF set when
000000000000…00000000000
SF set when
yxxxxxxxxxxxx...
+ yxxxxxxxxxxxx...
1xxxxxxxxxxxx...
CF set when
1xxxxxxxxxxxx...
+ 1xxxxxxxxxxxx... Carry
1 xxxxxxxxxxxxx...
1 0xxxxxxxxxxxx...
Borrow
- 1xxxxxxxxxxxx...
1xxxxxxxxxxxx...
OF set when
yxxxxxxxxxxxx... a
+ yxxxxxxxxxxxx... b
zxxxxxxxxxxxx... t
z = ~y
(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)
Very often:
testq %rax,%rax
- 0xxxxxxxxxxxx... b
0xxxxxxxxxxxx... t
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 38
Carnegie Mellon
Register Use(s)
int gt (long x, long y)
{ %rdi Argument x
return x > y; %rsi Argument y
}
%rax Return value
Register Use(s)
int gt (long x, long y)
{ %rdi Argument x
Zapped toreturn
all 0’s x > y; %rsi Argument y
}
%rax Return value
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
Jumping
jX Instructions
▪ Jump to different part of code depending on condition codes
▪ Implicit reading of condition codes
jX Condition Description
jmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
jg ~(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)
jb CF Below (unsigned)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 43
Carnegie Mellon
Goto Version
ntest = !Test; ▪ Create separate code regions for
if (ntest) goto Else;
then & else expressions
val = Then_Expr;
goto Done; ▪ Execute appropriate one
Else:
val = Else_Expr;
Done:
. . .
Goto Version
Why?
result = Then_Expr;
▪ Branches are very disruptive to eval = Else_Expr;
instruction flow through pipelines nt = !Test;
▪ Conditional moves do not require if (nt) result = eval;
control transfer return result;
absdiff:
movq %rdi, %rax # x
subq %rsi, %rax # result = x-y
movq %rsi, %rdx
When is subq %rdi, %rdx # eval = y-x
this bad? cmpq %rsi, %rdi # x:y
cmovle %rdx, %rax # if <=, result = eval
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 48
Carnegie Mellon
Risky Computations
val = p ? *p : 0;
Exercise
SetX Condition Description
cmpq b,a like computing a-b w/o setting dest sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
CF set if carry/borrow out from most significant setns ~SF Nonnegative
bit (used for unsigned comparisons) setg ~(SF^OF)&~ZF Greater (signed)
ZF set if a == b setge ~(SF^OF) Greater or Equal (signed)
setl SF^OF Less (signed)
SF set if (a-b) < 0 (as signed)
setle (SF^OF)|ZF Less or Equal (signed)
OF set if two’s-complement (signed) overflow seta ~CF&~ZF Above (unsigned)
setb CF Below (unsigned)
%rax SF CF OF ZF
xorq %rax, %rax 0x0000 0000 0000 0000 0 0 0 1
subq $1, %rax 0xFFFF FFFF FFFF FFFF 1 1 0 0
cmpq $2, %rax 0xFFFF FFFF FFFF FFFF 1 0 0 0
setl %al 0xFFFF FFFF FFFF FF01 1 0 0 0
movzblq %al, %eax 0x0000 0000 0000 0001 1 0 0 0
Exercise
SetX Condition Description
cmpq b,a like computing a-b w/o setting dest sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
CF set if carry/borrow out from most significant setns ~SF Nonnegative
bit (used for unsigned comparisons) setg ~(SF^OF)&~ZF Greater (signed)
ZF set if a == b setge ~(SF^OF) Greater or Equal (signed)
setl SF^OF Less (signed)
SF set if (a-b) < 0 (as signed)
setle (SF^OF)|ZF Less or Equal (signed)
OF set if two’s-complement (signed) overflow seta ~CF&~ZF Above (unsigned)
setb CF Below (unsigned)
%rax SF CF OF ZF
xorq %rax, %rax 0x0000 0000 0000 0000 0 0 0 1
subq $1, %rax 0xFFFF FFFF FFFF FFFF 1 1 0 0
cmpq $2, %rax 0xFFFF FFFF FFFF FFFF 1 0 0 0
setl %al 0xFFFF FFFF FFFF FF01 1 0 0 0
movzblq %al, %eax 0x0000 0000 0000 0001 1 0 0 0
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
Quiz Time!
Check out:
https://canvas.cmu.edu/courses/17808
Body: {
Statement1;
Statement2;
…
Statementn;
}
“Jump-to-middle” translation
Used with -Og Goto Version
goto test;
loop:
While version Body
while (Test) test:
Body if (Test)
goto loop;
done:
Goto Version
Do-While Version if (!Test)
if (!Test) goto done;
goto done; loop:
do Body
Body if (Test)
while(Test); goto loop;
done: done:
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 59
Carnegie Mellon
While Version
Init;
while (Test ) {
Body
Update;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 62
Carnegie Mellon
For-While Conversion
Init long pcount_for_while
i = 0 (unsigned long x)
{
size_t i;
Test long result = 0;
i < WSIZE i = 0;
while (i < WSIZE)
Update {
unsigned bit =
i++
(x >> i) & 0x1;
result += bit;
Body i++;
{ }
unsigned bit = return result;
(x >> i) & 0x1; }
result += bit;
}
Today
long my_switch
(long x, long y, long z) Switch Statement
{
long w = 1; Example
switch(x) {
case 1:
w = y*z; Multiple case labels
break; ▪ Here: 5 & 6
case 2:
w = y/z; Fall through cases
/* Fall Through */ ▪ Here: 2
case 3:
w += z; Missing cases
break; ▪ Here: 4
case 5:
case 6:
w -= z;
break;
default:
w = 2;
}
return w;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 66
Carnegie Mellon
•
Translation (Extended C) •
goto *JTab[x]; •
long my_switch
(long x, long y, long z) Switch Statement
{
long w = 1; Example
switch(x) {
case 1: my_switch:
.L3: w = y*z; cmpq $6, %rdi # x:6
break; ja .L8 # if x > 6 jump
case 2: # to default
.L5: w = y/z; jmp *.L4(,%rdi,8)
/* Fall Through */
case 3:
.L9: w += z;
break; .section .rodata
case 5: .align 8
case 6: .L4:
.quad .L8 # x = 0
.L7: w -= z; .quad .L3 # x = 1
break; .quad .L5 # x = 2
default: .quad .L9 # x = 3
.quad .L8 # x = 4
.L8: w = 2;
.quad .L7 # x = 5
} .quad .L7 # x = 6
return w;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 68
Carnegie Mellon
Code Blocks (x == 1)
switch(x) { .L3:
case 1: // .L3 movq %rsi, %rax # y
w = y*z; imulq %rdx, %rax # y*z
break; ret
. . .
}
Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
Handling Fall-Through
long w = 1;
. . .
switch(x) { case 2:
. . . w = y/z;
case 2: goto merge;
w = y/z;
/* Fall Through */
case 3:
w += z;
break;
. . .
case 3:
}
w = 1;
merge:
w += z;
Code Blocks (x == 2, x == 3)
.L5: # Case 2
long w = 1; movq %rsi, %rax
. . . cqto # sign extend
switch(x) { # rax to rdx:rax
. . . idivq %rcx # y/z
case 2: jmp .L6 # goto merge
w = y/z; .L9: # Case 3
/* Fall Through */ movl $1, %eax # w = 1
case 3: .L6: # merge:
w += z; addq %rcx, %rax # w += z
break; ret
. . .
} Register Use(s)
%rdi Argument x
%rsi Argument y
%rcx z
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 75
Carnegie Mellon
Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 76
Carnegie Mellon
Summarizing
C Control
▪ if-then-else
▪ do-while
▪ while, for
▪ switch
Assembler Control
▪ Conditional jump
▪ Conditional move
▪ Indirect jump (via jump tables)
▪ Compiler generates code sequence to implement more complex control
Standard Techniques
▪ Loops converted to do-while or jump-to-middle form
▪ Large switch statements use jump tables
▪ Sparse switch statements may use decision trees (if-elseif-elseif-else)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 77
Carnegie Mellon
Summary
Today
▪ Control: Condition codes
▪ Conditional branches & conditional moves
▪ Loops
▪ Switch statements
Next Time
▪ Stack
▪ Call / return
▪ Procedure call discipline
% gdb switch
(gdb) x /8xg 0x4007f0
0x4007f0: 0x0000000000400614 0x00000000004005f0
0x400800: 0x00000000004005f8 0x0000000000400602
0x400810: 0x0000000000400614 0x000000000040060b
0x400820: 0x000000000040060b 0x2c646c25203d2078
(gdb)
. . .
4005f0: 48 89 f0 mov %rsi,%rax
4005f3: 48 0f af c2 imul %rdx,%rax
4005f7: c3 retq
4005f8: 48 89 f0 mov %rsi,%rax
4005fb: 48 99 cqto
4005fd: 48 f7 f9 idiv %rcx
400600: eb 05 jmp 400607 <switch_eg+0x27>
400602: b8 01 00 00 00 mov $0x1,%eax
400607: 48 01 c8 add %rcx,%rax
40060a: c3 retq
40060b: b8 01 00 00 00 mov $0x1,%eax
400610: 48 29 d0 sub %rdx,%rax
400613: c3 retq
400614: b8 02 00 00 00 mov $0x2,%eax
400619: c3 retq