Search code examples
assemblyirvine32

operand must be relocatable Assembly x86 problem


In this code, I am trying to access the array[i-1][j+1] and in assembly, we can only handle 1D arrays so I am trying to access this index by subtracting the current index from my width "W" and multiply it by 4 because the type is DWORD but when I try this line "MOV EAX, [EDI - P]" it results in an error any hints?

        MOV EAX, W
        ADD EAX, 1
        MOV EBX, 4
        MUL EBX
        MOV P, EAX
        MOV EAX, [EDI- p];; up Right
        ADD ESI, EAX

Solution

  • Two problems with your attempt: You cannot subtract in an addressing mode, and you cannot use the contents of a memory location.

    However, you can add a constant and you can multiply by 2, 4, or 8, both of which can be used to advantage here.

            MOV EAX, W
            NEG EAX
            MOV EAX, 4[EDI+EAX*4];; up Right
            ADD ESI, EAX
    

    The constant can be negative, so when you want to access array[i-1][j-1], you can use

            MOV EAX, -4[EDI+EAX*4];; up left