The third day of self-made operating system merk11 in 30 days

Time:2022-1-11

The third day of self-made operating system merk11 in 30 days

Making real IPL

First, refer to the code harinote IPL

Purpose: read in 10 cylinders

; haribote-ipl
; TAB=4

CYLS 	 EQU 		 ten 				;  Declare cyls = 10

		ORG 		 0x7c00 			;  Indicates the program load address

;  Standard FAT12 format floppy code

		JMP		entry
		DB		0x90
		DB 		 "HARIBOTE" 		;  Boot sector name (8 bytes)
		DW 		 five hundred and twelve 				;  Size of each sector (512 bytes required)
		DB 		 one 				;  Cluster size (must be 1 sector)
		DW 		 one 				;  Fat start position (generally the first sector)
		DB 		 two 				;  Number of fat (must be 2)
		DW 		 two hundred and twenty-four 				;  Root directory size (usually 224 items)
		DW 		 two thousand eight hundred and eighty 			;  The disk size (must be 2880 sectors 1440 * 1024 / 512)
		DB 		 0xf0 			;  Disk type (must be 0xf0)
		DW 		 nine 				;  Length of fat (must be 9 sectors)
		DW 		 eighteen 				;  A track has several sectors (must be 18)
		DW 		 two 				;  Number of heads (must be 2)
		DD 		 0 				;  Partition not used, must be 0
		DD 		 two thousand eight hundred and eighty 			;  Rewrite disk size once
		DB 		 0,0,0x29 		;  Meaning unknown (fixed)
		DD 		 0xffffffff 		;  (possibly) volume label number
		DB 		 "HARIBOTEOS " 	;  Name of the disk (must be 11 bytes, not enough space)
		DB 		 "FAT12   " 		;  Disk format name (must be 8 bytes, not enough space)
		RESB 	 eighteen 				;  Free 18 bytes first

;  Program subject

entry:
		MOV 		 AX,0 			;  Initialization register
		MOV		SS,AX
		MOV		SP,0x7c00
		MOV		DS,AX

;  Read disk

		MOV		AX,0x0820
		MOV		ES,AX
		MOV 		 CH,0 			;  Cylinder 0
		MOV 		 DH,0 			;  Head 0
		MOV 		 CL,2 			;  Sector 2

readloop:
		MOV 		 SI,0 			;  Record failure count register

retry:
		MOV 		 AH,0x02 			;  Ah = 0x02: read in disk
		MOV 		 AL,1 			;  1 sector
		MOV		BX,0
		MOV 		 DL,0x00 			;  A drive
		INT 		 0x13 			;  Call disk BIOS
		JNC 		 next 			;  If there is no error, jump to next
		ADD 		 SI,1 			;  Add 1 to Si
		CMP 		 SI,5 			;  Compare Si with 5
		JAE 		 error 			;  Si > = 5 jump to error
		MOV		AH,0x00
		MOV 		 DL,0x00 			;  A drive
		INT 		 0x13 			;  Reset drive
		JMP		retry
next:
		MOV 		 AX,ES 			;  Move the memory address back 0x200 (512 / 16 hex conversion)
		ADD		AX,0x0020
		MOV 		 ES,AX 			;  Add es, 0x020 because there is no add es, it can only be done through ax
		ADD 		 CL,1 			;  Add 1 to CL
		CMP 		 CL,18 			;  Compare CL with 18
		JBE 		 readloop 		;  CL < = 18 jump to readloop
		MOV		CL,1
		ADD		DH,1
		CMP		DH,2
		JB 		 readloop 		;  DH < 2 jump to readloop
		MOV		DH,0
		ADD		CH,1
		CMP		CH,CYLS
		JB 		 readloop 		;  Ch < cyls jump to readloop

;  After reading, jump to harinote Sys execution!
		MOV 		 [0x0ff0],CH 		;  IPL がどこまでんだのかをモ
		JMP		0xc200

error:
		MOV		SI,msg

putloop:
		MOV		AL,[SI]
		ADD 		 SI,1 			;  Add 1 to Si
		CMP		AL,0
		JE		fin
		MOV 		 AH,0x0e 			;  Display a text
		MOV 		 BX,15 			;  Specifies the character color
		INT 		 0x10 			;  Calling the graphics card BIOS
		JMP		putloop

fin:
		HLT 						;  Stop the CPU and wait for instructions
		JMP 		 fin 				;  Infinite loop

msg:
		DB 		 0x0a, 0x0a 		;  Wrap twice
		DB		"load error"
		DB 		 0x0a 			;  Line feed
		DB		0

		RESB 	 0x7dfe-$ 		;  Fill in 0x00 until 0x001fe

		DB		0x55, 0xaa
		
; Please indicate the source for Reprint: https://www.cnblogs.com/merk11/p/15512260.html

Switch picture mode & enter 32-bit mode

; haribote-os boot asm
; TAB=4

BOTPAK 	 EQU 		 0x00280000 		;  Load bootpack
DSKCAC 	 EQU 		 0x00100000 		;  Location of disk cache
DSKCAC0 	 EQU 		 0x00008000 		;  Location of disk cache (real mode)

;  BOOT_ Info related
CYLS 	 EQU 		 0x0ff0 			;  Boot sector settings
LEDS	EQU		0x0ff1
VMODE 	 EQU 		 0x0ff2 			;  Information about colors
SCRNX 	 EQU 		 0x0ff4 			;  Resolution X
SCRNY 	 EQU 		 0x0ff6 			;  Resolution y
VRAM 	 EQU 		 0x0ff8 			;  The starting address of the image buffer

		ORG 		 0xc200 			;   The memory address of the program to be loaded

;  Screen setting

		MOV 		 AL,0x13 			;  VGA graphics card, 320x200x8bit
		MOV		AH,0x00
		INT		0x10
		MOV 		 BYTE [VMODE],8 	;  Screen mode (refer to the reference of C language)
		MOV		WORD [SCRNX],320
		MOV		WORD [SCRNY],200
		MOV		DWORD [VRAM],0x000a0000

;  Get indicator status through BIOS

		MOV		AH,0x02
		INT		0x16 			; keyboard BIOS
		MOV		[LEDS],AL

;  Prevent pic from accepting all interrupts
; 	 Specification and PIC initialization of at compatible machine
; 	 Then suspend without doing anything in the CLI
; 	 Pic is initialized after consent

		MOV		AL,0xff
		OUT		0x21,AL
		NOP 						;  Continuously execute out instruction
		OUT		0xa1,AL

		CLI 						;  Further interrupt CPU

;  Let the CPU support more than 1m memory and set a20gate

		CALL	waitkbdout
		MOV		AL,0xd1
		OUT		0x64,AL
		CALL	waitkbdout
		MOV		AL,0xdf			; enable A20
		OUT		0x60,AL
		CALL	waitkbdout

;  Protection mode conversion

[INSTRSET "i486p"] 				;  Description Use 486 instruction

		LGDT 	 [GDTR0] 			;  Set temporary GDT
		MOV		EAX,CR0
		AND 		 EAX,0x7fffffff 	;  Use bit31 (disable paging)
		OR 		 EAX,0x00000001 	;  Bit0 to 1 conversion (protection mode transition)
		MOV		CR0,EAX
		JMP		pipelineflush
pipelineflush:
		MOV 		 AX,1*8 			;   Write 32bit segments
		MOV		DS,AX
		MOV		ES,AX
		MOV		FS,AX
		MOV		GS,AX
		MOV		SS,AX

;  Bootpack delivery

		MOV 		 ESI,bootpack 	;  source
		MOV 		 EDI,BOTPAK 		;  target
		MOV		ECX,512*1024/4
		CALL	memcpy

;  Transfer disk data

;  Start from the boot area

		MOV 		 ESI,0x7c00 		;  source
		MOV 		 EDI,DSKCAC 		;  target
		MOV		ECX,512/4
		CALL	memcpy

;  All remaining

		MOV 		 ESI,DSKCAC0+512 	;  source
		MOV 		 EDI,DSKCAC+512 	;  target
		MOV		ECX,0
		MOV		CL,BYTE [CYLS]
		IMUL 	 ECX,512*18*2/4 	;  Divide by 4 to get the number of bytes
		SUB 		 ECX,512/4 		;  IPL offset
		CALL	memcpy

;  Because asmhead is still needed to complete
;  Complete the remaining bootpack tasks

;  Bootpack startup

		MOV		EBX,BOTPAK
		MOV		ECX,[EBX+16]
		ADD		ECX,3			; ECX += 3;
		SHR		ECX,2			; ECX /= 4;
		JZ 		 skip 			;  Transmission complete
		MOV 		 ESI,[EBX+20] 	;  source
		ADD		ESI,EBX
		MOV 		 EDI,[EBX+12] 	;  target
		CALL	memcpy
skip:
		MOV 		 ESP,[EBX+12] 	;  Initialization of stack
		JMP		DWORD 2*8:0x0000001b

waitkbdout:
		IN		 AL,0x64
		AND		 AL,0x02
		JNZ 		 waitkbdout 		;  If the and result is not 0, jump to waitkbdout
		RET

memcpy:
		MOV		EAX,[ESI]
		ADD		ESI,4
		MOV		[EDI],EAX
		ADD		EDI,4
		SUB		ECX,1
		JNZ 		 memcpy 			;  Jump to memcpy if the operation result is not 0
		RET
;  Memcpy address prefix size

		ALIGNB	16
GDT0:
		RESB 	 eight 				;  Initial value
		DW 		 0xffff,0x0000,0x9200,0x00cf 	;  Write 32bit bit segment register
		DW 		 0xffff,0x0000,0x9a28,0x0047 	;  32bit register of executable file (for bootpack)

		DW		0
GDTR0:
		DW		8*3-1
		DD		GDT0

		ALIGNB	16
bootpack:
; Please indicate the source for Reprint: https://www.cnblogs.com/merk11/p/15512260.html

Implement HLT

; naskfunc
; TAB=4

[FORMAT "WCOFF"] 				;  Mode of making object file	
[BITS 32] 						;  Machine language for making 32-bit mode


;  Information about making target files

[FILE "naskfunc.nas"] 			;  Source file name information

		GLOBAL 	_ io_ hlt 			;  The name of the function contained in the program


;  Here are the actual functions

[SECTION .text] 		;  Write these in the object file before writing the program

_io_hlt:	; void io_hlt(void);
		HLT
		RET
; Please indicate the source for Reprint: https://www.cnblogs.com/merk11/p/15512260.html

C language

/*Tell the C compiler that there is a function in another file*/

void io_hlt(void);

/*Is a function declaration, but instead of {}, use;, This means:
	The function is in another file. Find it yourself*/

void HariMain(void)
{

fin:
	io_ hlt(); /*  Execute naskfun In NAS_ io_ HLT function*/
	goto fin;

}
; Please indicate the source for Reprint: https://www.cnblogs.com/merk11/p/15512260.html

Assembly language learning

Equ: equivalent to in C language#define

JC: it is the abbreviation of “jump if carry”, which means to jump if the carry flag is 1.

JB: it is the abbreviation of “jump if below”. If less than, jump

In BIOS:

AH=0x02; (disk reading) ah = 0x03; (write disk)

AH=0x04; (calibration) ah = 0x0c; (seek)

Al = number of sectors to be processed; (only consecutive sectors can be processed at the same time)

Ch = cylinder number &0xff;

CL = sector code (0-5 digits) | (cylinder number & 0x300) > > 2;

DH = head number;

DL = drive letter;

Es: BX = buffer address; (not used for verification and seek)

Return value: (the return value here is used in conjunction with JC)

FLACS. CF = = 0: no error, ah = = 0

FLAGS. CF = = 1: there is an error, and the error number is stored in ah (the same as reset function)

Set the graphics card mode:

AH=0x00;

Al = mode: (some unimportant picture modes are omitted)

0x03: 16 color character mode, 80 × twenty-five

0x12: VGA graphics mode, 640 × four hundred and eighty × 4-bit color mode, unique 4-side storage mode

0x13: VGA graphics mode, 320 × two hundred × 8-bit color mode, palette mode

0x6a: extended VGA graphics mode, 800 × six hundred × 4-bit color mode, unique 4-side storage mode (some graphics cards do not support this mode)

Return value: None

Computer composition learning

floppy disk

It is divided into 18 sectors along the radius

It is divided into 80 cylinders by concentric circle

Since both sides of the disk can record data, there are two heads

Generally, when saving a file to an empty floppy disk

(1) The file name will be written after 0x002600;

(2) The contents of the file will be written after 0x004200.

Compiling principle learning

Own understanding:

The. C file is first generated under the processing of the gcc compiler Gas files,

The. Gas file is then generated Obj object file

The. Obj object file is generated after passing link BIM file

Although. BIM is a binary image file, it lacks the necessary file header and compression

Therefore, it needs to be processed into HRB real complete machine language file

The author’s explanation of the compilation process is shown in the original work:

First, use CC1 Exe from bootpack.exe C generate bootpack gas。

The second step is to use gas2nask Exe from bootpack.exe Gas generates bootpack nas。

Step 3: use nask Exe from bootpack.exe NAS generates bootpack obj。

Step 4: use obi2bim Exe from bootpack.exe Obj generates bootpack bim。

Finally, use bim2hrb Exe from bootpack.exe BIM generates bootpack hrb。

In this way, the machine language is made, and then use the copy instruction to asmhead Bin and bootpack When HRB is simply combined, it becomes harinote sys。