Study Guide 4
Quizzes are closed book. You can bring one written cheat sheet. 30 minutes in class.
Topics:
Everything from Study Guide 3, plus
-
Generating assembly from C code
-
Assembly basics: registers, instructions, operands, memory forms
Short answers
Can you answer the following?
-
What is assembly language?
-
How are data types handled in assembly?
-
What is a register?
-
What does the
leainstruction do? -
How are parameters passed to functions?
-
How is machine code generated from source code? What is the relationship between machine code and assembly?
-
What do the registers %rsp and %rbp represent? How do they relate to functions?
-
What the do the registers %rax and %eax typically represent? When do we use one versus the other?
-
What does the CMP function do?
-
What does the TEST function do?
-
What two steps occur when the CPU executes the instruction
push %rbp? -
What two steps occur when the CPU executes the instruction
pop %rbp? -
What does
callqdo? -
What does
retqdo?
Instruction practice
1) What would the values of ZF and SF be for the following comparisons if
-
%rax has the value 0
-
%edi has the value 12
-
%esi has the value 3
cmp $0, %rax
cmp %esi, %edi
test %edi, %esi
test %rax, %rax
2) Re-write the following code using goto statements
int getSmallest(int x, int y) {
int smallest;
if ( x > y ) { //if (conditional)
smallest = y; //then statement
}
else {
smallest = x; //else statement
}
return smallest;
}
3) Re-write the following code to use conditional move statements.
int get_min(int a, int b, int c, int d)
{
int result = 0;
if (a < b && a < c && a < d) result = a;
else if (b < a && b < c && b < d) result = b;
else if (c < a && c < b && c < d) result = c;
else if (d < a && d < b && d < c) result = d;
return result;
}
4) Consider the following program
int getSmallest(int x, int y) {
int smallest;
if ( x > y ) {
smallest = y;
}
else {
smallest = x;
}
return smallest;
}
The following table has x86_64 assembly code that corresponds to the above code. For each instruction, explain its meaning, and a description of how it corresponds to the original code. The first instruction has been done for you, as an example.
Instruction |
Meaning |
Description |
mov %edi,-0x14(%rbp) |
||
mov %esi,-0x18(%rbp) |
||
mov -0x14(%rbp),%eax |
||
cmp -0x18(%rbp),%eax |
||
jle 0x4005b0 <getSmallest+26> |
||
mov -0x18(%rbp),%eax |
||
jmp 0x4005b9 <getSmallest+35> |
||
mov -0x14(%rbp),%eax |
5) Consider the following program
int getSmallest_cmov(int x, int y) {
return x > y ? y : x;
}
Instruction |
Meaning |
Description |
push %rbp |
%rsp = %rsp - $0x8, M[%rsp] = %rbp |
save %rbp |
mov %rsp,%rbp |
$rbp = %rsp |
update %rbp |
mov %edi,-0x4(%rbp) |
||
mov %esi,-0x8(%rbp) |
||
mov -0x8(%rbp),%eax |
||
cmp %eax,-0x4(%rbp) |
||
cmovle -0x4(%rbp),%eax |
||
pop %rbp |
%rbp = M[%rsp], %rsp = %rsp + $0x8 |
restore %rbp |
retq |
pop %rip |
Return from the function |
Operands
Fill in the following table
Suppose memory has the following values:
|
And suppose our registers have the following values to start:
|
Operand |
Form |
Translation |
Value |
%eax |
|||
(%eax) |
|||
(%rsp,%esi) |
|||
(%rsp,%edi,%edx) |
|||
0x300(,%esi,2) |
|||
$0x310 |
|||
0x310 |
Tracing assembly
Dump of assembler code for function main:
=> 0x0000555555555149 <+0>: endbr64
0x000055555555514d <+4>: push %rbp
0x000055555555514e <+5>: mov %rsp,%rbp
0x0000555555555151 <+8>: sub $0x10,%rsp
0x0000555555555155 <+12>: movl $0xfffffff7,-0x8(%rbp)
0x000055555555515c <+19>: mov -0x8(%rbp),%eax
0x000055555555515f <+22>: shl $0x2,%eax
0x0000555555555162 <+25>: mov %eax,-0x4(%rbp)
0x0000555555555165 <+28>: mov -0x4(%rbp),%edx
0x0000555555555168 <+31>: mov -0x8(%rbp),%eax
0x000055555555516b <+34>: mov %eax,%esi
0x000055555555516d <+36>: lea 0xe90(%rip),%rdi # 0x555555556004
0x0000555555555174 <+43>: mov $0x0,%eax
0x0000555555555179 <+48>: callq 0x555555555050 <printf@plt>
0x000055555555517e <+53>: mov $0x0,%eax
0x0000555555555183 <+58>: leaveq
0x0000555555555184 <+59>: retq
Suppose memory has the following values:
|
And suppose our registers have the following values to start:
|
1) What is the value 0x10 as a base 10 integer?
2) What is the value 0xfffffff7 as a base 10 signed integer (two’s complement)?
3) Draw the contents of the registers and stack after executing the instruction sub $0x10,%rsp ( 0x0000555555555151)
|
"Stack top"
|
4) What is the translation of the memory form -0x8(%rbp)?
5) What is the shl instruction?
6) What are the contents of %eax after executing instruction 0x000055555555515c?
7) What are the contents of %eax after executing instruction 0x000055555555515f?
8) What are the contents of %rsi after executing instruction 0x000055555555516b?
9) What are the contents of %rdi after executing instruction 0x000055555555516d?