RiscV汇编介绍(1)-编译过程

从c/c++源文件,到可以执行文件,需要以下几个步骤:

  • 预处理/编译
  • 汇编
  • 链接


image


下面我们以hello world程序为例,展示整个编译链接过程。

1. 编写hello.c代码

#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}


2.使用gcc –E hello.c –o hello.i, 将源文件hello.c文件预处理生成hello.i

3.编译, gcc –S hello.i –o hello.s, 生成汇编程序hello.s,对于x86系统,生成x86汇编代码。

ction	.rodata
.LC0:
	.string	"Hello World!"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	.LC0(%rip), %rdi
	call	puts@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
	.section	.note.GNU-stack,"",@progbits

4.汇编 gcc –c hello.s –o hello.o, 生成目标机器码。

5.链接,和系统库文件进行链接,ld hello.o –o hello, 执行会出错,只靠一个hello.o不能生成一个完整的可执行文件。

gcc hello.c –o hello 可以直接生成可执行文件。















posted on 2019-08-12 20:29  迈克老狼2012  阅读(2947)  评论(0编辑  收藏  举报

导航