📄 program-kernel-boot-comment.html
字号:
<p> call empty_8042
<br> mov
al,#0xD1
| command write
<br> out
#0x64,al
<br> call empty_8042
<br> mov
al,#0xDF
| A20 on
<br> out
#0x60,al
<br> call empty_8042
<p>| well, that went ok, I hope. Now we have to reprogram the interrupts
:-(
<br>| we put them right after the intel-reserved hardware interrupts, at
<br>| int 0x20-0x2F. There they won't mess up anything. Sadly IBM really
<br>| messed this up with the original PC, and they haven't been able to
<br>| rectify it afterwards. Thus the bios puts interrupts at 0x08-0x0f,
<br>| which is used for the internal hardware interrupts as well. We just
<br>| have to reprogram the 8259's, and it isn't fun.
<br>| 初始化中断处理器8259i
<br>| 初始化顺序为: 1.
向主8259A写ICW1, 0x20
<br>|
2. 向第二块8259A写ICW1, 0xA0
<br>|
3. 向主8259A写ICW2, 0x21
<br>|
4. 向第二块8259A写ICW2, 0xA1
<br>|
5. 如果ICW1指示有级联中断处理器,则初始化Master&Slave
<br>|
(在下例中只有IR2有级联8259A), 0x21, 0xA1
<br>|
6. 向两块8259写ICW4,指定工作模式.
<br>| 输入了适当的初始化命令之后, 8259已经准备好接收中断请求.
<br>| 现在向他输入工作
<br>| 命令字以规定其工作方式. 8259A共有三个工作命令字,但下例中只用过OCW1.
<br>| OCW1将所有的中断都屏蔽掉, OCW2&OCW3也就没什么意义了.
<br>| ** ICW stands for Initialization Command Word;
<br>| OCW for Operation Command Word.
<br>1. mov al,#0x11
<br> out
#0x20,al
<br> .word 0x00eb,0x00eb
| jmp $+2, jmp $+2
<br>2. out #0xA0,al
| and to 8259A-2
<br> .word 0x00eb,0x00eb
<br>3. mov al,#0x20
| 向主8259A写入ICW2.
<br> out
#0x21,al
| 硬件中断入口地址0x20, 并由ICW1
<p>
| 得知中断向量长度 = 8 bytes.
<br> .word 0x00eb,0x00eb
<br>4. mov al,#0x28
| start of hardware int's 2 (0x28)
<br> out
#0xA1,al
| 第二块8259A的中断入口是0x28.
<br> .word 0x00eb,0x00eb
<br>5. mov al,#0x04
| 8259-1 is master
<br> out
#0x21,al
| Interrupt Request 2有级联处理.
<p> .word 0x00eb,0x00eb
<br> mov
al,#0x02
| 8259-2 is slave
<br> out
#0xA1,al
| 于上面对应,告诉大家我就是IR2对应
<br>
| 级联处理器.
<br> .word 0x00eb,0x00eb
<br>6. mov al,#0x01
| 8086 mode for both
<br> out
#0x21,al
<br> .word 0x00eb,0x00eb
<br> out
#0xA1,al
<p> .word 0x00eb,0x00eb
<br> mov
al,#0xFF
| mask off all interrupts for now
<br> out
#0x21,al
<p> .word 0x00eb,0x00eb
<br> out
#0xA1,al
<p>| well, that certainly wasn't fun :-(. Hopefully it works, and we don't
<br>| need no steenking BIOS anyway (except for the initial loading :-).
<br>| The BIOS-routine wants lots of unnecessary data, and it's less
<br>| "interesting" anyway. This is how REAL programmers do it.
<br>|
<br>| Well, now's the time to actually move into protected mode. To make
<br>| things as simple as possible, we do no register set-up or anything,
<br>| we let the gnu-compiled 32-bit programs do that. We just jump to
<br>| absolute address 0x00000, in 32-bit protected mode.
<p> mov
ax,#0x0001 | protected mode (PE) bit
<br> lmsw ax
| This is it!
<br> jmpi 0,8
| jmp offset 0 of segment 8 (cs)
<p>| This routine checks that the keyboard command queue is empty
<br>| No timeout is used - if this hangs there is something wrong with
<br>| the machine, and we probably couldn't proceed anyway.
<br>empty_8042:
<br> .word 0x00eb,0x00eb
<br> in
al,#0x64 | 8042 status port
<br> test al,#2
| is input buffer full?
<br> jnz
empty_8042 | yes - loop
<br> ret
<p>| This routine loads the system at address 0x10000, making sure
<br>| no 64kB boundaries are crossed. We try to load it as fast as
<br>| possible, loading whole tracks whenever we can.
<br>|
<br>| in: es - starting address segment (normally 0x1000)
<br>|
<br>| This routine has to be recompiled to fit another drive type,
<br>| just change the "sectors" variable at the start of the file
<br>| (originally 18, for a 1.44Mb drive)
<br>|
<br>sread: .word 1
| sectors read of current track
<br>head: .word 0
| current head
<br>track: .word 0
| current track
<br>read_it:
<br> mov ax,es
| ES当前应0x1000
<br> test ax,#0x0fff
| 必需确保ES处在64KB段边界上,即0x?000:XXXX.
<br>
| 要不你就会收到一个"DMA..."什么什么的ERR.
<br>die: jne die
| es must be at 64kB boundary
<br> xor bx,bx
| bx is starting address within segment
<br>rp_read:
| **** 循环入口处 ****
<br> mov ax,es
<br> cmp ax,#ENDSEG
| have we loaded all yet?
<br> jb ok1_read
<br> ret
<br>ok1_read:
<br> mov ax,#sectors
| 1.44M, sectors=18,linux的后续版本
<br>
| 中已改成由操作系统来探测sectors的值.
<br> sub ax,sread
| AX内记载需要读的扇区数,初始sread为1,
<br>
| 即跳过第一道的第一扇区(BOOT区)
<br> mov cx,ax
|
<br> shl cx,#9
| CX算出需要读出的扇区的字节数, ax*512.
<br> add cx,bx
| BX是当前段内偏移.
<br>
| 下面连续的两个转移指令开始还真让人莫名其妙.
<br> jnc ok2_read
| 这里先检查当前段内的空间够不够装ax个扇区
<br>
| cx算出字节数,加上当前偏移试试,够了的话,就
<br>
| 跳到ok2_read去读吧!
<br> je ok2_read
| 这么巧的事也有,刚刚够! 读!
<br>
| 如果到了这里就确认溢出了,看下面的:
<br> xor ax,ax
| 这段代码我觉得很精巧.
<br> sub ax,bx
| 它主要目的就是算出如果当前段内空间不够的话,
<br> shr ax,#9
| 那么反算出剩余空间最多能装多少个扇区,那么
<br>
| 就读出多少个.(Hint,段内空间是扇区的整数倍)
<p>ok2_read:
<br> call read_track
| 读取当前磁道.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -