Question 1
6 marks
1 6 marks
The pseudocode algorithm here will take in two numbers from the user, multiply them together using addition and output the result. For example, 4 multiplied by 3 would be 4 + 4 + 4 = 12.
You can assume the function input takes in a value as an integer.
numA = input("Enter first number")
numB = input("Enter second number")
answer = 0
while (numB > 0)
answer = answer + numA
numB = numB - 1
endwhile
print(answer)
Write this algorithm in assembly language using the Little Man Computer (LMC) instruction set.
Mark scheme 1
Tick each point your answer makes. Your mark is worked out from your ticks, and you can change it.
-
INP and STA two numbers correctly
-
Appropriate use of branching to exit the loop.
-
SUB ONE and STA NUM
-
LDA ANSWER and ADD NUMA
-
Output (correct) answer.
-
Set DAT for NUMA, NUMB, ONE, ANSWER after HLT
POSSIBLE SOLUTION:
INP
STA NUMA INP
STA NUMB LOOPSTART LDA NUMB
BRZ LOOPEND SUB ONE STA NUMB LDA ANSWER ADD NUMA STA ANSWER BRA LOOPSTART
LOOPEND LDA ANSWER
OUT
HLT
ANSWER D AT 0 ONE DAT 1 NUMA DAT 0 NUMB DAT 0
Guidance
First and second number can be interchanged Accept suitable labels for DAT and Loop points For point 6 the 0s for ANSWER, NUMA, NUMB are optional but ONE must be initialised to 1.
Allow FT for BP5 if logic is correct.
Accept alternative mnemonics identified in the specification.
Examiner’s Comments
Most candidates were given some of the available marks for this question. Most were able to take two inputs and perform the addition of two numbers, storing the result in the accumulator. Candidates that lost marks did not successfully loop through the instructions correctly, didn’t output the correct answer and/or did not make use of DAT to subtract a value correctly.