Compare commits
4 Commits
ff4e6e723e
...
092cc16146
Author | SHA1 | Date | |
---|---|---|---|
092cc16146 | |||
f16a6e64f5 | |||
49237ba657 | |||
be43e7d2a7 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
mult3or5/main
|
||||
evenFibonacci/main
|
||||
|
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## Project Euler Solutions
|
||||
|
||||
This repository contains my solutions to the [Project Euler problems](https://projecteuler.net/archives), written - to the best of my abilities - in 32-bit ARM Assembly.
|
||||
|
||||
The Assembly code was written and tested on a Raspberry Pi 3B, and contains C code to facilitate formatted input and output.
|
3
evenFibonacci/README.md
Normal file
3
evenFibonacci/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## Multiples of 3 or 5
|
||||
|
||||
This directory contains my solution to the ['Even Fibonacci numbers' problem](https://projecteuler.net/problem=2).
|
8
evenFibonacci/main.c
Normal file
8
evenFibonacci/main.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int evenFib();
|
||||
|
||||
void main() {
|
||||
printf("%d\n",evenFib());
|
||||
return;
|
||||
}
|
35
evenFibonacci/main.s
Normal file
35
evenFibonacci/main.s
Normal file
@@ -0,0 +1,35 @@
|
||||
.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
|
3
mult3or5/README.md
Normal file
3
mult3or5/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## Multiples of 3 or 5
|
||||
|
||||
This directory contains my solution to the ['Multiples of 3 or 5' problem](https://projecteuler.net/problem=1).
|
@@ -2,9 +2,11 @@
|
||||
.section .text
|
||||
|
||||
func:
|
||||
init:
|
||||
MOV R0,#3 @Move 3 to R0
|
||||
MOV R1,#5 @Move 5 to R1
|
||||
MOV R2,#0 @Move 0 to R2
|
||||
MOV R3,#1000 @The value to compare with
|
||||
loop:
|
||||
add5:
|
||||
ADD R2,R2,R1 @Add R1 to R2
|
||||
@@ -14,21 +16,20 @@ add3:
|
||||
ADD R0,R0,#3 @Increment R0 by 3
|
||||
|
||||
check3:
|
||||
CMP R0,#1000 @Is R0 less than 1000?
|
||||
BLT check5 @If true, check 5
|
||||
CMP R0,R3 @Is R0 less than 1000?
|
||||
BGE rmv15 @If the multiple of 3 is greater than 1000, go to rmv15
|
||||
|
||||
check5:
|
||||
CMP R1,#1000 @Is R1 less than 1000?
|
||||
CMP R1,R3 @Is R1 less than 1000?
|
||||
BLT loop @If true, go back to loop
|
||||
BGE add3 @If the multiple of 5 reaches 1000, go to add3 - Continue adding multiples of 3, because those wouldn't have reached 1000 yet
|
||||
BGE add3 @If the multiple of 5 reaches 1000, go back to add3 instead of loop - Continue adding multiples of 3, because those wouldn't have reached 1000 yet
|
||||
|
||||
rmv15:
|
||||
MOV R0,#15 @The loop that follows is intended to subtract multiples of 15, which would have been added twice - 3, 6, 9, 12, 15, 18 ; 5, 10, 15, 20
|
||||
MOV R0,#15 @The loop that follows is intended to subtract multiples of 15, which would have been added twice - 3, 6, 9, 12, [15], 18 ; 5, 10, [15], 20
|
||||
loop2:
|
||||
SUB R2,R2,R0
|
||||
ADD R0,R0,#15
|
||||
CMP R0,#1000
|
||||
CMP R0,R3
|
||||
BLT loop2
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user