Question 10
AQA Computer Science June 2019 Paper 2A level8 marks
On the specification
- 3.4 Theory of computation3.7 Fundamentals of computer organisation and architecture
Question text
The greatest common divisor of two positive integers A and B is the largest positive box 10 integer that divides both of the numbers without leaving a remainder. For example, if A = 4 and B = 6 then: • 4 has the divisors 1, 2 and 4 • 6 has the divisors 1, 2, 3 and 6 Therefore, the greatest common divisor of 4 and 6 is 2, since this is the biggest number which appears in the list of divisors of both 4 and 6. The method shown in Figure 7 is a famous method for determining the greatest common divisor of two positive integers, A and B: Figure 7 WHILE A ≠ B IF A > B THEN A = A – B ELSE B = B – A ENDIF ENDWHILE When the procedure described in the algorithm terminates, the value in A (and also B) is the greatest common divisor of A and B. Table 1 – standard AQA assembly language instruction set LDR Rd, <memory ref> Load the value stored in the memory location specified by <memory ref> into register d. STR Rd, <memory ref> Store the value that is in register d into the memory location specified by <memory ref>. ADD Rd, Rn, <operand2> Add the value specified in <operand2> to the value in register n and store the result in register d. SUB Rd, Rn, <operand2> Subtract the value specified by <operand2> from the value in register n and store the result in register d. MOV Rd, <operand2> Copy the value specified by <operand2> into register d. CMP Rn, <operand2> Compare the value stored in register n with the value specified by <operand2>. B <label> Always branch to the instruction at position <label> in the program. B<condition> <label> Branch to the instruction at position <label> if the last comparison met the criterion specified by <condition>. Possible values for <condition> and their meanings are: EQ: equal to NE: not equal to GT: greater than LT: less than AND Rd, Rn, <operand2> Perform a bitwise logical AND operation between the value in register n and the value specified by <operand2> and store the result in register d. ORR Rd, Rn, <operand2> Perform a bitwise logical OR operation between the value in register n and the value specified by <operand2> and store the result in register d. EOR Rd, Rn, <operand2> Perform a bitwise logical XOR (exclusive or) operation between the value in register n and the value specified by <operand2> and store the result in register d. MVN Rd, <operand2> Perform a bitwise logical NOT operation on the value specified by <operand2> and store the result in register d. LSL Rd, Rn, <operand2> Logically shift left the value stored in register n by the number of bits specified by <operand2> and store the result in register d. LSR Rd, Rn, <operand2> Logically shift right the value stored in register n by the number of bits specified by <operand2> and store the result in register d. HALT Stops the execution of the program. Labels: A label is placed in the code by writing an identifier followed by a colon (:). To refer to a label, the identifier of the label is placed after the branch instruction. Interpretation of <operand2> <operand2> can be interpreted in two different ways, depending on whether the first character is a # or an R: • # – use the decimal value specified after the #, eg #25 means use the decimal value 25. • Rm – use the value stored in register m, eg R6 means use the value stored in register 6. The available general purpose registers that the programmer can use are numbered 0 to 12. Write a program using the AQA assembly language instruction set, shown on box 10.1 page 18 in Table 1, that uses the method described in Figure 7 to calculate the greatest common divisor of two positive integers. • At the start, the positive integer A will be stored in memory location 102 and the positive integer B in memory location 103. Your program should use these values to find their greatest common divisor. • When your program terminates it should store the greatest common divisor of these two numbers in memory location 104. [8 marks]