Search code examples
assemblyx86offsetgnugnu-assembler

Refer to memory location with an offset in GNU Assembly


Sorry in advance for the novice question, but strangely enough I couldn't find a proper answer to this question. Simply put, let's say I want to movl from a certain variable to the %ecx register. This variable is a buffer full of input. How do I reference the buffer AT a specific offset, ie buffer + 1 or buffer + n? I need it for basic string iteration.

All help is appreciated, even if it's just helping me find a previous post that answers it but that I couldn't find.


Solution

  • In AT&T syntax (used by GAS), the following is used for an indirect offset reference:

     movl     8(%ebp), %eax   ; Move from EBP+8 into EAX
    

    In Intel syntax (Which is actually also understood and accepted by GAS) it would be:

     MOV      EAX, [EBP+8]
    

    Another alternative (which you seem to be interested in) is Base Plus Index addressing:

     MOV      EAX, [EBX + EDI]
    

    This moves into EAX (using Intel format) the content of the memory location that is the sum of EBX and EDI. In AT&T format this would be:

     movl     (%ebx,%edi), %eax
    

    Honestly, I rarely use this form. You may have to include a scale factor. You can use one to meet your needs:

     movl     (%ebx,%edi,1),%eax   ; Where 1 is a scaling factor (ebx+edi*scale)