You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.6 KiB
ArmAsm

.global evenFib
.section .text
evenFib:
init:
PUSH {R4} @The ARM calling convention only allows callee functions to use R0-R3. Since I need R4 as well, I am going to push R4 onto the stack, and pop it off when I am done using it.
MOV R0,#0 @Return value - where all the addition is going to happen to
LDR R4,=#4000000 @ARM doesn't allow you to 'MOV' constants greater than 8 bits. To get around that, we use the LDR instruction, which lets the assembler place the value in memory, and then load it from there.
MOV R1,#1 @Main fibonacci number
MOV R2,#0 @Used to hold temporary values when incrementing the main fibonacci number
MOV R3,#0 @Previous fibonacci number - This will be 0 at the start
loop:
incr:
MOV R2,R1 @Store the current fibonacci number in R2
ADD R1,R1,R3 @Add the previous fibonacci number with the current one
MOV R3,R2 @Move R2 (the 'old current' fibonacci number) into R3
checkbnd:
CMP R1,R4 @Check if R1 is greater than the upper bound for fibonacci numbers
BGE return @If it is, then we return
checkparity: @Parity - The fact of being even or odd.
TST R1,#0x1 @Equivalent of an ANDS operation, but the result is discarded. In this case, we are checking if R1's parity by checking the last bit, which would be set to 0 if it is even, and 1 if it is odd.
BNE loop @If R1 is odd, don't do the addition. Instead, go back to the loop, where we move on to the next fibonacci number.
isEven: @The program goes here if R1 is even
ADD R0,R0,R1 @Add the fibonacci number to R0
B loop
return:
POP {R4} @Pop the R4 that we pushed onto the stack earlier
BX lr
.section .data