Bruno Alves
Bruno Alves

Reputation: 318

Assembly IA32: What's wrong with this piece of code?

Anyone could help me on this piece of code? In this case I want to use the value pointed by ptrgrades and then multiply by 4(array of integers) to increment the correspondent value in ptrfreq(array with absolute frequencies) to update that value. But I think that I'm having some problems to get the correct address value to use in %edi register or maybe %eax value.

    movl    ptrgrades, %esi         # set esi pointer to grades
    movl    grades_length, %ecx     # length of vector grades
l_freq:
    movl    ptrfreq, %edi           # reset with inital addrs of pointer
    movl    (%esi), %eax            # tmp = grades[i]
    shll    $2, %eax                 # multiply grade by 4
    addl    %eax, %edi              # and add to the ptrfreq addrs
    incl    (%edi)                  # freq[i]++
    addl    $4, (%esi)               # ptrgrade++
    loop    l_freq                  # loop while ecx != 0

Upvotes: 2

Views: 75

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

addl    $4, (%esi)               # ptrgrade++

To correct your program (and have the comment reflect the code), you want to increase the %esi register and not the memory where %esi points to. Write this:

addl    $4, %esi                 # ptrgrade++

shll    $2, %eax                 # multiply grade by 4
addl    %eax, %edi              # and add to the ptrfreq addrs
incl    (%edi)                  # freq[i]++

This trio of instructions just begs for an indexed addressing:

incl    (%edi, %eax, 4)         # freq[i]++

This way you can also move the movl ptrfreq, %edi instruction outside of the loop (before the l_freq: label) since %edi no longer changes.

Upvotes: 2

Related Questions