When using Compiler Explorer, I've noticed that the char
is always offset by 1
from the previous position. For example:
int main() {
char a = 1;
char b = 2;
char c = 3;
char d = 4;
char e = 5;
return a + b + c + d + e;
}
main:
...
movb $1, -1(%rbp)
movb $2, -2(%rbp)
movb $3, -3(%rbp)
movb $4, -4(%rbp)
movb $5, -5(%rbp)
However, in the following example for a short
, why is it offset by 4 and not 2?
int main() {
char b = 2;
short c = 2;
return b;
}
main:
...
movb $2, -1(%rbp)
movw $2, -4(%rbp) <-- why isn't this offset by 2?
I think the answer because since it is two bytes, it needs to fit at memory addresses 3 & 4, so if it were at 2, it would not have enough space with 1 already being occupied, but I'm not too certain. What are the 'rules' for how bytes are padded/at what offset they are added?