cod*_*ith 7 assembly operating-system nasm 16-bit bootloader
我一直在尝试设计一个简单的操作系统,只是引导扇区,以及带有中断的16位实模式.我终于能够制作OS/bootloader,我在虚拟盒中进行了测试,并且它有效.
然后我将图像刻录到CD上,并将其启动到我的旧桌面,使用Pentium 4,BIOS修订版A05和1GB内存,并且它工作得很好 - 一个简单的操作系统,在顶部打印一个"标题"屏幕,所有它允许您键入屏幕,注册几个键导航光标.
然后我将光盘插入我1年前的笔记本电脑,配备i5处理器,2.6 GB RAM和A05 BIOS Revision,光标似乎随机移动,高速打印随机字符,最后停在anscii字符处235(扩展字符表的一部分),此时键盘工作正常,指定移动光标的键工作正常,只是标题.这是我测试它的计算机,对其进行了编译,编写并将其刻录成CD.(我使用Linux Mint 12 OS)
我已经跳过了我认为需要做的所有"箍":制作了一个iso图像,遵循El Torito'无仿真'启动标准,启动签名,512字节,并写入正确的扇区.
这是我的代码的问题,我没有做某事,或者这是正常的吗?
这是我的代码(NASM x86语法):
;**************************
; Note OS, Experimental OS
;**************************
[org 0x7C00]
[bits 16]
start:
jmp loader ;jump to the actual start of bootloader
times 8 - ($ - $$) db 0 ;pad eight bytes
;*********************
;El Torito Boot Info Table
;*********************
;in nasm, I couldn't figure out how to reserve bytes, in the middle of .text
;so I zeroed it out.
times 56 db 0
loader:
call cls ;clear the screen
mov si, head1 ;setup page headers
call printf
mov si, head2
call printf
jmp note ;start note program
cls:
mov ah, 0x0F ;get current video mode
mov al, 0x00 ;reset register
int 0x10 ;get video mode
mov ah, 0x00 ;set video mode
int 0x10 ;reset screen
mov ah, 0x02 ;set cursor pos
mov bh, 0x00 ;page 00
mov dh, 0x00 ;row 00
mov dl, 0x00 ;col. 00
int 0x10 ;set pos
ret
printf:
.loop ;our function that loops
mov al, [si] ;load byte
cmp al, 0 ;if null, end
je .end
mov ah, 0x0E ;function 0E
mov bh, 0x00 ;page 0x00
mov bl, 0x0F ;white text on black background
int 0x10 ;print
inc si ;increment source index
jmp .loop ;repeat
.end
ret ;return
;*******************
; Note 'Program'
;*******************
note:
mov ah, 0x00 ;function 00
int 0x16 ;get character
cmp al, '`' ;go up line?
je setcur
cmp al, 0x0D ;enter?
je setent
cmp al, '+' ;plus?
je setplu
cmp al, '-' ;minus?
je setminu
cmp al, '\' ;reset?
je loader
cmp al, 0x08 ;backspace?
je setback
mov ah, 0x0E ;function 0E
mov bh, 0x00 ;page 00
mov bl, 0x0F ;white on black
int 0x10 ;print
jmp note ;repeat
setcur:
mov ah, 0x03 ;get cur pos
mov bh, 0x00 ;page 00
int 0x10 ;get pos
cmp dh, 0x00 ;are we at top of page?
je .begin ;just reset cursor if so
sub dh, 0x01 ;go up one line
.begin
mov dl, 0x00 ;set to beginning of line
mov ah, 0x02 ;set cursor function
mov bh, 0x00 ;page 00
int 0x10 ;set position
jmp note ;read next character
setent:
mov ah, 0x0E ;write character
mov al, 0x0A ;begin line
mov bh, 0x00 ;page 00
mov bl, 0x0F ;white on black
int 0x10 ;print
setplu:
mov ah, 0x03 ;get cursor pos
mov bh, 0x00 ;page 0x00
int 0x10 ;get pos
mov ah, 0x02 ;set cursor pos
add dl, 0x01 ;add one to column
int 0x10 ;set new pos
jmp note ;get next char
setminu:
mov ah, 0x03 ;get cursor pos
mov bh, 0x00 ;page 00
int 0x10 ;get pos
mov ah, 0x02 ;set cursor pos
sub dl, 0x01 ;sub one to column
int 0x10 ;set new pos
jmp note ;get next char
setback:
mov ah, 0x03 ;get cursor pos
mov bh, 0x00 ;page 00
int 0x10 ;get pos
mov ah, 0x02 ;set cursor pos
sub dl, 0x01 ;sub one column
int 0x10 ;set pos
mov ah, 0x0E ;write char
mov al, ' ' ;write space
mov bh, 0x00 ;page 00
mov bl, 0x0F ;white on black
int 0x10
mov ah, 0x02 ;reset cur pos
int 0x10 ;reset
jmp note
;******************
; Our Page Headers
;******************
head1: db '- Note OS Version 1.2-', 0x0A, 0x0D, 0
head2: db '=======================', 0x0A, 0x0D, 0x0A, 0x0D, 0
times 510 - ($ - $$) db 0
dw 0xAA55
Run Code Online (Sandbox Code Playgroud)
供参考(我引用的东西):
Anscii表:http://www.asciitable.com/
El-Torito信息:http://wiki.osdev.org/El-Torito
编辑:这是我编程的键和它们的作用:
输入 - 正常工作现在退格 - 现在正常工作加 - 向右移动光标减去 - 向左移动光标` - 将光标移动到前一行的开头\ - "软重启"几乎跳到加载器的开头
问题就在这里:
[org 0x7C00]
...
start:
jmp loader ;jump to the actual start of bootloader
...
loader:
call cls ;clear the screen
mov si, head1 ;setup page headers
call printf
...
printf:
.loop ;our function that loops
mov al, [si] ;load byte
Run Code Online (Sandbox Code Playgroud)
您期望CS=DS=0代码何时启动。DS 不保证为 0。因此,当它非零时,mov al, [si]从段 0 以外的某个段读取字节,并且可能存在一些垃圾,而不是问候消息的副本。
此外,某些 BIOS 使用 的地址跳转到您的代码,0:0x7C00而其他 BIOS 使用0x7C0:0,这意味着即使 CS 也不能保证具有固定值。
我会做的是这样的:
[org 0x7C00]
[bits 16]
start:
jmp loader ;jump to the actual start of bootloader
...
loader:
jmp 0:loader2 ; this far jump guarantees CS=0
loader2:
push cs
pop ds ; this guarantees DS=0
; the rest of the code goes here
Run Code Online (Sandbox Code Playgroud)