Compilation Writing a self-started disk

zhaozj2021-02-16  57

The following is posted two code boot.asm and shell.asm, implement disk launch, and resolve two commands Time and Reboot, interested friends can take a look. Since I have a very dish, this article is just how to look at how the disk start is realized. If there is a wrong place, everyone can point out that is too thankful.

Steps: Place a disk, compile these two code into EXE, execute once, restart your computer, if there is a virtual system environment, such as the Virtual PC, you can see the effect.

The following is boot.asm

; Start code; ---------------------------------------------- -------

Code_SEG Segment Para 'Code'main Proc Far Assume CS: Code_SEG, DS: CODE_SEG ORG 00HSTART: PUSH DS SUB AX, AX PUSH AX MOV AX, CODE_SEG MOV DS, AX MOV ES, AX

MOV AX, 0301H; Write 1 sector MOV CX, 0001H MOV BX, 7C00H; starting from code 7C00H MOV DX, 0 int 13h

MOV AX, 0301H; write 2 sectors, data MOV CX, 0002H MOV BX, 7E00H; start MOV DX, 0 INT 13H RET ORG 7C00H; MBR starts MOV AX, 0 MOV ES, AX MOV AX, 201h MOV BX 7E00H; read 2 sectors into 7E00H MOV CX, 2; second sector MOV DX, 0 int 13h

MOV AH, 6H; Qingdou MOV Al, 26 MOV BH, 07H MOV CX, 0 MOV DH, 26 MOV DL, 80 INT 10H

MOV AX, 1301H; Show text MOV BX, 04EH MOV CX, 18 MOV DX, 0 Lea BP, Hello; 7E00H State Stalk INT 10h

MOV AH, 3H; Rank MOV BH, 0 INT 10H Inc DH MOV DL, 0 MOV AH, 2H MOV BH, 0 INT 10H JMP INIT; Jump to Initial Code ORG 7DFEH DB 55H, 0AH ORG 7E00H; Data Send Hello DB 'Loading system ....' org 7f00h; Initialization Zone

Init: MOV AX, 0 MOV ES, AX MOV DS, AX

MOV AX, 201H; 5 sector read 8000H, load the command interpreter into memory MOV BX, 8000H MOV CX, 5 MOV DX, 0 int 13h

MOV AX, 201H; 7 sectors read 8400h, put the command handler MOV BX, 8400h MOV CX, 7 MOV DX, 0 int 13h

JMP command; jump to the command interpreter org 7ffeh db 55h, 0aahcommand: org 8000h main endpcode_seg Ends End Start

The following is shell.asm

Command parser; -------------------------------------------- ---------------- Code_SEG Segment Para 'Code'main Proc Far Assume CS: CODE_SEG, DS: CODE_SEGSTART: PUSH DS SUB AX, AX PUSH AX MOV AX, CODE_SEG MOV DS, AX Mov ES, AX

MOV AX, 0301H; Write 5 sectors MOV CX, 0005H MOV BX, 8000H; start MOV DX, 0 INT 13HMOV AX, 0301H; write 7 sector data MOV CX, 0007H MOV BX, 8400H; starting from code 8400h MOV DX, 0 int 13h RET

ORG 8000H

Kaishi: Call PRINTTSF LEA DI, Command; Command Entering Start MOV DX, 0 Push DX Begin: MOV AH, 0H INT 16H

CMP Al, 0DH; equals Enter JE Finish

CMP Al, 08 JNE Sast Call BackGB JMP BEGIN Sast: POP DX CMP DX, 13; Command Maximum 14 Character JA TOLONG INC DX PUSH DX Stosb; Storage Single Character MOV AH, 9H; Print Single Character MOV BH, 0 MOV BL, 07h MOV CX, 1 INT 10h Call Tuigb; Cursor Mobile Jump1: JMP BEGINTOLONG: PUSH DX; Warning Sound MOV DX, 100 in Al, 61h And Al, 11111100Bsound: XOR Al, 2 OUT 61H, Al Mov CX, 140HWAIT1: loop Wait1 DX JNE Sound JMP Beginfinish: POP DX CMP DX, 0 JE NOS CALL SCROLLNOS: CALL CHECK_COM JMP KAISHI

Main endp; ----------------------------------- ------------- Command DB 14 DUP ('); 6 sector Start Messrb DB' System Will Reboot Now! 'Messnf DB' Input Command IsoT! 'TSF DB' $ 'Complist DB 'Reboot', 00H, 84H DB 'Time', 00H, 85H; -------------------------------------------------------------------------------------------------------------------------- --------------------------- CHECK_COM PROC NEAR LEA BX, Comlist; It is easy to locate the first address of each command Lea Di, Comlist; command table First address MOV DX, 2; Number of instructions CMPCom: ​​Lea Si, Command; Storage Enter the address CLD MOV CX, 14 REPZ CMPSB JZ Match Add BX, 16 MOV DI, BX DEC DX JNZ CMPCOM CALL GETGB MOV AX, 1301H; Show text Mess2 NO MOV BX, 07H MOV CX, 25 Lea BP, Messnf; No Found Address INT 10h Call Scroll

Lea Di, Command; Clear Command MOV CX, 0014 CLD MOV AX, 20H Rep Stosb RetMatch: Add BX, 14 Call [BX]; Location Command Processing address Lea Di, Command; Clear Command MOV CX, 0014 CLD MOV AX, 20H Rep Stosb retcheck_com Endp; ------------------------------------------------------------------------------------------------------------ --------------- Scroll Proc Near Call getGB CMP DH, 23; whether to reach 23 line JBE SCREND; judgment whether to reach the bottom MOV DL, 0; arrive at the bottom, to the first column Call setgb

Mov AH, 6; Rolling 1 line MOV Al, 1 MOV BH, 07 MOV CX, 0 MOV DH, 26 MOV DL, 80 INT 10h Retscrend: Call GETGB Inc DH MOV DL, 0 CALL SETGB RESCROLL ENDPTUIGB PROC NEAR CALL GETGB INC DL Call setgb rettuigb endp; --------------------------------------------- ----------------- PRINTTSF Proc Near Mov Ah, 9h; Print Single Character $ MOV Al, TSF MOV BH, 0 MOV BL, 07H MOV CX, 1 INT 10H Call Tuigb Retprinttsf endpbackgb proc near call getgb dec dl call setgb retbackgb endpgetgb proc near mov ah, 3h mov bh, 0 int 10h retgetgb endpsetgb proc near mov ah, 2h mov bh, 0 int 10h retsetgb endp org 83feh db 55h, 0aahorg 8400hreboot proc near call getgb MOV AX, 1301H; Display Text Restart Information MOV BX, 07H MOV CX, 23 LEA BP, Messrb; Reboot Character Address INT 10H Call Scroll MOV BL, 0FEH; Restart Command, Utilize Keyboard Controller XOR CX, CXCMD_Wait: in Al, 64H test al, 2 jz cmd_send loop cmd_wait jmp cmd_errorcmd_send: mov al, bl out 64h, al xor cx, cxcmd_accept: in al, 64h test al, 2 jz cmd_ok loop cmd_acceptcmd_error: mov ah, 1 jmp cmd_exitcmd_ok: xor ah, ahcmd_exit: retreboot ENDP

ORG 8500HTIME PROC Near Mov Ah, 4H INT 1AH PUSH CX

Lea Di, NOWTIME MOV Al, CH Call BCD2ASC POP CX MOV Al, Cl, DH Call BCD2ASC Inc Di Mov Al, DL Call BCD2ASC MOV AH, 2H INT 1AH PUSH CX INC DI MOV Al, CH Call BCD2ASC INC DI POP CX MOV AL, CL CALL BCD2ASC Inc Di Mov Al, DH Call BCD2ASC

Call GETGB MOV AX, 1301H MOV BX, 07H MOV CX, 19 Lea BP, NOWTIME; Time Address INT 10H Call Scroll

RET NOWTIME DB 4 DUP (?) DB '/' DB 2 DUP (?) DB '/' DB 2 DUP (?) DB '' DB '', ':', '', ':', '' Time Endp

BCD2ASC Proc Near Mov BL, Al Mov Cl, 4 Shr Al, Cl OR Al, 30H MOV [DI], Al Inc Di Mov Al, BL And Al, 0FH OR Al, 30H MOV [DI], Al INC DI RETBCD2ASC ENDP ORG 85FEH DB 55H, 0AHCode_Seg Ends; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------ End Start, THX

转载请注明原文地址:https://www.9cbs.com/read-24685.html

New Post(0)