aboutsummaryrefslogtreecommitdiffstats
path: root/01-Multiples-of-3-and-5/asm/euler1.asm
blob: 9541295a141136514ffc676a09516f6306d663e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
extern printf
SECTION .data
flag:   db  "%d",10,0           ; "%d\n\0"

SECTION .text
    global main    

main:
        push rbp                ; set up stack
        mov rbp, rsp
        xor rax, rax            ; start at multiple (i = 0)  
        mov r11, 0              ; sum = 0
        mov r12, 1000           ; max = 1000
.loop   xor rdx, rdx            ; clear rdx (remainder) 
        push rax                ; save rax
        mov rcx, 5              ; divisible by 5
        div rcx                 ; divide rdx:rax/rcx
        cmp rdx, 0              ; compare only remainder
        jz .sum                 ; if r = 0 , add to sum
        xor rdx,rdx             ; clear rdx
        mov rax, QWORD [rsp]    ; reload rax
        mov rcx, 3              ; divisible by 3
        div rcx                 ; divide rdx:rax/rcx
        cmp rdx, 0              ; compare only remainder
        jnz .next               ; if r = 0 , add to sum

.sum    add r11, QWORD [rsp]    ; add to sum register

.next   pop rax                 ; return rax value from stack to be inc
        inc rax                 ; i++
        cmp rax, r12            ; while rax < r12 (1000)
        jl .loop                ; repeat
        
        mov rdi, flag           ; arg 1 (format)
        mov rsi, r11            ; arg 2 (value)
        mov rax, 0		; no xmm registers
        call printf wrt ..plt
        pop rbp

        mov rax, 0
        ret