Skip to main content
Logo image

Dive Into Systems: Exercises

Section 9.2 Common Instructions

Exercise 9.2.1. Limitations of Operand Usage.

    Which of the following are valid ARM instructions? Select ALL that apply.
  • add x0, #8
  • Correct!
  • add x0, [x8]
  • Incorrect. Need to load from memory before you can use a value.
  • add x0, x8
  • Correct!
  • add #8, x0
  • Incorrect. Source and destination are switched.
  • ldr x0, [x8]
  • Correct!

Exercise 9.2.2. Understanding Addressing Modes, Program Stack.

Given the following assembly code:
sub sp, sp, #8
str x29, [sp]
mov x29, sp
str x0, [x29, #-8]

(a)

    Select all the statements that are true when the following instructions are executed:
    sub sp, sp, #8
    str x29, [sp]
    
  • The call stack is updated
  • Correct!
  • The value in register sp is updated.
  • Correct!
  • The value in register x29 is updated.
  • Incorrect.

(b)

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

(c)

    Select all the statements that are true when the following instruction is executed:
    str x0, [x29, #-8]
  • The value in register x0 is updated.
  • Incorrect.
  • The call stack is updated.
  • Correct!
  • The value in register x29 is updated.
  • Incorrect.

Exercise 9.2.3. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
sp 0xac0
x29 0xae0
x0 0xf1
x1 0xe2
For each location, indicate (in hex) the values stored after the following instructions are executed:
sub sp, sp, #8
str x29, [sp]
mov x29, sp
str x1, [x29, #-8]
ldr x0, [x29, #-8]
sub x0, x0, #2
  • sp
  • x29
  • [x29, #-8]
  • x0
  • x1

Exercise 9.2.4. Practice Tracing Assembly.

Given the following register table populated with initial values:
Register Value
sp 0xac0
x29 0xae0
x0 0x2c
x1 0x28
x2 0x20
Indicate (in hex) the values stored in memory and the register table after the following instructions are executed:
sub sp, sp, #0x8
str x29, [sp]
mov x29, sp
str x1, [x29, #-0x8]
str x2, [x29, #-0x10]
ldr x0, [x29, #-0x8]
sub x0, x0, x2
ldr x29, [sp]
add sp, sp, #0x8
Call Stack (Grows toward lower addresses)
  • 0xaa8
  • 0xab0
  • 0xab8
Register Table
  • x0
  • x29
Hint.
Consider drawing out the stack by hand to aid you in solving this problem.
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 9.2.5. Understanding Addressing Modes, how operations affect registers and memory. Also addresses the use of the x29 register..

    Given the following values in memory:
    0xe3a8: 0x20
    0xe3b0: 0x28
    0xe3b8: 0x0
    0xe3c0: 0xe3e0
    
    Suppose register x29 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 x0?
  • ldr x1, [x29, #0x10]
    ldr x2, [x29, #0x18]
    add x0, x1, x2
    
  • Incorrect.
  • ldr x1, [x29, #0x10]
    ldr x2, [x29, #0x18]
    add x0, x1, x2
    
  • Correct!
  • ldr x1, [x29 #-0x10]
    add x0, x1, [x29, #-0x18]
    
  • Incorrect.

Exercise 9.2.6. Understanding Addressing Modes, add Instruction.

    True or False: These two sets of instructions will place the same value in the register x0.
    str    x1, [x29, #-0x8]
    str    x2, [x29, #-0xc]
    mov    x3, #0x0
    str    x3, [x29, #-0x4]
    add    x1, x1, x2
    mov    x0, x1
    
    str    x1, [x29, #-0x8]
    str    x2, [x29, #-0xc]
    mov    x3, #0x0
    str    x3, [x29, #-0x4]
    add    x2, x2, x1
    mov    x0, x2
    
  • true
  • Correct!
  • false
  • Incorrect.