机器汇编和反汇编&指针、结构的尺寸(对齐)

机器汇编和反汇编

调用编译器产生汇编语言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;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值