ELEC40006-P1-CW/testing/fibonacci_not_completed.txt
2020-06-12 17:52:26 +01:00

40 lines
1.6 KiB
Plaintext

Comments:
R1 holds n, for calculating the nth Fibonacci number
R2 holds the number 1, for comparing <=1
R3 will hold the answers, the nth Fibonacci number (and any intermediate results)
R4
R5
(0x0) LDA R1 ##MEMORY_LOCATION_OF_n## (loads n into R1)
(0x1) LDA R2 ##MEMORY_LOCATION_CONTAINING_0x1## (loads number 1 into R2)
(0x2) LDA R3 ##MEMORY_LOCATION_OF_0x## (loads 0 into R3, we set 0 as the initial result)
(0x3) LDA R4 ##MEMORY_LOCATION_OF_n## (loads n into R4)
(0x4) LDA R6 ##MEMORY_LOCATION_CONTAINING_0xA## (loads 0xB, memory location of program end to R6)
(0x5) LDA R7 ##MEMORY_LOCATION_CONTAINING_0x6## (loads 0x6, memory location for jumping back)
(0x6) JC4 R6 R4 (if R4, in this case, n, is equal to 0, then jump to end, STP)
(0x7) MLA R1 R2 R3 (multiply and add, same as in the source code y=y*a+b)
(0x8) ADD R5 R5 R1 (adds y to the sum, so that sum+=y, as in the source code)
(0x9) SBO R4 R4 (decrease R4, i.e. n by one, so no infinite loops occur)
(0xA) JMP R7 (jumps back to check whether n>0 and if so, repeats again)
(0xB) STP
MOV
JC6 ... R1 R2
PSH R2
Example that can be used with the instruction generator program:
Requires setting the following data memory location:
Set location '0' to value of n;
Set location '1' to value of 1 (decimal number);
Set location '2' to value of 0 (decimal number)