Concerning the following small code, which was illustrated in another post about the size of structure and all the possibilities to align data correctly :
struct
{
char Data1;
short Data2;
int Data3;
char Data4;
} x;
unsigned fun ( void )
{
x.Data1=1;
x.Data2=2;
x.Data3=3;
x.Data4=4;
return(sizeof(x));
}
I get the corresponding disassembly (with 64 bits)
0000000000000000 <fun>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: c6 05 00 00 00 00 01 movb $0x1,0x0(%rip) # b <fun+0xb>
b: 66 c7 05 00 00 00 00 movw $0x2,0x0(%rip) # 14 <fun+0x14>
12: 02 00
14: c7 05 00 00 00 00 03 movl $0x3,0x0(%rip) # 1e <fun+0x1e>
1b: 00 00 00
1e: c6 05 00 00 00 00 04 movb $0x4,0x0(%rip) # 25 <fun+0x25>
25: b8 0c 00 00 00 mov $0xc,%eax
2a: 5d pop %rbp
2b: c3 retq
I don't know how to calculate the terms located on the right which seems to be the address of local variables
used. Moreover, I don't know to calculate it with %rip register
Could you give an example which shows the link between %rip
and %rsp
or %rbp
, i.e especially in the computation of address when I use move
instructions.
rip
is the instruction pointer (hence the name). You can't address locals relative to it. Note thatx
is not a local. Also note that you used objdump on an intermediate object file hence you did not get the correct offsets. You might want to run it on a linked executable and/or use-r
option to see relocation entries.RIP
, notRPI
. ("instruction pointer" not "pointer instruction"). Also in 32b mode the 32b varianteip
is used, and in 16b mode the 16bip
part.rip
has no 8 bit aliases (likerax
hasal
).movb $0x4,0x0
will store byte value4
into memory at absolute address0
.movb $0x4,0x0(%rip)
will store byte value4
into memory at absolute addressrip + 0
, ie. at relative-to-RIP address0
. It's same as using other registers for addressing, likemovb $4,0(%edi)
. The difference is, that therip
points at the time of evaluation to the beginning of next instruction. So the usage ofrip
for relative addressing allows the compiler to produce "PIC" Position Independent Code. The OS then needs to load the data + code together to maintain their relative position to each other.