机器汇编和反汇编
调用编译器产生汇编语言mstore.s:
gcc -Og -S mstore.c
调用汇编器产生二进制文件mstore.o:
gcc -Og -c mstore.c
反汇编:
objdump -d mstore.o
编译生成二进制可执行文件prog:
gcc -Og -o prog main.c mstore.c
反汇编:
objdump -d prog
mstore.c
long mult2(long,long);
void multstore(long x,long y,long *dest){
long t=mult2(x,y);
*dest=t;
}
mstore.s
.file "mstore.c"
.text
.globl multstore
.type multstore, @function
multstore:
.LFB0:
.cfi_startproc
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
movq %rdx, %rbx
call mult2@PLT
movq %rax, (%rbx)
popq %rbx
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE0:
.size multstore, .-multstore
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
二进制文件mstore.o不可查看
反汇编mstore.o:
0000000000000000 <multstore>:
0: 53 push %rbx
1: 48 89 d3 mov %rdx,%rbx
4: e8 00 00 00 00 callq 9 <multstore+0x9>
9: 48 89 03 mov %rax,(%rbx)
c: 5b pop %rbx
d: c3 retq
要生成可执行代码需要一组目标代码文件.o运行链接器,而这组目标代码文件中必须包含一个main函数
main函数:
#include<stdio.h>
void multstore(long x,long y,long *dest);
int main(){
long d;
multstore(2,3,&d);
printf("2 * 3 --> %ld\n",d);
return 0;
}
long mult2(long a,long b){
long s= a*b;
return s;
}
指针、结构的尺寸(对齐)
#include<stdio.h>
struct A{
char c;
int b;
char a;
};
struct B{
char c;
char a;
int b;
};
struct C{
int b;
char a;
char c;
};
int main(){
int (*A1)[3];
int *A2[3];
printf("%d %d %d \n",sizeof(A1),sizeof(*A1),sizeof(**A1)); //8 12 4
printf("%d %d %d \n",sizeof(A2),sizeof(*A2),sizeof(**A2)); //24 8 4
printf("%d %d %d \n",sizeof(A),sizeof(B),sizeof(C)); //12 8 8
return 0;
}