Search code examples
assemblyx86calling-convention

How can I print array in Assembly


I want to print result array, which has 3000 elements. I wrote this code:

.intel_syntax noprefix
.text
.globl main
main:
mov ecx, 3000
mov edx, offset result
llp:

mov al,[edx]
push eax
mov eax, offset message
push eax
call printf
add esp, 8
inc edx

loop llp
mov eax, 0
ret

.data
message : 
.asciz " Wynik: %i\n"

The problem is, that program prints only first element 3000 times. What should I change?

UPDATE

solved


Solution

  • ecx and edx are caller-saved registers, meaning they can be freely used in called functions such as the printf. You are lucky you even got 3000 items printed. One possible solution is to save and restore those registers using the stack around the call printf:

    llp:
    
    mov al,[edx]
    push ecx
    push edx
    push eax
    mov eax, offset message
    push eax
    call printf
    add esp, 8
    pop edx
    pop ecx
    inc edx
    
    loop llp