Skip to main content
Logo image

Dive Into Systems: Exercises

Section 8.2 Common Instructions

Exercise 8.2.1. Limitations of Operand Usage.

    Which of the following are valid x86 instructions? Select ALL that apply.
  • add %eax, $8
  • Incorrect. Destination cannot be a constant.
  • add %eax, (%ebx)
  • Correct!
  • add (%eax), %ebx
  • Correct!
  • add (%eax), (%ebx)
  • Incorrect.
  • add (%eax, %ebx, 10), %ecx
  • Incorrect. Only 1, 2, 4, and 8 are options for scaling factor.

Exercise 8.2.2. Understanding Addressing Modes, Program Stack.

Given the following assembly code:
push %ebp
mov %esp, %ebp
mov %edi, -0x4(%ebp)

(a)

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

(b)

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

(c)

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

Exercise 8.2.3. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
%esp 0xac0
%ebp 0xae0
%eax 0xf1
%edi 0xe2
For each location, indicate (in hex) the values stored after the following instructions are executed:
push %ebp
mov %esp, %ebp
mov %edi, -0x4(%ebp)
mov -0x4(%ebp), %eax
sub $0x2, %eax
  • %esp
  • %ebp
  • -0x8(%ebp)
  • %eax
  • %edi
Hint.
Remember the instruction push %ebp is equivalent to the sequence of instructions sub $0x4, %esp and mov %ebp, (%esp).
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 8.2.4. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
%esp 0xac0
%ebp 0xae0
%eax 0x2c
%edi 0x28
%edx 0x20
Indicate (in hex) the values stored in memory and the register table after the following instructions are executed:
push %ebp
mov %esp, %ebp
mov %edi, -0x4(%ebp)
mov %edx, -0x8(%ebp)
mov -0x4(%ebp), %eax
sub -0x8(%ebp), %eax
pop %ebp
Call Stack (Grows toward lower addresses)
  • 0xab4
  • 0xab8
  • 0xabc
Register Table
  • %eax
  • %ebp
Hint 1.
Remember the instruction push %ebp is equivalent to the sequence of instructions sub $0x4, %esp and mov %ebp, (%esp). Likewise, the instruction pop %ebp is equivalent to the sequence of instructions mov (%esp), %ebp and add $0x4, %esp.
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 8.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:
    0xe3b4: 0x20
    0xe3b8: 0x28
    0xe3bc: 0x0
    0xe3c0: 0xe3e0
    
    Suppose register %rbp contains the value 0xe3c0. Which set of instructions will add the values in memory location 0xE3B8 and 0xe3b4 together and store the result in the register %eax?
  • mov 0x8(%rbp), %rax
    add 0xc(%rbp), %rax
    
  • Incorrect.
  • mov -0x8(%rbp), %rax
    add -0xc(%rbp), %rax
    
  • Correct!
  • add -0x8(%rbp), -0xc(%rbp)
    mov -0x14(%rbp), %rax
    
  • Incorrect.

Exercise 8.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(%ebp)
    0x4ee mov    %esi,-0xc(%ebp)
    0x4f1 movl   $0x0,-0x4(%ebp)
    0x4f8 add    -0xc(%ebp),%edi
    0x4fe mov    %edi,%eax
    
    0x4eb mov    %edi,-0x8(%ebp)
    0x4ee mov    %esi,-0xc(%ebp)
    0x4f1 movl   $0x0,-0x4(%ebp)
    0x4f8 add    -0x8(%ebp),%esi
    0x4fe mov    %esi,%eax
    
  • True
  • Correct!
  • False
  • Incorrect.