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

📄 boot.s

📁 用于汇编领域的,运用于OS的MAIN函数.基于硬件基础的源代码
💻 S
字号:
# Boot sector for 1.44 Mb floppy disk# The Major task of this boot sector is finding and loading the setup# from the floppy disk to offset of 0x0920:0000## DS, ES : 0x07C0:0000# SS : 0x07E0:0000 SP : 0x01F0#.code16.text.globl	_start_start:	jmp	_bentry	nop	# SET BPB Entry	# But, this entry is Not used	# we will copy this image except BPB using unix command 'dd'	.byte	'm','y','O','S',0x20,0x20,0x20,0x20	# OEM Name	.byte	0x00,0x02				# BytePerSector	.byte	1					# SectorPerCluster	.byte	1,0					# ReservedSector	.byte	2					# NumberOfFAT	.byte	0xe0,0x00				# RootEntryCount	.byte	0x40,0x0b				# TotalSector	.byte	0xf0					# MediaType	.byte	0x09,0					# FATSize	.byte	0x12,0					# SectorPerTrack	.byte	0x02,0					# NumberOfHead	.byte	0,0,0,0					# HiddenSector	.byte	0,0,0,0					# TotalSector# Start point of boot sector code_bentry:	# Set each segment	# DS: 0x07c0:0000	# ES: 0x07e0:0000	# SS: 0x07e0:0000	movw	$0x07c0, %ax	movw	%ax, %ds	#movw	%ax, %es	movw	$0x0900, %ax	movw	%ax, %es	movw	$0x07e0, %ax	movw	%ax, %ss	movw	$0x01f0, %ax	movw	%ax, %sp	# Reset Disk Controller	xorw	%ax, %ax	xorw	%dx, %dx	int	$0x13	# We will find root directory entry from sector 0x12 to 0x20	# for convenienceload_loop:	incw	block_num	cmpw	$0x20, block_num	jne	load_proceed	call	loader_not_foundload_proceed:	xorw	%bx, %bx	movw	block_num, %ax	call	read_block		# read block at 0x0900:0000scan_loop:	movw	%bx, %di	movw	$loader_name, %si	movw	$11, %cx	cld	repe	cmpsb			# compare string	jcxz	found_file	addw	$32, %bx		# root dir entry size	cmpw	$0x200, %bx		# sector size	jl	scan_loop	jmp	load_loopfound_file:	movw	%es:26(%bx), %ax	# cluster number	addw	$33, %ax		# data area start	subw	$2, %ax			# ??	movw	%ax, ldr_block		# maybe sector per cluster is 1	movw	%es:28(%bx), %ax	# size	movw	$0x200, %bx	xorw	%dx, %dx	divw	%bx	incw	%ax	movw	%ax, ldr_sects	movw	$0x9a0, %ax	movw	%ax, %es	xorw	%bx, %bx###################### For DEBUG#	movw	ldr_block, %ax#	call 	print_numberh#	movw	ldr_sects, %ax#	call	print_numberh#####################do_load:	# loading setup to 0x9a0:0000	movw	ldr_block, %ax	call	read_block	decw	ldr_sects	cmpw	$0x00, ldr_sects	je	loading_done	addw	$0x200, %bx	incw	ldr_block	jmp	do_loadloading_done:	# killing driver motor & jmp to setup	movw	$0x3f2, %dx	xorw	%ax, %ax	outb	%al, %dx	movw	$msg1, %si	call	print_string	ljmp	$0x09a0, $0x0000	# jump to setup code############################# convert lba block num in AX to CHS sector numberlba_to_chs:	pushw	%bx	pushw	%ax	movw	$0x24, %bx	# calc track number	xorw	%dx, %dx	divw	%bx	movb	%al, cyl	popw	%ax		# it stores lba block num	pushw	%ax	movw	$0x12, %bx	# calc head number	xorw	%dx, %dx	divw	%bx	movw	$0x02, %bx	xorw	%dx, %dx	divw	%bx	cmpw	$0x00, %dx	jne	head_1	xorw	%dx, %dx	jmp	get_sect	xorw	%dx, %dxhead_1:	movb	$1, %dlget_sect:	movb	%dl, head	popw	%ax		# it stores lba block num	movw	$0x12, %bx	xorw	%dx, %dx	divw	%bx	incb	%dl	movb	%dl, sect	popw	%bx	retread_block:	# block number in AX	call	lba_to_chs	movb	cyl, %ch	movb	sect, %cl	movb	head, %dh	movb	$0, %dl	movw	$0x0201, %ax		# read to ES:BX	int	$0x13	ret################################## ERROR routineloader_not_found:	movw	$ldr_error, %sipr_loop:	lodsb	cmpb	$0x00, %al	je	pr_exit	movb	$0x0e, %ah	xorw	%bx, %bx	int	$0x10	jmp	pr_looppr_exit:	hlt	jmp pr_exit	ret################################### DEBUG functionprint_numberh:	pushw	%ax	movw	$hex_prefix, %si	call	print_string	popw	%ax	movw	$0x10, %bx	call	number_convert	retprint_string:	movb	(%si), %al	cmpb	$0x00, %al	je	end_print	movb	$0x0e, %ah	xorw	%bx, %bx	int	$0x10	incw	%si	jmp	print_stringend_print:	retnumber_convert:	movw	$number_buf, %di	xorw	%cx, %cxloop1:	pushw	%bx	xorw	%dx, %dx	divw	%bx	popw	%bx	movw	$number_array, %si	addw	%dx, %si	movb	(%si), %dl	movb	%dl, (%di)	incw	%cx	incw	%di	cmpw	$0x0000, %ax	jg	loop1loop2:	decw	%di	movb	(%di), %al	movb	$0x0e, %ah	xorw	%bx, %bx	int	$0x10	loop	loop2	ret#################################### Variablesloader_name:	.ascii	"SETUP      "		# file name with 8.3 formatldr_block:	.word	0x0000ldr_sects:	.word	0x0000ldr_error:	.asciz	"SETUP is not found... Please reboot."sect:	.byte	0x00cyl:		.byte	0x00head:	.byte	0x00block_num:	.word	0x0012############################ DEBUG variablesnumber_array:	.ascii	"0123456789ABCDEF"hex_prefix:	.asciz	"0x"number_buf:	.fill	32,1,0msg1:	.asciz	"jmp to setup"############################# Boot sector signature.org 0x1fe	.word	0xAA55

⌨️ 快捷键说明

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