俺啥也不懂-不知道什么时候才会编写操作系统-再写Hello World-讲讲gcc和ld
感觉我智商有问题啊,网上关于中断的解释太多了,我居然看不懂,反正没有我想要的例子就是看不懂。
还是先贴hello world
; Hello World Program - asmtutor.com
; Compile with: nasm -f elf helloworld.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 helloworld.o -o helloworld
; Run with: ./helloworld
SECTION .data
msg db 'Hello World!', 0Ah ; assign msg variable with your message string
SECTION .text
global _start
_start:
mov edx, 13 ; number of bytes to write - one for each letter plus 0Ah (line feed character)
mov ecx, msg ; move the memory address of our message string into ecx
mov ebx, 1 ; write to the STDOUT file
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
int 80h
为什么上面的代码编译链接之后就能打印hello world,我一开始还认为是寄存器的问题,考虑的是哪个寄存器该存哪个类型的值,哭了。
后来直接搜索80h中断,发现这就是linux规定好的,原来我写的是linux汇编,智商果然是硬伤,我不研究内核了,伤不起。
建议大家看一下这篇文章:https://blog.csdn.net/xiaominthere/article/details/17287965,作者应该不会生气,相当于替作者宣传
eax,ebx,ecx,edx一共四个寄存器,当eax等于4的时候相当于调用linux的sys_write,当eax等于1的时候相当于调用linux内核的sys_exit,而这两种情况下,其他几个寄存器应该是怎样的值都有明确的规定。所以,俺觉得以后凡是有中断的地方,第一个反应应该是找到相关中断的说明,然后用现有的例子进行验证。
当然sys_eixt和sys_write位于linux内核的哪个文件上面那个地址的作者也给大家一一列举,非常细心。
下一篇从自制操作系统里面挑一段hello world的代码继续写