从零开始写操作系统00:helloworld是如何运行的

本专栏是在学习LMOS的《操作系统实战45讲》时的学习笔记,以检验学习效果。

环境准备

用vmware安装一个ubuntu操作系统,参考如下文章:
https://zhuanlan.zhihu.com/p/141033713
注意,vmware装14的版本就好,16的版本会出现很多问题(已踩坑)

hello world 的编译过程

在这里插入图片描述
源文件生成预处理文件: gcc -E HelloWorld.c -o HelloWorld.i
预处理文件生成编译文件: gcc -S HelloWorld.i -o HelloWorld.s
编译文件生成汇编文件: gcc -c HelloWorld.s -o HelloWorld.o
汇编文件生成可执行文件:gcc HelloWorld.o -o HelloWorld
源文件生成可执行文件:gcc HelloWorld.c -o HelloWorld
Linux系统运行可执行文件:./HelloWorld

-c表示只编译,不链接
-o表示指定生成目标文件名,如果不写就生成默认的名字。
-E只做预处理
-S只编译,生成汇编代码

HelloWorld.c文件内容:

#include "stdio.h"
int main(int argc, char const *argv[])
{
    printf("hello world!\n");
    return 0;
}

HelloWorld.s文件内容:

  1         .file   "HelloWorld.c"
  2         .text
  3         .section        .rodata
  4 .LC0:
  5         .string "hello world!"
  6         .text
  7         .globl  main
  8         .type   main, @function
  9 main:
 10 .LFB0:
 11         .cfi_startproc
 12         endbr64
 13         pushq   %rbp
 14         .cfi_def_cfa_offset 16
 15         .cfi_offset 6, -16
 16         movq    %rsp, %rbp
 17         .cfi_def_cfa_register 6
 18         subq    $16, %rsp
 19         movl    %edi, -4(%rbp)
 20         movq    %rsi, -16(%rbp)
 21         leaq    .LC0(%rip), %rdi
 22         call    puts@PLT
 23         movl    $0, %eax
 24         leave
 25         .cfi_def_cfa 7, 8
 26         ret
 27         .cfi_endproc
 28 .LFE0:
 29         .size   main, .-main
 30         .ident  "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
 31         .section        .note.GNU-stack,"",@progbits
 32         .section        .note.gnu.property,"a"
 33         .align 8
 34         .long    1f - 0f
 35         .long    4f - 1f
 36         .long    5
 37 0:
 38         .string  "GNU"
 39 1:
 40         .align 8
 41         .long    0xc0000002
 42         .long    3f - 2f
 43 2:
 44         .long    0x3
 45 3:
 46         .align 8
 47 4:

用objdump反汇编.out文件:

doalter@doalterUbuntu:~/Documents/code/cosmos/lesson01/HelloWorld$ objdump -d HelloWorld

HelloWorld:     file format elf64-x86-64


Disassembly of section .init:

00000000000004e8 <_init>:
 4e8:	48 83 ec 08          	sub    $0x8,%rsp
 4ec:	48 8b 05 f5 0a 20 00 	mov    0x200af5(%rip),%rax        # 200fe8 <__gmon_start__>
 4f3:	48 85 c0             	test   %rax,%rax
 4f6:	74 02                	je     4fa <_init+0x12>
 4f8:	ff d0                	callq  *%rax
 4fa:	48 83 c4 08          	add    $0x8,%rsp
 4fe:	c3                   	retq   

Disassembly of section .plt:

0000000000000500 <.plt>:
 500:	ff 35 02 0b 20 00    	pushq  0x200b02(%rip)        # 201008 <_GLOBAL_OFFSET_TABLE_+0x8>
 506:	ff 25 04 0b 20 00    	jmpq   *0x200b04(%rip)        # 201010 <_GLOBAL_OFFSET_TABLE_+0x10>
 50c:	0f 1f 40 00          	nopl   0x0(%rax)

0000000000000510 <puts@plt>:
 510:	ff 25 02 0b 20 00    	jmpq   *0x200b02(%rip)        # 201018 <puts@GLIBC_2.2.5>
 516:	68 00 00 00 00       	pushq  $0x0
 51b:	e9 e0 ff ff ff       	jmpq   500 <.plt>

Disassembly of section .plt.got:

0000000000000520 <__cxa_finalize@plt>:
 520:	ff 25 d2 0a 20 00    	jmpq   *0x200ad2(%rip)        # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
 526:	66 90                	xchg   %ax,%ax

Disassembly of section .text:

0000000000000530 <_start>:
 530:	31 ed                	xor    %ebp,%ebp
 532:	49 89 d1             	mov    %rdx,%r9
 535:	5e                   	pop    %rsi
 536:	48 89 e2             	mov    %rsp,%rdx
 539:	48 83 e4 f0          	and    $0xfffffffffffffff0,%rsp
 53d:	50                   	push   %rax
 53e:	54                   	push   %rsp
 53f:	4c 8d 05 8a 01 00 00 	lea    0x18a(%rip),%r8        # 6d0 <__libc_csu_fini>
 546:	48 8d 0d 13 01 00 00 	lea    0x113(%rip),%rcx        # 660 <__libc_csu_init>
 54d:	48 8d 3d e6 00 00 00 	lea    0xe6(%rip),%rdi        # 63a <main>
 554:	ff 15 86 0a 20 00    	callq  *0x200a86(%rip)        # 200fe0 <__libc_start_main@GLIBC_2.2.5>
 55a:	f4                   	hlt    
 55b:	0f 1f 44 00 00       	nopl   0x0(%rax,%rax,1)

0000000000000560 <deregister_tm_clones>:
 560:	48 8d 3d c9 0a 20 00 	lea    0x200ac9(%rip),%rdi        # 201030 <__TMC_END__>
 567:	55                   	push   %rbp
 568:	48 8d 05 c1 0a 20 00 	lea    0x200ac1(%rip),%rax        # 201030 <__TMC_END__>
 56f:	48 39 f8             	cmp    %rdi,%rax
 572:	48 89 e5             	mov    %rsp,%rbp
 575:	74 19                	je     590 <deregister_tm_clones+0x30>
 577:	48 8b 05 5a 0a 20 00 	mov    0x200a5a(%rip),%rax        # 200fd8 <_ITM_deregisterTMCloneTable>
 57e:	48 85 c0             	test   %rax,%rax
 581:	74 0d                	je     590 <deregister_tm_clones+0x30>
 583:	5d                   	pop    %rbp
 584:	ff e0                	jmpq   *%rax
 586:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
 58d:	00 00 00 
 590:	5d                   	pop    %rbp
 591:	c3                   	retq   
 592:	0f 1f 40 00          	nopl   0x0(%rax)
 596:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
 59d:	00 00 00 

00000000000005a0 <register_tm_clones>:
 5a0:	48 8d 3d 89 0a 20 00 	lea    0x200a89(%rip),%rdi        # 201030 <__TMC_END__>
 5a7:	48 8d 35 82 0a 20 00 	lea    0x200a82(%rip),%rsi        # 201030 <__TMC_END__>
 5ae:	55                   	push   %rbp
 5af:	48 29 fe             	sub    %rdi,%rsi
 5b2:	48 89 e5             	mov    %rsp,%rbp
 5b5:	48 c1 fe 03          	sar    $0x3,%rsi
 5b9:	48 89 f0             	mov    %rsi,%rax
 5bc:	48 c1 e8 3f          	shr    $0x3f,%rax
 5c0:	48 01 c6             	add    %rax,%rsi
 5c3:	48 d1 fe             	sar    %rsi
 5c6:	74 18                	je     5e0 <register_tm_clones+0x40>
 5c8:	48 8b 05 21 0a 20 00 	mov    0x200a21(%rip),%rax        # 200ff0 <_ITM_registerTMCloneTable>
 5cf:	48 85 c0             	test   %rax,%rax
 5d2:	74 0c                	je     5e0 <register_tm_clones+0x40>
 5d4:	5d                   	pop    %rbp
 5d5:	ff e0                	jmpq   *%rax
 5d7:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
 5de:	00 00 
 5e0:	5d                   	pop    %rbp
 5e1:	c3                   	retq   
 5e2:	0f 1f 40 00          	nopl   0x0(%rax)
 5e6:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
 5ed:	00 00 00 

00000000000005f0 <__do_global_dtors_aux>:
 5f0:	80 3d 39 0a 20 00 00 	cmpb   $0x0,0x200a39(%rip)        # 201030 <__TMC_END__>
 5f7:	75 2f                	jne    628 <__do_global_dtors_aux+0x38>
 5f9:	48 83 3d f7 09 20 00 	cmpq   $0x0,0x2009f7(%rip)        # 200ff8 <__cxa_finalize@GLIBC_2.2.5>
 600:	00 
 601:	55                   	push   %rbp
 602:	48 89 e5             	mov    %rsp,%rbp
 605:	74 0c                	je     613 <__do_global_dtors_aux+0x23>
 607:	48 8b 3d 1a 0a 20 00 	mov    0x200a1a(%rip),%rdi        # 201028 <__dso_handle>
 60e:	e8 0d ff ff ff       	callq  520 <__cxa_finalize@plt>
 613:	e8 48 ff ff ff       	callq  560 <deregister_tm_clones>
 618:	c6 05 11 0a 20 00 01 	movb   $0x1,0x200a11(%rip)        # 201030 <__TMC_END__>
 61f:	5d                   	pop    %rbp
 620:	c3                   	retq   
 621:	0f 1f 80 00 00 00 00 	nopl   0x0(%rax)
 628:	f3 c3                	repz retq 
 62a:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)

0000000000000630 <frame_dummy>:
 630:	55                   	push   %rbp
 631:	48 89 e5             	mov    %rsp,%rbp
 634:	5d                   	pop    %rbp
 635:	e9 66 ff ff ff       	jmpq   5a0 <register_tm_clones>

000000000000063a <main>:
 63a:	55                   	push   %rbp
 63b:	48 89 e5             	mov    %rsp,%rbp
 63e:	48 83 ec 10          	sub    $0x10,%rsp
 642:	89 7d fc             	mov    %edi,-0x4(%rbp)
 645:	48 89 75 f0          	mov    %rsi,-0x10(%rbp)
 649:	48 8d 3d 94 00 00 00 	lea    0x94(%rip),%rdi        # 6e4 <_IO_stdin_used+0x4>
 650:	e8 bb fe ff ff       	callq  510 <puts@plt>
 655:	b8 00 00 00 00       	mov    $0x0,%eax
 65a:	c9                   	leaveq 
 65b:	c3                   	retq   
 65c:	0f 1f 40 00          	nopl   0x0(%rax)

0000000000000660 <__libc_csu_init>:
 660:	41 57                	push   %r15
 662:	41 56                	push   %r14
 664:	49 89 d7             	mov    %rdx,%r15
 667:	41 55                	push   %r13
 669:	41 54                	push   %r12
 66b:	4c 8d 25 76 07 20 00 	lea    0x200776(%rip),%r12        # 200de8 <__frame_dummy_init_array_entry>
 672:	55                   	push   %rbp
 673:	48 8d 2d 76 07 20 00 	lea    0x200776(%rip),%rbp        # 200df0 <__do_global_dtors_aux_fini_array_entry>
 67a:	53                   	push   %rbx
 67b:	41 89 fd             	mov    %edi,%r13d
 67e:	49 89 f6             	mov    %rsi,%r14
 681:	4c 29 e5             	sub    %r12,%rbp
 684:	48 83 ec 08          	sub    $0x8,%rsp
 688:	48 c1 fd 03          	sar    $0x3,%rbp
 68c:	e8 57 fe ff ff       	callq  4e8 <_init>
 691:	48 85 ed             	test   %rbp,%rbp
 694:	74 20                	je     6b6 <__libc_csu_init+0x56>
 696:	31 db                	xor    %ebx,%ebx
 698:	0f 1f 84 00 00 00 00 	nopl   0x0(%rax,%rax,1)
 69f:	00 
 6a0:	4c 89 fa             	mov    %r15,%rdx
 6a3:	4c 89 f6             	mov    %r14,%rsi
 6a6:	44 89 ef             	mov    %r13d,%edi
 6a9:	41 ff 14 dc          	callq  *(%r12,%rbx,8)
 6ad:	48 83 c3 01          	add    $0x1,%rbx
 6b1:	48 39 dd             	cmp    %rbx,%rbp
 6b4:	75 ea                	jne    6a0 <__libc_csu_init+0x40>
 6b6:	48 83 c4 08          	add    $0x8,%rsp
 6ba:	5b                   	pop    %rbx
 6bb:	5d                   	pop    %rbp
 6bc:	41 5c                	pop    %r12
 6be:	41 5d                	pop    %r13
 6c0:	41 5e                	pop    %r14
 6c2:	41 5f                	pop    %r15
 6c4:	c3                   	retq   
 6c5:	90                   	nop
 6c6:	66 2e 0f 1f 84 00 00 	nopw   %cs:0x0(%rax,%rax,1)
 6cd:	00 00 00 

00000000000006d0 <__libc_csu_fini>:
 6d0:	f3 c3                	repz retq 

Disassembly of section .fini:

00000000000006d4 <_fini>:
 6d4:	48 83 ec 08          	sub    $0x8,%rsp
 6d8:	48 83 c4 08          	add    $0x8,%rsp
 6dc:	c3                   	retq  

程序最终都是一条条计算机指令,按照顺序执行的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值