Search code examples
arraysloopsassemblyx86att

Update array in assembly


Just starting to learn assembly, so I am having trouble with some basic. For example, I am trying to modify a file that normally runs to find the maximum value.

What I am trying to do is iterate through the data_items and modify it by changing the list item, while still maintaining its ability to execute to find the maximum value.

I have tried modifying the start_loop to include an addl data_items, 0x01 but it errors out since it only pulls the first value.

I then adjusted it to do something like addl data_items(,%esi,4), 0x01 and iterate through adjusting it but that didn't work either. I know this is probably very simplistic, but I can't seem to find a method that works.

 #
.section .data
data_items:          #These are the data items trying to modify and store
 .long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

.section .text
.globl _start
_start:
    movl $0, %edi             
    movl data_items(,%edi,4), %eax 
    movl %eax, %ebx
start_loop:
    cmpl $0, %eax
    je loop_exit
    incl %edi
    movl data_items(,%edi,4), %eax
    cmpl %ebx, %eax
    jle start_loop
    movl %eax, %ebx
    jmp start_loop            
loop_exit:
    movl $1, %eax            
    int  $0x80

Solution

  • Once you have an array element in %eax just increment the memory that was last used to read %eax from:

    start_loop:
        cmpl $0, %eax     ; Not actual element if this is 0
        je loop_exit
        incl data_items(,%edi,4)    <<<<<<<<<<<<<<<<<
        incl %edi
        movl data_items(,%edi,4), %eax
    

    This does not mess with the current finding of the max value. Only next time will the max value be 1 greater.