Eroxl's Notes
02 - CPU & Static Variables (CPSC 213 Practice)

Problem 1

When the following instruction executes (i.e., after it has been read into memory), the instruction copies the address of the constant a, without reading its value from the memory, in a register.

For the purposes of this question, a register is not considered a memory location.

ld $a, r0
  • [x] (a) True
  • [ ] (b) False

Problem 2

The following instruction is legal, and when it executes it reads a value from memory and places it in a register.

Assume that the registers have been preloaded with appropriate values.

For the purposes of this question, a register is not considered a memory location.

ld (r0), r1
  • [ ] (a) True
  • [x] (b) False

Problem 3

The following instruction is legal, and when it executes it copies a value from one memory location to another.

Assume that the registers have been preloaded with appropriate values.

For the purposes of this question, a register is not considered a memory location.

st r0, (r1)
  • [x] (a) True
  • [ ] (b) False

Problem 4

If r1 = and r2 = . Which assembly code will store the value 26 in register r1?

  • [ ] (a) add r1, $21
  • [ ] (b) add r1, r2
  • [x] (c) add r2, r1
  • [ ] (d) add $21, r1
  • [ ] (e) r[1] ← r[1] + r[2]

Select all possible options that apply.

Problem 5

Assume r1 = . Which assembly code will store the value in register r1?

  • [ ] (a)
shl $2, r1 
  • [ ] (b)
ld $2, r2
mul r2, r1
  • [ ] (c)
mul $2, r1
  • [ ] (d)
ld $1, r2
shr r2, r1
  • [x] (e)
shl $1, r1
  • [ ] (f)
shr $2, r1
  • [ ] (g)

  • [ ] (h)

shr $1, r1
  • [ ] (i)
ld $1, r2
shl r2, r1

Select all possible options that apply.

Problem 6

Assume r1 = . Which assembly code will store the value in register r1?

  • [ ] (a)
ld $8, r2
div r2, r1
  • [ ] (b)
div $8, r1
  • [ ] (c)
  • [ ] (d)
shl $8, r1
  • [ ] (e)
ld $8, r2
shr r2, r1
  • [ ] (f)
shr $8, r1
  • [ ] (g)
ld $3, r2
shl r2, r1
  • [ ] (h)
shl $3, r1
  • [x] (i)
shr $3, r1

Select all possible options that apply.

Problem 7

.question { margin-top: 1em; } div.question:not(:last-child) { padding-bottom: 1em; border-bottom: 1px dotted grey; }

Given the following description (in RTL or human-readable text), write out the corresponding assembly and machine language translation.

Make sure your answer follows these rules:

  • Write any number values in the assembly in base-10.
  • If there is no offset, write 0 in the assembly
    • eg. Write ld 0(r3), r4 and not ld (r3), r4, although both are valid in the simulator.
  • If the machine language does not specify a value, write F instead.
    • eg. The load immediate instruction ld $1, r1 would have the machine language translation 01FF00000001.

(a).

  • SM213 Assembly: inc r7
  • Machine Language: 0x63F7

(b). Subtract 4 from the Value in R7

  • SM213 Assembly: deca r7
  • Machine Language: 0x66F7

Problem 8

Which of the following assembly-language instructions would be a way to compute the following C statement (where b is a static array of ints)?

b[3] = 18;

Assume that registers have already been initialized with the following values:

  • r0 = 18

  • r1 = base address of b

  • r2 = 3

  • r3 = 12

  • [ ] (a)

ld r0, 12(r1)
  • [ ] (b)
add r3, r1
ld r0, (r1)
  • [ ] (c)
st r0, (r2, r1, 4)
  • [ ] (d)
ld r0, (r1, r2, 4)
  • [x] (e)
st r0, 12(r1)
  • [x] (f)
st r0, (r1, r2, 4)
  • [ ] (g)
ld r0, (r1)
  • [x] (h)
add r3, r1
st r0, (r1)
  • [ ] (i)
st r0, (r1)

Select all possible options that apply.

Problem 9

Consider the following assembly code:

.pos 0x100
	 ld $1, r0
	 ld  $a, r1
	 ld (r1, r0, 4), r2
	 inca r0
	 st r2, (r1, r0, 4)

.pos 0x1000
a:  .long 13
	.long 19
	.long 18
	.long 10
	.long 9

What are the values of the array a after this code executes?

[13, 19, 18, 10, 9]

Problem 10

Consider the following assembly code:

.pos 0x100
     ld  $a, r0
     ld  $b, r1
     ld  (r0), r0
     ld  (r1, r0, 4), r2
     ld  (r1, r2, 4), r3
     ld  (r1, r3, 4), r4
     st  r2, (r1, r4, 4)
    
.pos 0x1000
a:  .long 0
b:  .long 2
    .long 3
    .long 1
    .long 0

What are the values stored in array b after this code executes?

[2, 3, 1, 2]

Problem 11

Consider the following C global variable declaration.

int array[20];
int var;

Translate the following C statement into assembly language.

Use labels to refer to the memory address of each variable. Assume that these labels have already been defined; i.e., do not include their definition in your answer. Also, do not include any assembly directives such as .pos or .long.

Translate this C code

var = var + 1

Into SM213 Assembly code

ld $var, r0
ld (r0), r1
inc r1
st r1, (r0)