Skip to content

Instantly share code, notes, and snippets.

@ga2arch
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save ga2arch/a78aac03cf3d1ad90874 to your computer and use it in GitHub Desktop.

Select an option

Save ga2arch/a78aac03cf3d1ad90874 to your computer and use it in GitHub Desktop.
Euler Problem 1 in assembly
;; nasm -f macho64 euler1.s --prefix _
;; ld -e _start -lSystem -macosx_version_min 10.7.0 -o hello euler1.o -lc
;; If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
;; The sum of these multiples is 23.
;; Find the sum of all the multiples of 3 or 5 below 1000.
use64
extern printf
global start
section .text
print:
mov rdi, type
mov rsi, [rbx - 0x4]
mov rax, 0
call printf
ret
start:
push rbx
mov rbx, rsp
sub rsp, 16
mov qword [rbx - 0x4], 0
mov rcx, 999
label:
push rcx
xor rdx, rdx
mov rax, rcx
mov r8, 3
div r8
cmp rdx, 0
je true
jmp Try_Div5
Try_Div5:
xor rdx, rdx
mov rax, rcx
mov r8, 5
div r8
cmp rdx, 0
je true
jmp false
true:
add qword [rbx - 0x4], rcx
false:
pop rcx
loop label
finish:
call print
mov rsp, rbx
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
section .data
type: db "%d", $ - str, 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment