Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly code that prints all elements of an array

Tags:

x86

assembly

I've been trying print elements of an array. I've got this code that sums all elements of an array and prints the result and I tried to edit in a lot of ways with no luck.

Here is the code I found on internet:

    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 here is how I edited it with no luck (I tried lots of other things). This prints the first element '2' and gives Segmentation fault error.

EDIT: This is the 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 "int 0x80" style 4 means write and first argument is stored in ebx, second in ecx, third in edx. But can't figure this out.

like image 373
cbt Avatar asked May 02 '26 15:05

cbt


1 Answers

The solution that you say is working for you still has severe issues!

  • Because toprint was defined as a word you should not write a dword value in it with the instruction mov [toprint], eax.
  • Because the array elements are defined as byte you should not read them as dword with the instruction mov eax, [edi] ;store current element.
  • sys_exit uses EBX as a 2nd 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 study the commentary given by @David C. Rankin. It's very useful for your purpose.

like image 51
Sep Roland Avatar answered May 05 '26 08:05

Sep Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!