BIOS 基础培训:
自己刚入职时接受的BIOS一些培训回忆出来和大家分享
1, DATASORT
要求:
(1) 用汇编写一个序档为 DATASORT.asm 的程序。
(2) 把 Address Range FFFF0000---FFFFFFFh 64KB 的资料存到名字为DATAORG.DAT的档案。
(3) 该64KB的资料以DWORD 的单位由小到大排序
(4) 书面以Hex show 出该资料排序前与排序后的DWORD checksum值
(5) 将排序的结果存到名字为DATASORT.DAT 档案。
现在来分析这个题目,主要要注意以下几点,要进去 Big real mode, 怎么建立文件,怎么将数据存入文件,怎样排序,以及排序的算法:
创建文件:
push cx
mov dx, offset sun
mov ah,3Ch
mov cx,0
int 21h
pop cx
写文件:
mov bx,ax
mov cx,10000h/2
mov ax,cs:BuffSeg
mov dx,offset IOBuffer
mov ds,ax
mov ah,40h
int 21h
排序用冒泡排序法,大家大学的时候应该都学过这个吧,就不详细说了。
编译器用MASM.exe link.exe ml.exe
.586p
Option SEGMENT:USE16
INCLUDEX86.INC
_DATA SEGMENTPUBLIC 'DATA'
GDT_Begin MyGDT
GdtEntrysBigReal, 00000000h, 000000h, 93h ;4GB Base 0
GDT_End
ControlFlags DW 0
A20_DISABLED EQU 0001h
errNotRealMode DB 'Please run thisprogram in real-mode DOS !!',13,10,0
msgBootBlock DB 13,10, 'Before Sort BootBlock Checksum: ', 0
amsgBootBlock db 13,10, 'After Sort Boot BlockChecksum:',0
msg db 13,10, 'Please Wait ,the programme isruuning',0
msgToQuit DB 13,10,10, 'Press <Enter> to Quit...',0
sun db 'c:/dataorg.dat',0
hui db 'c:/datasort.dat',0
_DATA ENDS
;-------------------------------------------------------------------------------------
_TEXT SEGMENTPUBLIC 'CODE'
ASSUMECS:_TEXT, DS:_DATA
EXTERN BIOS_Get_KBD :NEAR ;BIOSTTY.ASM
EXTERN BIOS_Display_Char : NEAR
EXTERN BIOS_Display_String : NEAR
DataSeg DW SEG_DATA
BuffSeg DW SEG_BUFF
start:
mov ax, 0ABCDh
int 12h ;ForDebug Break purpose
cld
mov ds, cs:[DataSeg]
smsw ax
test al, 01h ;IsCPU in Protected Mode?
jz short IsRealMode ;No, it is in Real Mode.
mov si, offset errNotRealMode
ErrorExit:
call BIOS_Display_String
mov ax, 4CFFh
int 21h ;Terminatewith error
IsRealMode:
;Prepare GDT Table for setting Big-Real mode
movzx eax, DataSeg
shl eax, 4
add eax, offset MyGDT ;EAX=Physical addr of MyGDT
or dword ptr MyGDT+2, eax ;Update Base of GDTbl
lgdt fword ptr MyGDT
;Switch CPU to protected mode and set ES to 4GB limit (Base=0)
pushf
cli
mov eax, cr0
push eax
or al, 01h
mov cr0, eax ;SetCPU to protected mode
jmp short $+2
mov dx, sBigReal
mov es, dx ;Load4GB limit selector to ES
pop eax
mov cr0, eax ;Backto real mode
jmp short $+2
xor ax, ax
mov es, ax ;ES= Base 0, 4GB limit
popf
call CheckEnableA20Gate
mov si, offset msg
call BIOS_Display_String
;Move 64KB Data from Boot Block (0FFFF0000h) to Buffer.
movzx edi, cs:BuffSeg
shl edi, 4 ;EDI=physicaladdr of buffer
mov esi, 0FFFF0000h
mov ecx, 10000h/4 ;ECX=64KBytes=16K Dwords
rep movsd [edi], es:[esi]
;---------------------------------------------------------------creatdataorg.dat file
push cx
mov dx, offset sun
mov ah, 3Ch
mov cx, 0
int 21h
pop cx
;----------------------------------------------------------------writeto dataorg.dat file
push ds
mov bx,ax
mov cx,10000h/2
mov ax,cs:BuffSeg
mov dx,offset IOBuffer
mov ds,ax
mov ah, 40h
int 21h
pop ds
push ds
mov cx,10000h/2
mov ax,cs:BuffSeg
mov dx,10000h/2
mov ds,ax
mov ah, 40h
int 21h
pop ds
;Caculate DWORD checksum of 64KB BootBlock. Result in EDX.
push fs
mov fs, cs:BuffSeg
xor si, si ;FS:SI=ptrto Buffer
xor edx, edx
mov cx, 10000h/4 ;CX=16KDWORDs
@@:
lodsd fs:[si] ;load the address ofds:e[si] to eax
add edx,eax
loop @b
pop fs
;EDX = DWORD checksum of [FFFF0000~FFFFFFFFh]. (Boot Block)
push edx
;---------------------------------------------------------------creatdatasort.dat file
call bubble
push cx
mov dx, offset hui
mov ah, 3Ch
mov cx, 0
int 21h
pop cx
;----------------------------------------------------------------writeto datasort.dat file
push ds
mov bx,ax
mov cx,10000h/2
mov ax,cs:BuffSeg
mov dx,0000h
mov ds,ax
mov ah, 40h
int 21h
mov cx,10000h/2
mov ax,cs:BuffSeg
mov dx,10000h/2
mov ds,ax
mov ah, 40h
int 21h
pop ds
;--------------------------------------------------------------------------------------------
;Caculate DWORD checksum of 64KB Boot Block. Result in EDX.
push fs
mov fs, cs:BuffSeg
xor si, si ;FS:SI=ptrto Buffer
xor edx, edx
mov cx, 10000h/4 ;CX=16KDWORDs
@@:
lodsd fs:[si] ;load the address ofds:e[si] to eax
add edx,eax
loop @b
pop fs
;EDX = DWORD checksum of [FFFF0000~FFFFFFFFh]. (Boot Block)
push edx
call RestoreA20Gate
;Show the checksum result by Hex.
mov si, offset amsgBootBlock
call BIOS_Display_String
pop eax
push ax
call ShowAXHex
pop ax
call ShowAXHex
mov si, offset msgBootBlock
call BIOS_Display_String
pop eax
push ax
call ShowAXHex
pop ax
call ShowAXHex
mov si, offset msgToQuit
call BIOS_Display_String
@@:
call BIOS_Get_KBD
cmp al, 0Dh ;Is <Enter> Key?
jnz short @b ;Wait until<Enter> pressed
mov ax, 4C00h ;ProgramTerminate
int 21h ;returnto DOS