⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 core.asm

📁 一个微型操作系统源码
💻 ASM
字号:
            %TITLE      "instinct - core 0.0.3"

; APM code provided by Vinny <vincent@gmx.at>
; FAT12 code provided by Simeon Maxein <smaxein@freenet.de>
; IDEAL mode code inspired by Tom Swan's Mastering Borland Turbo Assembler

            IDEAL
            MODEL       SMALL, C
            STACK
            JUMPS
            LOCALS      @@
            CODESEG
            P386N
start:
            MOV         AX, CS
            MOV         DS, AX
            MOV         ES, AX
            SHL         EAX, 16d                    ; Map Interrupt 20h
            MOV         AX, OFFSET int20h
            XOR         BX, BX
            MOV         FS, BX
            CLI
            MOV         [FS:20h*4], EAX
            STI

            CALL        consoleClearScreen
            MOV         SI, OFFSET bMSG
            CALL        consoleBufferedOutput
            CALL        consoleCLI
            JMP         $

bMSG        DB          'instinct v0.0.3',13,10,0

wSectsPTrk  DW          18d
wDrivHeads  DW          2d
bDrivNumbr  DB          00h
bdiskfsCYL  DB          ?
bdiskfHEAD  DB          ?
bdiskfSECT  DB          ?
wCoreVersn  DW          0003h









            ORG         700h
int20h:
D00h:       CMP         AH, 00h
            JNE         D01h
            CALL        appReturn
            JMP         Dxxh
D01h:       CMP         AH, 01h
            JNE         D02h
            CALL        appExecute
            JMP         Dxxh
D02h:       CMP         AH, 02h
            JNE         D03h
            CALL        consoleBufferedOutput
            JMP         Dxxh
D03h:       CMP         AH, 03h
            JNE         D04h
            CALL        consoleBufferedInput
            JMP         Dxxh
D04h:       CMP         AH, 04h
            JNE         D05h
            CALL        consoleClearScreen
            JMP         Dxxh
D05h:       CMP         AH, 05h
            JNE         D06h
            CALL        coreBeep
            JMP         Dxxh
D06h:       CMP         AH, 06h
            JNE         D07h
            CALL        coreShutdown
            JMP         Dxxh
D07h:       CMP         AH, 07h
            JNE         D08h
            CALL        diskfsReadSector, BX, CX, ES
            JMP         Dxxh
D08h:       CMP         AH, 08h
            JNE         D09h
            CALL        diskfsWriteSector
            JMP         Dxxh
D09h:       CMP         AH, 09h
            JNE         D0Ah
            CALL        coreQueryVersion
            JMP         Dxxh
D0Ah:       CMP         AH, 0Ah
            JNE         DFFh
            CALL        consoleCLI
            JMP         Dxxh
DFFh:       MOV         AX, 0FFFFh
            STC
Dxxh:
            IRET







PROC consoleCLI NEAR
            MOV         AX, CS
            MOV         DS, AX
            MOV         ES, AX
@@CLILoop:
            MOV         SI, OFFSET @@bPrompt
            CALL        consoleBufferedOutput
            MOV         DI, OFFSET @@bBuffer
            MOV         CX, 0FFh
            CALL        consoleBufferedInput
            CMP         DI, OFFSET @@bBuffer+1
            JE          @@CLILoop
            MOV         SI, OFFSET @@bCRLF
            CALL        consoleBufferedOutput

            CALL        findFile, OFFSET @@bBuffer, 2500h
            OR          BX, BX
            JNZ         @@Fail
            CALL        chainLoad, AX, 5000h
            OR          BX, BX
            JNZ         @@Fail
            PUSH        DS
            PUSH        ES
            DB          09Ah
            DW          0000h
            DW          5000h
            POP         ES
            POP         DS

            JMP         @@CLILoop
@@Fail:
            MOV         SI, OFFSET @@bLoadFail
            CALL        consoleBufferedOutput
            JMP         @@CLILoop

@@bBuffer   DB          256 DUP (?)
@@bCRLF     DB          13,10,0
@@bPrompt   DB          13,10,'> ',0
@@bLoadFail DB          'Unknown Command',13,10,0

ENDP consoleCLI

PROC appReturn NEAR
            XOR         AX, AX
            CLC
            RET

ENDP appReturn

PROC appExecute NEAR
            XOR         AX, AX
            CLC
            RET

ENDP appExecute

PROC consoleBufferedOutput NEAR
@@charLoop:
            LODSB
            OR          AL, AL
            JZ          @@success
            MOV         BX, 0007h
            MOV         AH, 0Eh
            INT         10h
            JMP         @@charLoop

@@success:
            XOR         AX, AX
            CLC
            RET

ENDP consoleBufferedOutput

PROC consoleBufferedInput NEAR
            MOV         DX, DI
@@keyLoop:
            XOR         AX, AX
            INT         16h                         ; Key Input
            CMP         AL, 13d
            JE          @@success
            CMP         AL, 8d
            JE          @@backspace
            MOV         BX, DI
            SUB         BX, DX
            CMP         BX, CX
            JAE         @@keyLoop
            MOV         AH, 0Eh
            MOV         BX, 0007h
            INT         10h                         ; Character Output
@@charSave:
            STOSB
            JMP         @@keyLoop
@@backspace:
            CMP         DI, DX
            JE          @@keyCancel
            DEC         DI
            MOV         AL, 8d
            MOV         AH, 0Eh
            MOV         BX, 0007h
            INT         10h                         ; Character Output
            MOV         AL, ' '
            MOV         AH, 0Eh
            MOV         BX, 0007h
            INT         10h                         ; Character Output
            MOV         AL, 8d
            MOV         AH, 0Eh
            MOV         BX, 0007h
            INT         10h                         ; Character Output
            JMP         @@keyLoop
@@keyCancel:
            MOV         AH, 05h                     ; Beep
            INT         20h
            JMP         @@keyLoop
@@success:
            MOV         CX, DI
            SUB         CX, DX
            MOV         AL, AL
            STOSB
            MOV         AX, AX
            CLC
            RET

ENDP consoleBufferedInput

PROC consoleClearScreen NEAR
            MOV         al, 00h
            MOV         bh, 7d
            MOV         cx, 00h
            MOV         dh, 24d
            MOV         dl, 79d
            MOV         ah, 06h
            INT         10h
            MOV         bh, 00h
            MOV         dh, 00h
            MOV         dl, 00h
            MOV         ah, 02h
            INT         10h
            XOR         AX, AX
            CLC
            RET

ENDP consoleClearScreen

PROC coreBeep NEAR
            CLI
            MOV         DX, 535                     ; higher = quieter 
            MOV         AL, 0B6h
            OUT         43h, AL                     ; PIT ctrl word port
            MOV         AL, DL
            OUT         42h, AL                     ; PIT misc reg port
            MOV         AL, DH
            OUT         42h, AL                     ; PIT misc reg port  
            IN          AL, 61h                     ; 8042 ctrl reg
            MOV         AH, AL
            OR          AL, 03h
            OUT         61h, AL                     ; 8042 ctrl reg
            MOV         AH, 00h                     ; Get time of day 
            INT         1Ah 
            ADD         DX, 04h                     ; Duration: 16th of seconds
            MOV         BX, DX
@@waitLoop:
            INT         1Ah                         ; Get counter
            CMP         DX, BX                      ; Keep waiting
            JNE         @@waitloop
            MOV         AL, AH
            OUT         61h, AL                     ; 8042 ctrl reg
            STI
@@keyWaitLoop:
            MOV         AH, 01h                     ; Wait for key release 
            INT         16h
            JNZ         @@keyRelease
            JMP         @@success
@@keyRelease:
            MOV         AH, 00h                     ; Clear key buffer
            INT         16h
            JMP         @@keyWaitLoop

@@success:
            XOR         AX, AX
            CLC
            RET

ENDP coreBeep

PROC coreShutdown NEAR
            MOV         AX, 5300h
            XOR         BX, BX
            INT         15h
            MOV         AX, 5304h
            XOR         BX, BX
            INT         15h
            MOV         AX, 5301h
            XOR         BX, BX
            INT         15h
            MOV         AX, 5307h
            MOV         BX, 01h
            MOV         CX, 03h
            INT         15h
            PUSH        DS
            MOV         AX, CS
            MOV         DS, AX
            MOV         SI, OFFSET @@shutoff
            MOV         AH, 02h
            INT         20h
            XOR         AX, AX
            INT         16h
            MOV         SI, OFFSET @@canceled
            MOV         AH, 02h
            INT         20h
            POP         DS
            MOV         AX, 0001h
            STC
            RET

@@shutoff   DB          'The system is now safe to switch off...',0
@@canceled  DB          'canceled.',13,10,0

ENDP coreShutdown

PROC diskfsReadSector NEAR  @@SectStart:WORD, @@SectCount:WORD, @@LoadSeg:WORD
            LOCAL       @@Attempt:BYTE
            PUSH        ES
            MOV         [@@Attempt], 03h

@@sectLoop:
            CMP         [@@SectCount], 0000h
            JE          @@success
            CALL        getCHS, [@@SectStart]
            MOV         AX, [@@LoadSeg]
            MOV         ES, AX
            XOR         BX, BX
            MOV         CH, [bdiskfsCYL]
            MOV         CL, [bdiskfSECT]
            MOV         DH, [bdiskfHEAD]
            MOV         DL, [bDrivNumbr]
            MOV         AL, 01h
            MOV         AH, 02h
            INT         13h
            JNC         @@L01
            DEC         [@@Attempt]
            CMP         [@@Attempt], 0
            JE          @@failure
            CALL        resetDisk
            JMP         @@sectLoop
@@L01:
            ADD         [@@LoadSeg], 20h
            INC         [@@SectStart]
            DEC         [@@SectCount]
            JMP         @@sectLoop

@@success:
            POP         ES
            CLC
            RET

@@failure:
            POP         ES
            STC
            RET

ENDP diskfsReadSector

PROC diskfsWriteSector NEAR

            XOR         AX, AX
            CLC
            RET

ENDP diskfsWriteSector

PROC coreQueryVersion NEAR
            MOV         AX, [wCoreVersn]
            CLC
            RET

ENDP coreQueryVersion

PROC memAlloc NEAR      @@length:WORD
            MOV         AX, [@@length]
            ADD         [@@NextSeg], AX             ; 4K
            MOV         AX, [@@NextSeg]
            RET

@@NextSeg   DW          500h
ENDP memAlloc






PROC getCHS NEAR        @@address:WORD
            MOV         AX, [@@address]
            XOR         DX, DX
            DIV         [wSectsPTrk]
            INC         DX
            MOV         [bdiskfSECT], DL
            XOR         DX, DX
            DIV         [wDrivHeads]
            MOV         [bdiskfsCYL], AL
            MOV         [bdiskfHEAD], DL
            RET

ENDP getCHS

PROC resetDisk NEAR
            MOV         DL, [bDrivNumbr]
            MOV         AH, 0
            INT         13h
            JC          @@failure

@@success:
            XOR         BX, BX
            RET

@@failure:
            MOV         BX, 0001h
            RET

ENDP resetDisk

PROC chainLoad NEAR     @@Cluster:WORD, @@StartSeg:WORD
            PUSH        ES
            MOV         AX, [@@Cluster]

@@chlstart:
            ADD         AX, 31d
            CALL        diskfsReadSector, AX, 0001h, [@@StartSeg]
            JC          @@failure
            ADD         [@@StartSeg], 20h

            MOV         AX, 2000h                   ; FAT
            MOV         ES, AX
            MOV         SI, [@@Cluster]
            SHL         SI, 01d                     ; si=si*2
            ADD         SI, [@@Cluster]             ; si=si+cluster (si=si*3)
            SHR         SI, 01d                     ; si=si/2 (si=si*1.5)
            MOV         AX, [ES:SI]
            JNC         @@L01

            SHR         AX, 04d
            JMP         @@L02
@@L01:
            AND         AX, 0FFFh
@@L02:
            CMP         AX, 0FF8h
            JAE         @@success
            MOV         [@@Cluster], AX
            JMP         @@chlstart

@@success:
            POP         ES
            XOR         BX, BX
            RET

@@failure:
            POP         ES
            MOV         BX, 0001h
            RET

ENDP chainLoad

PROC findFile NEAR      @@FilePointer:WORD, @@StartSeg
            PUSH        ES
            MOV         AX, [@@StartSeg]
            MOV         ES, AX
            XOR         DX, DX

@@ffloop1:
            MOV         SI, [@@FilePointer]
            MOV         DI, DX
            MOV         CX, 11
            CLD
            REPE CMPSB
            JE          @@getCluster
            ADD         DX, 32d
            CMP         DX, 1C00h                   ; 7K
            JA          @@failure
            JMP         @@ffloop1

@@getcluster:
            ADD         DX, 26d
            MOV         DI, DX
            MOV         AX, [ES:DI]                 ; Start Cluster
            ADD         DI, 4d
            MOV         CX, [ES:DI]                 ; Size ( <= 64k )

@@success:
            POP         ES
            XOR         BX, BX
            RET

@@failure:
            POP         ES
            MOV         BX, 0001h
            RET

ENDP findFile

            END         start

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -