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

📄 kernel.asm

📁 一个微型操作系统源码
💻 ASM
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	    Project:	LeeOS
;;	     Author:	Robert Lee  <robert.lee@leeos.cjb.net>
;;	       Date:	Thursday, January 04, 2001
;;	Description:	LeeOS kernel
;;  Binary Location:	/system/kernel.sys
;;
;;	Notes:
;;		In its current form this file sets up the system interupt, displays the
;;		welcome message and loads the command prompt (shell.asm). Filesystem
;;		support will be added in version 0.0.3. The entire API will be rewritten
;;		in future versions.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.model tiny
.code
.386

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Services
;;
;;		These are the services, they are only run when INT 20h is called
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
int20h:
	cmp ah, 0			; Call service by number
	je AH_0h
	cmp ah, 1
	je AH_1h
	cmp ah, 2
	je AH_2h
	cmp ah, 3
	je AH_3h
	cmp ah, 4
	je AH_4h
	cmp ah, 5
	je AH_5h
	cmp ah, 6
	je AH_6h
	cmp ah, 7
	je AH_7h
	mov AX, 0FFFFh			; Error: Service not found
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 0: Return control to LeeOS
AH_0h:
	pop ax
	pop ax
	pop ax
	xor ax, ax
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	; Service 1: Pass control to an application
AH_1h:
runprog:
	;mov ECX, 4			; Sector#
	xor bx, bx			; Offset
	mov ax, 7000h			; Segment
	mov es, ax
	mov si, 10			; Sector count
	mov dl, 0			; Floppy A:
	mov ah, 7
	int 20h
	mov ax,7000h			; Pass control to program
	mov es,ax
	mov ds,ax
	push ax
	mov ax,0
	push ax
	retf
	xor ax, ax
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 2: Buffered Output
AH_2h:
	lodsb
	or al, al
	jz AH_2hd
	mov ah, 0Eh
	mov bx, 0007h
	int 10h
	jmp AH_2h
AH_2hd:
	xor ax, ax
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 3: Buffered Input
AH_3h:
	mov dx, di			; Remember initial offset
getkey:
	xor ax, ax			; Get a key
	int 16h
	cmp al, 13			; Enter?
	je AH_3hd
	cmp al, 8			; Backspace?
	je backspace
	mov bx, di			; Are we at CX bytes already?
	sub bx, dx
	cmp bx, cx
	jae getkey
	mov ah, 0Eh			; Write Character
	mov bx, 0001h
	int 10h
	cmp al, 65			; Lowercase only
	jb writeit
	cmp al, 90
	ja writeit
	add al, 32
writeit:				; Got a key
	stosb				; Record and display keystroke
	jmp getkey
backspace:				
	cmp di, dx			; Beginning of string?
	je noback
	dec di				; Go back in buffer
	mov al, 8			; Move cursor back
	mov ah, 0Eh
	mov bx, 0001h
	int 10h
	mov al, 32			; Print a space
	mov ah, 0Eh
	mov bx, 0001h
	int 10h
	mov al, 8			; Move cursor back again
	mov ah, 0Eh
	mov bx, 0001h
	int 10h
	jmp getkey			; Get another key
noback:
	mov ah, 5			; BEEP!
	int 20h
	jmp getkey			; Get another key
AH_3hd:					; Finish up
	mov cx, di			; CX = byte count
	sub cx, dx
	xor al,al			; Zero-terminate
	stosb
	xor ax, ax			; Success
	iret				; Return
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 4: Clear Screen
AH_4h:
	mov ah, 6
	mov al, 0
	mov bh, 7
	mov cx, 0
	mov dh, 24
	mov dl, 79
	int 10h
	mov ah, 2
	mov bh, 0
	mov dh, 0
	mov dl, 0
	int 10h
	xor ax, ax
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 5: Beep PC Speaker
AH_5h:
	pusha
	cli				; Clear interrupts 
	mov dx,535			; Volume: higher = quieter 
	mov al,0b6h
	out 43h,al			; program. int timer ctl word port
	mov al,dl
	out 42h,al			; program. int timer misc. reg port
	mov al,dh
	out 42h,al			; program. int timer misc. reg port  
	in al,61h			; Read port 61h 8042 ctrl reg
	mov ah,al
	or al,3
	out 61h,al			; Write port 61h 8042 ctrl reg
	mov ah,0			; Get time of day 
	int 1ah 
	add dx,4			; 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			; Write port 61h 8042 ctrl reg
	sti				; Allow interrupts
	eep:
	mov ah,1			; Wait for key release 
	int 16h
	jnz releez
	popa
	xor ax, ax			; Success
	iret
	releez:
	mov ah,0			; Clear key buffer
	int 16h
	jmp eep
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 6: Shutdown System
;data:
	shutoff		db 'It is now safe to shut off your computer...',0
	canceled	db 'canceled.',13,10,0
AH_6h:
	mov ax,5300h			; APM code provided my Vinny <mailto:vincent@gmx.at>
	xor bx, bx			; Thanks!
	int 15h
	mov ax,5304h
	xor bx, bx
	int 15h
	mov ax,5301h
	xor bx, bx
	int 15h
	mov ax,5307h
	mov bx,1
	mov cx,3
	int 15h
	mov ax, ds			; Save DS
	push ax
	mov ax, cs			; DS = CS
	mov ds, ax
	mov si, offset shutoff		; Print Shutdown Message
	mov ah, 2
	int 20h
	xor ax, ax			; Wait for Keypress
	int 16h
	mov si, offset canceled		; Print Canceled Message
	mov ah, 2
	int 20h
	pop ax				; Restore DS
	mov ds, ax
	mov ax, 1			; Return 'Canceled'
	iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	Service 7: Load Sector
AH_7h:
	pusha				; Reset Disks
	mov dl, 0
	mov ax, 0
	int 13h
	popa
	MOV EAX,ECX			; EAX = Sector
	PUSH ESI			; Save For Whatever Reason
	PUSH BX				; Save Buffer Offset
	PUSH DX				; Save Drive
	XOR EDX,EDX			; Prepare For Dividing
	MOV ESI,EDX			; Set High 16 bits of ESI to 0 For Divide
	MOV SI,18			; ESI = 32 bit Sectors per Cylinder
	DIV ESI				; EDX = Remainder / EAX = Quotient
	INC DX				; DX = Sector
	MOV CX,DX			; CX = Sector
	XOR EDX,EDX			; EDX = 0 for another divide
	MOV ESI,EDX			; ESI = 0 Again
	MOV SI,2			; ESI = 32 bit Heads per Cylinder
	DIV ESI				; DX = Head / AX = Cylinder
	MOV BX,DX			; Save Head
	POP DX				; Get Drive Back (DL)
	MOV DH,BL			; Set DH to Head
	MOV CH,AL			; CH = Cylinder
	SHR AX,2			; AH = bits 10 and 11 of Cylinder
	AND AL,11000000b		; Set AL for adding to CL
	ADD CL,AL			; Add on bits 8 and 9 of Cylinder
	SHL AH,6			; Set AH for adding to DH
	ADD DH,AH			; Add on bits 10 and 11 of Cylinder
	POP BX				; Restore Offset
	POP ESI				; Restore ESI
	MOV AX,SI			; Set AL to number to read (1 to xx)
	MOV AH,2			; Function AH=2 / INT 13h = Read Sector
	INT 13h				; Read sector(s) into memory
	IRET
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;	The End
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	END

⌨️ 快捷键说明

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