Assembly code that prints all elements of an array

I am trying to print the elements of an array. I have this code that sums all the elements of an array and prints the result and I tried to edit in many ways with no luck.

Here is the code I found online:

    section .text
    global _start   ;must be declared for linker (ld)
_start: 

      mov  eax,3      ;number bytes to be summed 
      mov  ebx,0      ;EBX will store the sum
      mov  ecx, x     ;ECX will point to the current element to be summed
top:  add  ebx, [ecx]
      add  ecx,1      ;move pointer to next element
      dec  eax        ;decrement counter
      jnz  top        ;if counter not 0, then loop again
done: 
      add   ebx, '0'
      mov  [sum], ebx ;done, store result in "sum"
display:
      mov  edx,1      ;message length
      mov  ecx, sum   ;message to write
      mov  ebx, 1     ;file descriptor (stdout)
      mov  eax, 4     ;system call number (sys_write)
      int  0x80       ;call kernel
      mov  eax, 1     ;system call number (sys_exit)
      int  0x80       ;call kernel

section .data
global x
x:    
      db  2
      db  4
      db  3
sum: 
      db  0

      

And this is how I edited it with no luck (I tried many other things). This prints the first element "2" and gives a segmentation fault.

EDIT: This is a working code, thanks everyone, thanks ElderBug! :)

_start: 

      mov  esi,3      ;number bytes to be traversed 
      mov  edi, x     ;EDI will point to the current element to be summed
      mov  eax, 0     ;eax will hold the text to print

top:  

      mov eax, [edi] ;store current element
      add  edi,1      ;move pointer to next element

      add  eax, '0' ;do the conversion
      mov [toprint], eax


      mov  ecx, toprint ;message to write
      mov  edx,1      ;message length
      mov  ebx, 1     ;file descriptor (stdout)
      mov  eax, 4     ;system call number (sys_write)
      int  0x80       ;call kernel



      dec  esi        ;decrement counter
      jnz  top        ;if counter not 0, then loop again

      mov  eax, 1     ;system call number (sys_exit)
      int  0x80       ;call kernel



   section  .data
    global x
    x:    
          db  2
          db  4
          db  3

   toprint:

         dw 'x'

      

I know that "int 0x80" style 4 means that the record and first argument are stored in ebx, the second in ecx, the third in edx. But I can't figure it out.

+3


source to share


1 answer


The solution you say works for you still has major problems!

  • Since printprint was defined as word , you should not write the dword value in it with an instruction mov [toprint], eax

    .
  • Since array elements are defined as bytes , you shouldn't read them as dword with an instruction mov eax, [edi] ;store current element

    .
  • sys_exit uses EBX as the second parameter.

This is how it should look like



top:  
 mov al, [edi]     ;store current element
 add edi, 1        ;move pointer to next element
 add al, '0'       ;do the conversion
 mov [toprint], al

      

Please read the comment given by @David C. Rankin. This is very helpful for your purpose.

+2


source







All Articles