S
S
sos1mple2020-05-29 13:02:06
macOS
sos1mple, 2020-05-29 13:02:06

How to make simple loop with printf on nasm 64bit macos?

Good afternoon!
I want to display values ​​from 0 to 4. As far as I read correctly, ecx is an iterative register. But in my case, for some reason, the cycle turns into an endless one. How to fix it? Thanks in advance!

At the moment there is this code:

default rel

section .data
    fmt:    db "%d",10,0
    i: dq 0

extern	_printf

section .text
  global	start
    start:
        sub rbp, 8
        push rbp			; set up stack

        mov ecx, 5
        mov rax, 0
        mov [i], rax
        call loop1


    loop1:
        mov	rdi, fmt ; Загружаем шаблон вывода
        mov	rsi, [i]; Передаем параметр
        call _printf

        mov rax, [i]
        inc rax
        mov [i], rax
        loop loop1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2020-05-29
@sos1mple

On Mac OS x86-64, rcx is used as the fourth parameter and is not required to retain its value after the function is called. Store the value of the counter in memory or in one of the saved registers (rbx, rbp, r12-r15), but remember that you are also required to restore the values ​​of these registers when exiting the C code, if you changed them of course.
Since rcx is not saved, it is better to replace loop with a pair of instructions dec, jne.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question