Linux汇编写引导扇区


最近再看《Orange‘s一个操作系统的实现》,里面用的是intel的汇编语法,自己比较习惯AT&T的语法了,自己研究了好长时间才将代码修改成功。

  1. .code16  
  2. .section.text  
  3. .globl_start  
  4. _start:  
  5. movw%cs,%ax  
  6. movw%ax,%ds  
  7. movw%ax,%es  
  8. callDispStr  
  9. loop1:jmploop1  
  10. DispStr:  
  11. movw$BootMessage,%ax  
  12. movw%ax,%bp  
  13. movw$0x10,%cx  
  14. movw$0x1301,%ax  
  15. movw$0xc,%bx  
  16. movb$0,%dl  
  17. int$0x10  
  18. ret  
  19. BootMessage:.ascii "Hello, OS world!"  
  20. .org510  
  21. .word0xAA55  

 .code16指示as生成16位的代码。While `as' normally writes only "pure" 32-bit i386 code or 64-bit x86-64 code depending on the default configuration, it also supports writing code to run in real mode or in 16-bit protected mode code segment.

编译的话 as -o boot.o boot.s ;

ld -Ttext=0x7c00 --oformat binary -o boot.bin boot.o;

这样就ok了~~

相关内容