Compare commits

...

11 Commits

10 changed files with 123 additions and 21 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
mult3or5/main
evenFibonacci/main
*/main

5
README.md Normal file
View 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
View File

@@ -0,0 +1,3 @@
## Even Fibonacci Numbers
This directory contains my solution to the ['Even Fibonacci numbers' problem](https://projecteuler.net/problem=2).

8
evenFibonacci/main.c Normal file
View File

@@ -0,0 +1,8 @@
#include <stdio.h>
int evenFib();
void main() {
printf("%d\n",evenFib());
return;
}

35
evenFibonacci/main.s Normal file
View 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
View 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).

Binary file not shown.

View File

@@ -2,39 +2,34 @@
.section .text
func:
MOV R0,#3
MOV R1,#5
MOV R2,#0
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:
checkeq:
@ CMP R0,R1
@mult15:
@ ADDEQ R2,R2,R0
@ BEQ incr
else:
add5:
ADD R2,R2,R1
ADD R1,R1,#5
ADD R2,R2,R1 @Add R1 to R2
ADD R1,R1,#5 @Increment R1 by 5
add3:
ADD R2,R2,R0
ADD R0,R0,#3
ADD R2,R2,R0 @Add R0 to R2
ADD R0,R0,#3 @Increment R0 by 3
check3:
CMP R0,#1000
BLT check5
BGE rmv15
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
BLT loop
BGE add3
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 back to add3 instead of loop - Continue adding multiples of 3, because those wouldn't have reached 1000 yet
rmv15:
MOV R0,#15
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

View File

@@ -0,0 +1,7 @@
#include <stdio.h>
int func();
int main() {
printf("%d\n",func());
}

View File

@@ -0,0 +1,43 @@
.global func
.section .text
func: @ Find the difference between the sum of the squares and the square of the sum, for the first hundred numbers.
@ (a+b)^2 = a^2 + b^2 + 2ab
@ (a+b+c)^2 = a^2 + b^2 + c^2 + 2(ab +bc + ca)
@ Therefore, the difference must be 2(ab + ac + ad .... + bc + bd + be ... + cd + ce .....)
MOV R0,#0
MOV R2,#1 @ 'a' from the equation above
MOV R3,#2 @ 'b' from the equation above
mainloop:
MUL R1,R2,R3 @ 'a' * 'b' - keep in mind we need to do this for all possible 2-number permutations in the sequence
ADD R0,R0,R1 @Add the result to R0, which will be returned
incr_b:
ADD R3,R3,#1 @ Increments 'b'
chk_b:
CMP R3,#100 @ If 'b' is less than or equal to 100, we continue the multiplication
BLE mainloop
incr_a:
ADD R2,R2,#1 @ Increments 'a'
new_b: @This is a way for me to store R2+1 into R3
ADD R2,R2,#1
MOV R3,R2
SUB R2,R2,#1
chk_a:
CMP R2,#100
BLT mainloop
mul2:
LSL R0,R0,#1 @Bitwise left shit by 1 - Multiplies by 2
return:
BX LR
.section .data