This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How does gcc compile source code just like *(ptr base + offset) ?


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 <malloc@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!

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]