0

I'm learning Assembly with linux x86_64 and i compiles my codes with nasm and ld. This time i want to print secuentially a list of numbers set into an array named 'Llista'. Once compiled, linked and executed with ./printLlista the terminal shows me this:

The list is: 1�Violació de segment (s'ha bolcat la memòria)

Mi intention is to do something like this in C:

The program printLlista.asm is this:

; printLlista.asm 
; Print a list of values from an array.
; Compiled : nasm -f elf printLlista.asm
; Linked : ld -m elf_i386 printLlista.o -o printLlista
; Execute : ./printLlista

section      .data 
    Llista db 1, 2, -3, 4
    len_Llista equ $ - Llista
    res_Llista db 0
    Missatge db "The list is: ", 0h
    len_Missatge equ $ - Missatge

section      .text 
global  _start

    _start:
    print_Missatge: 
 mov     edx, len_Missatge
 mov     ecx, Missatge
 mov     ebx, 1
 mov     eax, 4
 int     80h

   get_first_item: 
 mov     esi, 0
 mov     ebx, 0
 mov     ecx, Llista

    search_Llista:
 mov     ebx, [ecx]
 add     ebx, '0'
 mov     [res_Llista], ebx

    print_items:
 mov     edx, len_Llista
 mov     ecx, res_Llista
 mov     ebx, 1
 mov     eax, 4
 int     80h

   test_if_loop: 
 mov     ecx, 1
 inc     esi
 cmp     esi, len_Llista
 jl      search_Llista

    exit:
 mov     ebx, 0
 mov     eax, 1
 int     80h 

I don't understand where is the problem, I hope you can. Thanks in advance.

Ferran
  • 21
  • 3
  • 2
    `mov ecx, 1` you wanted `add ecx, 1`. Note you are processing 4 bytes instead of 1. Use 8 bit registers. Also learn to use a debugger. – Jester Aug 15 '20 at 11:39
  • Also your `mov ecx, res_Llista` of course overwrites `ecx` so not even `add` will work. It's unclear what your `print_items` block is trying to do inside that loop. – Jester Aug 15 '20 at 11:45
  • `search_Llista` should have `movsx ebx, byte [Llista + esi]`. And you need to implement signed decimal formatting. Just adding '0' only works for single-digit positive numbers. The `res_Llista` buffer should be long enough to hold a negative 3-digit number and you need to keep track of how many bytes are used. – ecm Aug 15 '20 at 14:45
  • Quick search brings up https://stackoverflow.com/questions/13166064/how-do-i-print-an-integer-in-assembly-level-programming-without-printf-from-the/13166463#13166463 – ecm Aug 15 '20 at 14:52

0 Answers0