head_test.asm
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [bits 32] 2 start_32: 3 mov ax,0x10 4 mov ds,ax 5 mov word [gdtr],gdt_end-gdt 6 mov dword [gdtr+2],gdt 7 mov word [idtr],idt_end-idt 8 mov dword [idtr+2],idt 9 lgdt [gdtr] 10 lidt [idtr] 11 mov ax,0x10 12 mov ds,ax 13 mov es,ax 14 mov ss,ax ; stack segment = data segment = code seg 15 mov sp,stack_top 16 mov ax,0x18 17 mov gs,ax 18 19 call clr_scr 20 call welcome 21 22 jmp $ 23 24 welcome: 25 mov esi,str_welcome 26 sub edi,edi 27 mov ah,0ch 28 rp_welcome: 29 mov al,[esi] 30 test al,al ; al & al = 0 ? 31 jz end_welcome 32 mov [gs:edi],ax 33 inc esi 34 add edi,2 35 jmp rp_welcome 36 end_welcome: 37 ret 38 39 clr_scr: 40 xor ah,ah 41 mov al,' ' 42 mov ecx,2000 43 sub edi,edi 44 rp_clr: 45 mov [gs:edi],ax 46 add edi,2 47 dec ecx 48 jne rp_clr 49 ret 50 51 52 53 ;--------------------------------------------------------------------- 54 ; data segment, ds:esi, r/w 55 ; 因为数据段和代码段重叠,而前面一部分已经被代码段覆盖,所以 56 ; 数据段str_welcome的地址不是 0,而是代码段地址结尾处。 57 ; 堆栈段包含在数据段中,即16MB的末尾部分。 58 ; 实际分布:代码段->数据段->堆栈段 59 60 str_welcome: db "Hello, OS! Welcome to protected mode!",0 61 62 align 2 63 gdt: dw 0,0,0,0 ; 256*4*2bytes = 2KB 64 dw 0x1000,0x0000,0x9a00,0x00c0 ; 16MB 65 dw 0x1000,0x0000,0x9200,0x00c0 66 dw 0x0002,0x8000,0x920b,0x00c0 67 times (256-4) dw 0,0,0,0 68 gdt_end: 69 70 idt: times 256 dw 0,0,0,0 ; 256 interrupts 71 idt_end: 72 73 gdtr: dw 0,0,0 74 align 2 75 idtr: dw 0,0,0 76 77 ;--------------------------------------------------------------------- 78 ; stack segment 79 80 stack_buttom: times 512 dw 0 ;1KB 81 stack_top: