日期:2014-05-16  浏览次数:20698 次

保护模式下写变量问题
现在在学习写内核 

一写变量就出现内核错误呀 怎么搞的? 
我把内核放到了0x80000处 
链接内核用的是ld 
-N -s --oformat binary -e _start -Ttext 0x80000 

我知道这也许是保护模式下定位变量的问题 
但我用的是C语言怎么会这样? 


kernel.asm 
[section .text] 
[bits 32] 
global _start 
extern init 

_start: 
  mov ax,cs 
  mov ds,ax 
  mov es,ax 
  mov fs,ax  
  jmp init 

init.c 
void init() 

  qwe=1; <----------------运行到这里就出现堆栈错误 
  while(1) 
  ; 

堆栈没有问题 已经检查了 
用bochs调试结果: 


D:\Program Files\Bochs-2.3.pre3\tinix> D:\Progra~1\Bochs-2.3.pre3\bochsdbg.exe -q -f bochsrc.bxrc 
00000000000i[APIC?] local apic in initializing 
======================================================================== 
  Bochs x86 Emulator 2.3.pre3 
  Build from CVS snapshot on August 6, 2006 
======================================================================== 
00000000000i[ ] reading configuration from bochsrc.bxrc 
00000000000i[ ] installing win32 module as the Bochs GUI 
00000000000i[ ] using log file bochsout.txt 
Next at t=0 
(0) [0xfffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b ; ea5be000f0 
<bochs:1> pb 0x80000 
<bochs:2> c 
(0) Breakpoint 1, 0x00080000 in ?? () 
Next at t=1796393 
(0) [0x00080000] 0030:00080000 (unk. ctxt): mov ax, cs ; 668cc8 
<bochs:3> s 
Next at t=1796394 
(0) [0x00080003] 0030:00080003 (unk. ctxt): mov ds, ax ; 8ed8 
<bochs:4> 
Next at t=1796395 
(0) [0x00080005] 0030:00080005 (unk. ctxt): mov es, ax ; 8ec0 
<bochs:5> 
Next at t=1796396 
(0) [0x00080007] 0030:00080007 (unk. ctxt): mov fs, ax ; 8ee0 
<bochs:6> 
Next at t=1796397 
(0) [0x00080009] 0030:00080009 (unk. ctxt): jmp .+0x00000046 (0x00080054) ; e946000000 
<bochs:7> 
Next at t=1796398 
(0) [0x00080054] 0030:00080054 (unk. ctxt): push ebp ; 55 
<bochs:8> 
Next at t=1796399 
(0) [0x00080055] 0030:00080055 (unk. ctxt): mov ebp, esp ; 89e5 
<bochs:9> 
Next at t=1796400 
(0) [0x00080057] 0030:00080057 (unk. ctxt): sub esp, 0x00000008 ; 83ec08 
<bochs:10> 
Next at t=1796401 
(0) [0x0008005a] 0030:0008005a (unk. ctxt): mov dword ptr ds:0x80080, 0x0000004e ; c705800008004e000000 
<bochs:11> print-stack 
Stack address size 4 
  ¦ STACK 0x000003f3 [0x00ff53f0] 
  ¦ STACK 0x000003f7 [0x00ff53f0] 
  ¦ STACK 0x000003fb [0x00ff53f0] 
  ¦ STACK 0x000003ff [0x0003f8f0] 
  ¦ STACK 0x00000403 [0x00000000]