please help me, i need to print my array in nasm. this is what i have so far,

section .bss

section .data

    array dd 1,2,3,4,5

    arraylen equ $ - array

section .text

    global _start:

_start:

    mov edx, arraylen
    mov ecx, array
    mov ebx, 1
    mov eax, 4
    int 80h

exit: 
    nop
    mov eax, 1
    mov ebx, 0
    int 80H

the output i receive are characters that are unrecognizeable.

First thing, arraylen does not contain what you think. It contains 20: 5 dword sized elements. To get the number of elements divide by 4

arraylen equ ($ - array) / 4

Next, you cannot print a number, you first need to convert it to ASCII. For your elements it is simple (since they are single digits), add 48 to each element (for numbers > 9 it is a bit different).

Here is some code to get you going:

%include '../../include/syscalls32.inc'

section .bss

section .data

    array dd 1,2,3,4,5

    arraylen equ ($ - array) / 4            ; array length * 4 = number of elements

section .text

    global _start:

_start:

    mov     esi, array                      ; get pointer to array
    mov     edi, arraylen - 1               ; edi = number of array elements

.PrintArray:
    mov     edx, 1                          ; print 1 byte                 
    mov     ecx, [esi]                      ; get current array element
    add     ecx, 48                         ; add 48 to convet to ASCII
    push    ecx                             ; push to stack since we need an address of item to print
    mov     ecx, esp                        ; mov address of char to ecx
    mov     ebx, stdout
    mov     eax, sys_write
    int     80h                             ; now print it
    pop     ecx                             ; balance stack
    add     esi, 4                          ; get next element, 4 because it's an array of dwords
    dec     edi                             ; decrease loop counter
    jns     .PrintArray                     ; if edi ! -1 continue loop

.PrintLineFeed:
    sub     esp, 4
    mov     byte [esp], 10
    mov     edx, 1
    mov     ecx, esp
    mov     ebx, stdout
    mov     eax, sys_write
    int     80h
    add     esp, 4                          ; not needed since next call is exit, but learn good techniques.
exit: 
    mov     ebx, 0
    mov     eax, sys_exit
    int 80H

I created syscalls32.inc with a little grep magic:
grep __NR /usr/include/asm/unistd_32.h | grep define | sed -e 's/\#/\%/' -e 's/__NR_/sys_/' > syscalls32.inc

acfec3f40ee57d5cee57853112823193

thank you very much! i have a complete understanding of what is going now. is there any chance you know how to reverse the array? I have tried with this algorithm but it does not generate the results i am looking for and i am a bit stumped. I have tried swapping the elements in the array, unless you know there is a less troublesum alternative example: 1,2,3,4,5 becomes 5,4,3,2,1.

(sorry the daniweb insert code function is not working)

mov esi, array

mov edi, array
add edi, 16

reverseloop:

nop
mov eax, edi
mov ebx, esi

;swap elements

mov dword [edi], eax
mov dword [esi], ebx
add esi, 4
sub edi, 4
cmp esi, edi
jb reverseloop
ret

This article has been dead for over six months. Start a new discussion instead.