Skip to main content
Logo image

Dive Into Systems: Exercises

Section 7.2 Common Instructions

Exercise 7.2.1. Limitations of Operand Usage.

    Which of the following are valid x86_64 instructions? Select ALL that apply.
  • add %rax, $8
  • Incorrect.
  • add %rax, (%rbx)
  • Correct!
  • add (%rax), %rbx
  • Correct!
  • add (%rax), (%rbx)
  • Incorrect.
  • add (%rax, %rbx, 10), %rcx
  • Incorrect.

Exercise 7.2.2. Understanding Addressing Modes, Program Stack.

Given the following assembly code:
push %rbp
mov %rsp, %rbp
mov %rdi, -0x8(%rbp)

(a)

    Select all the statements that are true when the following instruction is executed:
    push %rbp
  • The call stack is updated
  • Correct!
  • The value in register %rsp is updated.
  • Correct!
  • The value in register %rbp is updated.
  • Incorrect.
Answer.
Recall that the instruction push %rbp is equivalent to the following sequence of two instructions: sub $8, %rsp and mov %rbp, (%rsp)

(b)

    Select all the statements that are true when the following instruction is executed:
    mov %rsp, %rbp
  • The call stack is updated.
  • Incorrect.
  • The value in register %rsp is updated.
  • Incorrect.
  • The value in register %rbp is updated.
  • Correct!

(c)

    Select all the statements that are true when the following instruction is executed:
    mov %rdi, -0x8(%rbp)
  • The value in register %rdi is updated.
  • Incorrect.
  • The call stack is updated.
  • Correct!
  • The value in register %rbp is updated.
  • Incorrect.

Exercise 7.2.3. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
%rsp 0xAC0
%rbp 0xAE0
%rax 0xF1
%rdi 0xE2
For each location, indicate (in hex) the values stored after the following instructions are executed:
push %rbp
mov %rsp, %rbp
mov %rdi, -0x8(%rbp)
mov -0x8(%rbp), %rax
sub $0x2, %rax
  • %rsp
  • %rbp
  • -0x8(%rbp)
  • %rax
  • %rdi
Hint.
Remember the instruction push %rbp is equivalent to the sequence of instructions sub $0x8, %rsp and mov %rbp, (%rsp).
Answer.
Click here to use ASM Visualizer
 1 
asm.diveintosystems.org
to trace of the code. Note that the trace shows the full stack addresses, while we only show the lower two bytes of the addresses in the example above.

Exercise 7.2.4. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
%rsp 0xAC0
%rbp 0xAE0
%rax 0x2C
%rdi 0x28
%rdx 0x20
Indicate (in hex) the values stored in memory and the register table after the following instructions are executed:
push %rbp
mov %rsp, %rbp
mov %rdi, -0x8(%rbp)
mov %rdx, -0x10(%rbp)
mov -0x8(%rbp), %rax
sub -0x10(%rbp), %rax
pop %rbp
Call Stack (Grows toward lower addresses)
  • 0xAA8
  • 0xAB0
  • 0xAB8
Register Table
  • %rax
  • %rbp
Hint 1.
Remember the instruction push %rbp is equivalent to the sequence of instructions sub $0x8, %rsp and mov %rbp, (%rsp). Likewise, the instruction pop %rbp is equivalent to the sequence of instructions mov (%rsp), %rbp and add $0x8, %rsp.
Hint 2.
Consider drawing out the stack by hand to aid you in solving this problem.
Answer.
Click here to use ASM Visualizer
 2 
asm.diveintosystems.org
to trace of the code. Note that the trace shows the full stack addresses, while we only show the lower two bytes of the addresses in the example above.

Exercise 7.2.5. Understanding Addressing Modes, the add instruction, how operations affect registers and memory. Also addresses the use of the %rip register..

    Given the following values in memory:
    0xe3a8: 0x20
    0xe3b0: 0x28
    0xe3b8: 0x0
    0xe3c0: 0xe3e0
    
    Suppose register %rbp contains the value 0xe3c0. Which set of instructions will add the values in memory location 0xe3b0 and 0xe3a8 together and store the result in the register %eax?
  • mov 0x10(%rbp), %rax
    add 0x18(%rbp), %rax
    
  • Incorrect.
  • mov -0x10(%rbp), %rax
    add -0x18(%rbp), %rax
    
  • Correct!
  • add -0x10(%rbp), -0x8(%rbp)
    mov -0x18(%rbp), %rax
    
  • Incorrect.

Exercise 7.2.6. Understanding Addressing Modes, add Instruction.

    True or False: These two sets of instructions will place the same value in the register %eax.
    0x4eb mov    %edi,-0x8(%rbp)
    0x4ee mov    %esi,-0xc(%rbp)
    0x4f1 movl   $0x0,-0x4(%rbp)
    0x4f8 add    -0xc(%rbp),%edi
    0x4fe mov    %edi,%eax
    
    0x4eb mov    %edi,-0x8(%rbp)
    0x4ee mov    %esi,-0xc(%rbp)
    0x4f1 movl   $0x0,-0x4(%rbp)
    0x4f8 add    -0x8(%rbp),%esi
    0x4fe mov    %esi,%eax
    
  • true
  • Correct!
  • false
  • Incorrect.