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

📄 bm.asm

📁 一个操作系统启动代码
💻 ASM
字号:
;*******************************************************************
; Bootmaker 
;  (c) Andre v.d. Merwe 4-Feb-1999
;  <dart@pobox.com> <dart@iafrica.com>
;  See "zen.txt" for copyright information
;
; Zen Source Library. Assembler example 3
; http://users.iafrica.com/d/da/dart/zen/zen.html
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
; Compiler TASM
;
; Boot Maker (bm) is a simple application (dos com file) that writes 
;  data to an absolute location on a disk. 
;*******************************************************************


	TITLE BootMaker
	JUMPS

	.model Tiny

;---------------------------------------------------------------------------------
.code
.286
org 100h
;---------------------------------------------------------------------------------

start:  
	 ; Get the command line and use it as the file name
	mov	dx,	offset CmdLine
	call	func_GetCommandLine


	 ; There must be at least 5 chars on cmd line   [start_tract, space, start_sector, space, file_name]
	cmp	ax,	5
	jae	Got_Command_Line


Bad_Command_Line:
	 ; No file name given,  show error message
	mov	dx,	offset Error_No_Command_Line
	call	func_ShowString
	jmp	Exit_App



Got_Command_Line:

	;--------------------------------
	; Get start track from cmd line
	;--------------------------------

	 ;Get 1st byte should be start track [0-9]
	mov	si,	offset CmdLine
	mov	al,	[si]

	 ;Less than 0 = error
	cmp	al,	'0'
	jb	Bad_Command_Line

	 ;Greater than 9 = error
	cmp	al,	'9'
	ja	Bad_Command_Line


	 ;Store start track as a number
	sub	al,	'0'
	mov	[StartTrack],	al


 	 ;Next char must be a space
	inc	si
	mov	al,	[si]
	cmp	al,	' '
	jne	Bad_Command_Line



	;--------------------------------
	; Get start sector from cmd line
	;--------------------------------

	 ;Get 1st byte should be start sector [1-9]
	inc	si
	mov	al,	[si]

	 ;Less than 1 = error
	cmp	al,	'1'
	jb	Bad_Command_Line

	 ;Greater than 9 = error
	cmp	al,	'9'
	ja	Bad_Command_Line


	 ;Store start sector as a number
	sub	al,	'0'
	mov	[StartSector],	al


 	 ;Next char must be a space
	inc	si
	mov	al,	[si]
	cmp	al,	' '
	jne	Bad_Command_Line



	;------------------------------------------
	; Open the file to be written to the disk
	;------------------------------------------

	 ; Open the file
	 ;   On int 21h  AX = file handle, or error
	mov	ah,	3dh	; int 21h 3dh  (open file)
	mov	al,	00h	; open mode readonly
	mov	dx,	offset_FileName
	int	21h

	jc	Open_Error
	jmp	Open_Ok


Open_Error:
	 ; Cant open file
	mov	dx,	offset Error_Cant_Open_File
	call	func_ShowString
	jmp	Exit_App


Open_Ok:
	 ; Save the file handle
	mov	[File_Handle],	ax


Read_File:
	 ; int 21h - 3fh  read file
	mov	ah,	3fh
	mov	bx,	[File_Handle]
	mov	cx,	512
	mov	dx,	offset File_Data
	int	21h


	 ;Was 512 bytes of data read
	cmp	ax,	512
	jl	   No_More_Data

	 ;There could be more data
	mov	[MoreFileData],	1
	jmp	Process_Current_Data


No_More_Data:
	 ;No more data to read
	mov	[MoreFileData],	0


Process_Current_Data:

	 ; AX contains bytes read
	cmp	ax,	0
	je	Close_File

 
Write_To_Disk:

	;--------------------------------------------
	; Data loaded write it to A:\'s boot sector
	;--------------------------------------------

	mov	ah,	03		;int 13h service 03  (Disk Write)
	mov	al,	01		;write 1 sector
	mov 	ch,	[StartTrack]	;track y
	mov	cl,	[StartSector]	;sector x
	mov	dh,	0		;side 1
        mov     dl,     0               ;drive
	push	ds
	pop	es
	mov	bx,	offset File_Data
	int	13h

	jnc	Disk_Ok

	 ; Data not succesfully written to disk
	mov	dx,	offset Msg_Error_Exit
	call	func_ShowString
	jmp	Close_File
	

Disk_Ok:
	 ;Next sector
	add	[StartSector],	1
	
	 ;More data?
	cmp	[MoreFileData],	1
	je	Read_File
	

	 ; Data succesfully written to disk
	mov	dx,	offset Msg_Ok_Exit
	call	func_ShowString
		

		
Close_File:	;Assumes handle is in [File_Handle]

	mov	ah,	3eh	; int 21h - 3eh  Close File
	mov	bx,	[File_Handle]
	int	21h



Exit_App:
	int	20h
;---------------------------------------------------------------------------------




;---------------------------------------------------------------------------------
; Function func_ShowString
;  Displays string using int 21h - 09
;  String must be at ds:dx and terminated with a '$'
;---------------------------------------------------------------------------------
func_ShowString		proc

	mov	ah,	09
	int	21h
	ret

func_ShowString		endp
;---------------------------------------------------------------------------------





;---------------------------------------------------------------------------------
;  Function  func_GetCommandLine
;    Stores the whole command line to the address in dx  (ds:dx)
;    Command line can be a max of 127 chars,  the buffer at ds:sx
;     must thus be 128 chars long  (127 + terminating 0)
;
;   Returns the length of the command line in ax
;---------------------------------------------------------------------------------
func_GetCommandLine	proc

	 ; Get length of command line
	mov	cl,	cs:[80h]
	xor	ch,	ch


	 ; Command line available
	cmp	cl,	0
	je	No_Command_Line


	 ; Ignore leading space
	dec	cl
	cmp	cl,	0
	je	No_Command_Line	


	 ; AX stored the loop count, ie current pos in command line
        mov     ax,     cx


cmdline_top:
	cld					;From si to di
	mov	si,	82h			;Source
	mov	di,	offset CmdLine		;Dest
	rep	movsb				;Move string (byte by byte)
	mov	byte ptr [di],	0			;Null terminate the string
	

No_Command_Line:
	ret


func_GetCommandLine	endp
;---------------------------------------------------------------------------------





;---------------------------------------------------------------------------------
.data
;---------------------------------------------------------------------------------
MoreFileData	db	0
File_Handle	dw	0

CmdLine		db	128	dup(0)
offset_FileName	EQU	(offset CmdLine) + 4

File_Data	db	512	dup(0)
StartSector	db	0
StartTrack	db	0


Error_No_Command_Line:
	db	13, 10
	db	'Invalid parameter!'
	db	13, 10
	db	'  bm.com start_sector start_track full_file_name', 13,10
	db	'     start_track  0 to 9', 13, 10
	db	'     start_sector 1 to 9', 13, 10, '$'
	db	13, 10, '$'

Error_Cant_Open_File:
	db	13, 10
	db	'Unable to open file'
	db	13, 10, '$'

Msg_Ok_Exit:
	db	13, 10
	db	'File succesfully writen to disk 0, track x, sector n'
	db	13, 10, '$'


Msg_Error_Exit:
	db	13, 10
	db	'Unable to write file to disk 0, track x, sector n'
	db	13, 10, '$'

;---------------------------------------------------------------------------------


end     start



⌨️ 快捷键说明

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