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

Short answers

Can you answer the following?

  1. What is assembly language?

  2. How are data types handled in assembly?

  3. What is a register?

  4. What does the lea instruction do?

  5. How are parameters passed to functions?

  6. How is machine code generated from source code? What is the relationship between machine code and assembly?

  7. What do the registers %rsp and %rbp represent? How do they relate to functions?

  8. What the do the registers %rax and %eax typically represent? When do we use one versus the other?

  9. What does the CMP function do?

  10. What does the TEST function do?

  11. What two steps occur when the CPU executes the instruction push %rbp?

  12. What two steps occur when the CPU executes the instruction pop %rbp?

  13. What does callq do?

  14. What does retq do?

Instruction practice

1) What would the values of ZF and SF be for the following comparisons if

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:

Address

Value

0x304

0xCA

0x308

0xF0

0x30C

0x03

0x310

0x1E

And suppose our registers have the following values to start:

Register

Value

%eax

0x304

%edx

0x8

%rbp

0x0

%rsp

0x300

%esi

0x4

%edi

0x1

%rip

0x0000555555555149

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:

Address

Value

M[0x555555556004]

"%d %d"

And suppose our registers have the following values to start:

Register

Value

%eax

0xd0d

%edx

0xe148

%rbp

0x0

%rsp

0x048

%esi

0xe138

%edi

0x1

%rip

0x0000555555555149

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)

Register

Value

%eax

%edx

%rbp

%rsp

%esi

%edi

%rip

"Stack top"

Address

Stack value

 

 

 

 

 

 

 

0x048

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?