iPodpadec
iPodpadec

Reputation: 21

Cant get loop working in ASSEMBLY x64 nasm

Can someone please explain what's up with this code? I thought I understood this, but apparently I don't.

    global main
    extern printf
    extern scanf

section .data

numberFormat:   db '%d', 10, 0

section .text

  main:

    push rbp        
    mov r8, 2       

  loop:

    add r8, 1
    mov rdi, numberFormat
    mov rsi, r8
    mov rax, 0
    call printf

    cmp r8, 15
    jl loop

    mov rax, 0      ; normal exit
    ret

my question is, why does this print out only number 3 instead of all numbers between 3 and 15

Upvotes: 0

Views: 2339

Answers (1)

iPodpadec
iPodpadec

Reputation: 21

Because r8 is a volatile register (caller saved) and it is being overwritten by printf. You could use RBX, RBP, R12–R15 which are preserved across a function call. – Michael Petch

Upvotes: 2

Related Questions