Skip to content

Commit

Permalink
Merge pull request #17 from tijko/Assembly
Browse files Browse the repository at this point in the history
Assembly Solution #3
  • Loading branch information
tijko authored Nov 25, 2023
2 parents 43f183b + fe97869 commit c58bb04
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
64 changes: 64 additions & 0 deletions ASM/asm_solutions_1-10/Euler_3.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.section .data
answer: .asciz "Answer: %d\n"
anslen: .word 12

.section .text
.globl main

# Layout:
# r8 -> BigInt
# r9 -> Start of Canidates
# r10 -> Half of Current-Canidate
# r11 -> Range (2..r10/2)
main:
pushq %rbp
movq %rsp, %rbp
xor %rdx, %rdx
jmp setup

setup:
xor %r8, %r8
movq $600851475143, %r8
cvtsi2sdq %r8, %xmm0
sqrtsd %xmm0, %xmm0
cvttsd2si %xmm0, %r9
jmp next

next:
dec %r9
movq %r9, %rax
cvtsi2sdq %r9, %xmm1
sqrtsd %xmm1, %xmm1
cvttsd2si %xmm1, %r10
movq $2, %r11
movq %r9, %rax
jmp factor

factor:
xor %rdx, %rdx
div %r11
test %rdx, %rdx
jz next
cmp %r11, %r10
je prime
inc %r11
movq %r9, %rax
xor %rdx, %rdx
jmp factor

prime:
movq %r8, %rax
xor %rdx, %rdx
div %r9
test %rdx, %rdx
jz exit
jmp next

exit:
leaq answer(%rip), %rdi
movq %r9, %rsi
callq printf

movq $60, %rax
movq $1, %rdi
syscall
6 changes: 3 additions & 3 deletions ASM/asm_solutions_1-10/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ LFLAGS := -static -lc
CFLAGS := -c -g -fverbose-asm

default:
$(CC) $(CFLAGS) Euler_2.S
$(CC) $(CFLAGS) Euler_3.S

link:
$(CC) $(LFLAGS) Euler_2.o -o Euler_2
$(CC) $(LFLAGS) Euler_3.o -o Euler_3

clean:
rm Euler_2.o Euler_2
rm Euler_3.o Euler_3

0 comments on commit c58bb04

Please sign in to comment.