Implementing Load Instructions for Computing Fibonacci Numbers
.text
.global main
main:
MOV R1, #34 /* set values on the registers 1 to 3 */
MOV R2, #54
MOV R3, #21
MOV R0, #0 /* register to hold answer */
MOV R0, R1 /* assume R1 is the largest value */
CMP R0, R2 /* see if R0 is bigger than R2 */
BGE ifR3 /* if so, now compare with R3 */
MOV R0, R2 /* else, R2 is the biggest, save in R0 */
ifR3:
CMP R0, R3 /* see if R0 is bigger than R3 */
BGE end /* if so, R0 is the biggest, end program */
MOV R0, R3 /* else, R3 is the biggest, save in R0 */
end:
BX LR /* return and exit program */
.text
.global main
main:
MOV R0, #23 /* counter of terms to generate only 25 terms (23 + the 2 initial ones)*/
MOV R1, #1 /* first fibonacci number */
MOV R2, #1 /* second fibonacci number */
fibloop:
ADD R3, R1, R2 /* calculate new Fibonacci number adding the 2 previous ones */
MOV R1, R2 /* move the second number down for the next iteration */
MOV R2, R3 /* set sum as a new second number to sum for next iteration */
SUBS R0, R0, #1 /* decrement term counter */
BNE fibloop /* repeat while the counter is not zero */
MOV R0, R2 /* save last fibonacci number in R0 for returning it */
end:
BX LR /* return and exit program */
.text
.global main
main:
/* load values on the registers 1 to 3 */
LDR R1, =N1
LDR R1, [R1] /* load N1 value into R1 */
LDR R2, =N2
LDR R2, [R2] /* load N2 value into R2 */
LDR R3, =N3
LDR R3, [R3] /* load N3 value into R3 */
MOV R0, #0 /* register to hold answer */
MOV R0, R1 /* assume R1 is the largest value */
CMP R0, R2 /* see if R0 is bigger than R2 */
BGE ifR3 /* if so, now compare with R3 */
MOV R0, R2 /* else, R2 is the biggest, save in R0 */
ifR3:
CMP R0, R3 /* see if R0 is bigger than R3 */
BGE endif /* if so, R0 is the biggest, end comparisons */
MOV R0, R3 /* else, R3 is the biggest, save in R0 */
endif:
LDR R4, =LARGEST
STR R0, [R4] /* save largest value in the variable */
end:
BX LR /* return and exit program */
.data
.balign 4
/* declare the 3 numbers to use */
N1: .word 34
N2: .word 54
N3: .word 21
/* place to save the largest value */
LARGEST: .skip 4
.text
.global main
main:
MOV R0, #23 /* counter of terms to generate only 25 terms (23 + the 2 initial ones)*/
LDR R4, =fib_numbers /* get the address of the fibobacci number array into R4 */
MOV R1, #1 /* first fibonacci number */
STR R1, [R4], #4 /* save number in array, increment pointer */
MOV R2, #1 /* second fibonacci number */
STR R2, [R4], #4 /* save number in array, increment pointer */
fibloop:
ADD R3, R1, R2 /* calculate new Fibonacci number adding the 2 previous ones */
MOV R1, R2 /* move the second number down for the next iteration */
MOV R2, R3 /* set sum as a new second number to sum for next iteration */
STR R2, [R4], #4 /* save number in array, increment pointer */
SUBS R0, R0, #1 /* decrement term counter */
BNE fibloop /* repeat while the counter is not zero */
MOV R0, R2 /* save last fibonacci number in R0 for returning it */
end:
BX LR /* return and exit program */
.data
.balign 4
/* place to save the first 25 Fibonacci numbers (25*4), initialize to zero */
fib_numbers: .skip 100, 0