I'm learning convert C to assembly, then I found char and short data are stored in 4 byte registers.
note: I use -Og -g to compiler C, and use gdb disas main! In addition, my computer is 64bit.
Below is code about char and correspond to assembly(I think short and char are same problem, so I put one of two code):
#include <stdio.h>
int main(void) {
const int LEN = 3;
char c[LEN];
c[0] = 1;
c[1] = 2;
c[2] = 3;
for(int i = 0; i < LEN; i ++) {
printf("%d\n", c[i]);
}
return 0;
}
a part of disassembler code!
0x000000000000116e <+5>: sub $0x10,%rsp
0x0000000000001172 <+9>: mov %fs:0x28,%rax
0x000000000000117b <+18>: mov %rax,0x8(%rsp)
0x0000000000001180 <+23>: xor %eax,%eax
0x0000000000001182 <+25>: movb $0x1,0x5(%rsp)
0x0000000000001187 <+30>: movb $0x2,0x6(%rsp)
0x000000000000118c <+35>: movb $0x3,0x7(%rsp)
0x0000000000001191 <+40>: mov $0x0,%ebx
0x0000000000001196 <+45>: jmp 0x11b9 <main+80>
0x0000000000001198 <+47>: movslq %ebx,%rax
# why %edx?
0x000000000000119b <+50>: movsbl 0x5(%rsp,%rax,1),%edx
0x00000000000011a0 <+55>: lea 0xe5d(%rip),%rsi # 0x2004
0x00000000000011a7 <+62>: mov $0x1,%edi
0x00000000000011ac <+67>: mov $0x0,%eax
0x00000000000011b1 <+72>: callq 0x1070 <__printf_chk@plt>
0x00000000000011b6 <+77>: add $0x1,%ebx
0x00000000000011b9 <+80>: cmp $0x2,%ebx
0x00000000000011bc <+83>: jle 0x1198 <main+47>
I have learnt a little about java data types, hmm, like byte, char, or short is promoted to int. I'm not sure they are something related.