C语言转为汇编

vscode实现。

1、编写简单C语言程序hello.c:

#include <stdio.h>

int main(){
    int i=0;
    printf("%d\n",i);
    return 0;
}

2、生成汇编文件:

终端输入gcc -S hello.c 就会生成hello.s

可添加asm插件,使汇编代码有颜色便于阅读。

	.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 10, 15	sdk_version 10, 15, 6
	.globl	_main                   ## -- Begin function main
	.p2align	4, 0x90
_main:                                  ## @main
	.cfi_startproc
## %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register %rbp
	subq	$16, %rsp
	movl	$0, -4(%rbp)
	movl	$0, -8(%rbp)
	movl	-8(%rbp), %esi
	leaq	L_.str(%rip), %rdi
	movb	$0, %al
	callq	_printf
	xorl	%ecx, %ecx
	movl	%eax, -12(%rbp)         ## 4-byte Spill
	movl	%ecx, %eax
	addq	$16, %rsp
	popq	%rbp
	retq
	.cfi_endproc
                                        ## -- End function
	.section	__TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
	.asciz	"%d\n"

.subsections_via_symbols

3、生成二进制目标文件hello.o

终端输入gcc -c hello.c

4、生成二进制可执行文件prog

终端输入gcc -o prog hello.c sum.c

5、反汇编

终端输入objdump -d hello.o

hello.o:        file format Mach-O 64-bit x86-64


Disassembly of section __TEXT,__text:

0000000000000000 _main:
       0: 55                            pushq   %rbp
       1: 48 89 e5                      movq    %rsp, %rbp
       4: 48 83 ec 10                   subq    $16, %rsp
       8: c7 45 fc 00 00 00 00          movl    $0, -4(%rbp)
       f: c7 45 f8 00 00 00 00          movl    $0, -8(%rbp)
      16: 8b 75 f8                      movl    -8(%rbp), %esi
      19: 48 8d 3d 14 00 00 00          leaq    20(%rip), %rdi
      20: b0 00                         movb    $0, %al
      22: e8 00 00 00 00                callq   0 <_main+0x27>
      27: 31 c9                         xorl    %ecx, %ecx
      29: 89 45 f4                      movl    %eax, -12(%rbp)
      2c: 89 c8                         movl    %ecx, %eax
      2e: 48 83 c4 10                   addq    $16, %rsp
      32: 5d                            popq    %rbp
      33: c3                            retq

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值