Hi!
    I really want to how does gcc compile code like *(ptr base +
offset), where ptr base is the initial address of a pointer variable
and offset is any legal integer expression. There is a example here:

    int i = 1;
    int j = 1;
    int *buf = (int*)malloc(10 *sizeof(int));
    *(buf + i + j) = 7;

    And the correspondent assembly code is :
    ......
        int i = 1;
 80483b5:       c7 45 f0 01 00 00 00    movl   $0x1,-0x10(%ebp)
        int j = 1;
 80483bc:       c7 45 f4 01 00 00 00    movl   $0x1,-0xc(%ebp)
        int *buf = (int*)malloc(10 * sizeof(int));
 80483c3:       c7 04 24 28 00 00 00    movl   $0x28,(%esp)
 80483ca:       e8 09 ff ff ff          call   80482d8 <mal...@plt>
 80483cf:       89 45 f8                mov    %eax,-0x8(%ebp)

        *(buf + i + j) = 7;
 80483d2:       8b 55 f0                mov    -0x10(%ebp),%edx
 80483d5:       8b 45 f4                mov    -0xc(%ebp),%eax
 80483d8:       8d 04 02                lea    (%edx,%eax,1),%eax
 80483db:       c1 e0 02                shl    $0x2,%eax
 80483de:       03 45 f8                add    -0x8(%ebp),%eax
 80483e1:       c7 00 07 00 00 00       movl   $0x7,(%eax)
   ......
So I guess that gcc would always compute offset "i+j" first, and then
add the result of "i + j" to the base address of buf to obtain the
final address. Do I guess right? Is there any exception?
ps: My gcc version is 4.3.3.
 Thank you!

Reply via email to