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.