;[]-------------------------------------------------------------------------[]
;|	Program name: 3_2_3.ASM                                              |
;|	Image   file: None                                                   |
;|	Note        : Write the register value and sector data to files.     |
;|                    List the subroutines only.                             |
;[]-------------------------------------------------------------------------[]


;------- Data area ---------------------

count		dw	0
reg_buf		dw	800H dup (0FFFFH)
reg_file	db	'REGISTER.DAT',0
sec_file	db	'A_SECTOR.DAT',0
handle		dw	0


;------- INT 13H -----------------------

;---------------------------------------
; Record register value after accessing disk.
;---------------------------------------

record_reg2	proc	near

	pushall				; Save all the register values

	mov	ax,cs			; Adjust DS, ES
	mov	ds,ax
	mov	es,ax

	cmp	count,0F00H		; Check for buffer remainder size
	ja	record2_done		; Not enough, don't record

	mov	di,offset reg_buf	; Get buffer start address
	add	di,count		; Adjust di with count

	get_ax				; Get AX from stack
	mov	AX_2,ax			; Put AX to buffer
	get_bx				; Get BX from stack
	mov	BX_2,ax			; Put BX to buffer
	get_cx				; Get CX from stack
	mov	CX_2,ax			; Put CX to buffer
	get_dx				; Get DX from stack
	mov	DX_2,ax			; Put DX to buffer
	get_f				; Get FLAG from stack
	mov	FLAG_2,ax		; Put FLAG to buffer

	add	count,BLOCK * 2		; Increase count with block size

	mov	ax,AX_1			; Get function call number
	cmp	ah,02H			; Is it a read request ?
	jne	r2_1			; If not, then branch
	call	save_sector		; If yes, then save sector data

r2_1:
	call	save_reg		; Write reg_buf to file 'REGISTER.DAT'

record2_done:

	popall				; Restore all the register values

	ret				; Return

record_reg2	endp

;------- INT 13H Subroutine ------------

;---------------------------------------
; Write sector data to file.
;---------------------------------------
; In this routine, ds:di must point to current block in reg_buf.

save_sector	proc	near

	mov	ax,351EH		; Get INT 1EH interrupt vector
	int	21H			; ES:BX will point to disk parameter
					; after this function call

	mov	cl,es:[bx+3]		; Get disk parameter N value
	mov     ax,AX_1			; Get requesting AX value
	mov	ah,al			; Let AH hold sector number
	xor	al,al			; Let AL to be reset
	shr	ax,1			; Divide AX by 2
	shl	ax,cl			; AX get total bytes to be saved
					; Each sector has 128 * 2 ^ N bytes

	mov	BYTES,ax		; Record saved bytes
	mov	ax,word ptr sec_file	; Get saving file name
	mov	NAMES,ax		; Record saving file name

	h_creat	sec_file,0		; Creat saving file
	jc      s1			; If error, then branch

	push	ds			; Save DS value
	mov	cx,BYTES		; Let CX get the number of saving bytes
	mov	bx,handle		; Let BX get the handle
	get_bx				; In requesting program, ES:BX is
	mov	dx,ax			;   pointing to the buffer that data to
	get_es				;   be loaded, so let DS:DX point to
	mov	ds,ax			;   the buffer now.
	mov	ah,40H			; Let AX get the function call number
	int	21H			; Write to file
	pop	ds			; Restore DS value

	h_close	handle			; Close file

s1:	inc	sec_file		; Let sec_file to be the next file

	ret				; Return

save_sector	endp

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