Eroxl's Notes
06 - Static Control Flow (CPSC 213 Practice)

Problem 1

What C code performs the same computation as the following assembly code:

ld $a, r0
ld (r0), r1
ld $-10, r2
add r1, r2
bgt r2, L0
ld $0, r1
br L1
 
L0: ld $1, r1
    br L1

L1: st r1, (r0)
  • [ ] (a)
if(a <= 0) a = 1;
else a = 0;
  • [ ] (b)
if(a <= 10) a = 1;
else a = 0;
  • [ ] (c)
if(a > 0) a = 1;
else a = 0;
  • [x] (d)
if(a > 10) a = 1;
else a = 0;
  • [ ] None of the above.

Problem 2

Indicate below which piece of C code performs the same computation as the following assembly code. You will note that this code uses r1 without initializing it. This is intentional. Your answer must work for all possible initial values of r1.

ld  $a, r0
ld  $0, r2

L0: ld  (r0, r1, 4), r4
    inc r1
    add r4, r2
    ld  $s, r4
    st r2, (r4)
    ld $i, r4
    st r1, (r4)
    ld  $-10, r3
    add r1, r3
    beq r3, L1
    br  L0

L1: halt
  • [ ] (a)
s = 0;
while (i != 10) {
    i += 1;
    s += a[i];
}
  • [ ] (b)
s = 0;
while (i != 10) {
    s += a[i];
    i += 1;
}
  • [ ] (c)
s = 0;
do {
    i += 1;
    s += a[i];
} while (i != 10)
  • [x] (d)
s = 0;
do {
    s += a[i];
    i += 1;
} while (i != 10)
  • [ ] None of the above