2IC30 · assessment
PP2 simulation exercises
Thirteen editable PP2 assembly exercises imported from the original simulator bank.
1/13
Not started
Subroutine Sum receives n on the stack and returns 1+2+…+n in the caller-reserved result slot. The driver (given) loads n=5 and copies the returned value to memory[100] so it can be checked. Write the body of Sum . Expected: 15 .
Self-mark against the exam rubric
0/5 criteria met
Compute n! with a countdown loop. The driver loads n=5 and the result is published to memory[100]. Write the loop that leaves n! in R1. Expected: 120 .
Self-mark against the exam rubric
0/4 criteria met
Determine whether n is prime by trial division. The driver loads n=7 (prime). Write code that sets R1 = 1 if n is prime, else 0. Result goes to memory[100]. Change LOAD R0 7 to 9 to test a composite. Expected: 1 .
Self-mark against the exam rubric
0/5 criteria met
Find the largest element of a 5-element array preloaded at memory[200..204] = {3,9,2,7,5}. The pointer (R0=200) and count (R1=5) are given. Walk the array and leave the maximum in R2. Result to memory[100]. Expected: 9 .
Self-mark against the exam rubric
0/4 criteria met
Iterative Fibonacci: fib(0)=0, fib(1)=1, fib(k)=fib(k-1)+fib(k-2). The driver loads n=10. Write the loop that leaves fib(n) in R1. Result to memory[100]. Expected: 55 .
Self-mark against the exam rubric
0/4 criteria met
Greatest common divisor with Euclid: gcd(a,b) = gcd(b, a mod b) until b = 0. The driver loads a=48, b=36. Write the loop that leaves gcd in R0. Uses the MOD instruction. Result to memory[100]. Expected: 12 .
Self-mark against the exam rubric
0/4 criteria met
Compute x n by repeated multiplication. The driver loads x=2, n=10. Write the loop that leaves the result in R2. Result to memory[100]. Expected: 1024 .
Self-mark against the exam rubric
0/4 criteria met
Return the index of the first element equal to 7 in a 5-element array at memory[200..204] = {3,9,2,7,5}, or −1 if absent. Pointer R0=200, count R1=5, target R2=7 are given. Leave the index (or −1) in R3. Result to memory[100]. Expected: 3 .
Self-mark against the exam rubric
0/4 criteria met
Compute a × b using only addition — the classic 'no multiply instruction' exercise. The driver loads a=6, b=7. Write the loop that leaves the product in R2. Result to memory[100]. Expected: 42 .
Self-mark against the exam rubric
0/4 criteria met
Reverse the 5-element array at memory[200..204] = {1,2,3,4,5} in place using two pointers that meet in the middle. Left pointer R0=200 and right pointer R1=204 are given. Swap from the ends inward. The check reads memory[200] (should be 5) into memory[100]. Expected: 5 .
Self-mark against the exam rubric
0/4 criteria met
Recursive n! exercising the full calling convention: fac(n) = 1 if n≤1, else n·fac(n−1). The driver loads n=5, reserves a result slot, pushes n, calls with BRS , and publishes the returned value to memory[100]. Write the body of Fac — it must save n across its recursive call. Expected: 120 .
Self-mark against the exam rubric
0/5 criteria met
Real 2018 exam, Question 5. Translate IsPrime(n) to a PP2 subroutine. The parameter and result share one stack slot: caller does PUSH R0 / BRS IsPrime / PULL R0 . The subroutine builds a boolean array a [0..n] on the stack ( SUB SP R0 allocates n+1 words; a [i] = RAM[SP+i]), marks all true, then sieves out multiples, and returns a [n]. n is at SP+1 on entry; the result goes back to SP+1. Driver loads n=7. Write the body of IsPrime . Expected: 1 (7 is prime).
Self-mark against the exam rubric
0/5 criteria met
Real 2017 exam, Question 5. Translate the procedure addSquares(m,n,a) that computes c = Σ i<m Σ j<n a[i·n+j]² to a PP2 subroutine. Parameters are on the stack: m at SP+1, n at SP+2, base address a at SP+3; result to SP+4. Element a[i·n+j] is RAM[a+i·n+j]. Overflow ( BVS ) jumps to exception . The driver passes a 2×2 matrix {1,2,3,4} (m=n=2). Write the body of addSquares . Expected: 30 (1²+2²+3²+4²).
Self-mark against the exam rubric
0/5 criteria met