;[]-------------------------------------------------------------------------[]
;|	Program name: 3_1_3.ASM                                              |
;|	Image   file: None                                                   |
;|	Note        : Record register value whenever INT 13H accessing disk A|
;|                    List new_int13 and its subroutines only.               |
;[]-------------------------------------------------------------------------[]


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

count		dw	0
reg_buf		dw	800H dup (0FFFFH)

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

new_int13	proc	far

	sti				; Enable interrupt

	cmp	dl,00H			; Check for accessing disk A
	je	i13_2			; If true, go to save routine.

i13_1:	popf				; Restore FLAG value
	cli				; Disable interrupt
	jmp	cs:old_int13		; Jump to Old INT 13H routine

i13_2:	popf				; Restore FLAG value
	call	record_reg1		; Record register value
	call	transfer_data		; Transfer data
	call	record_reg2		; Record register value
	ret	2			; Discard FLAG value in stack
new_int13	endp

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

record_reg1	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	record1_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	[di+00],ax		; Put AX to buffer
	get_bx				; Get BX from stack
	mov	[di+02],ax		; Put BX to buffer
	get_cx				; Get CX from stack
	mov	[di+04],ax		; Put CX to buffer
	get_dx				; Get DX from stack
	mov	[di+06],ax		; Put DX to buffer

record1_done:

	popall				; Restore all the register value

	ret				; Return

record_reg1	endp

;---------------------------------------
; Transfer data by using Old INT 13H
;---------------------------------------

transfer_data	proc	near

	cli
	pushf
	call	cs:old_int13		; Call old_int13 to access disk
	sti

	ret

transfer_data	endp

;---------------------------------------
; 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	[di+08],ax			; Put AX to buffer
	get_bx				; Get BX from stack
	mov	[di+10],ax			; Put BX to buffer
	get_cx				; Get CX from stack
	mov	[di+12],ax			; Put CX to buffer
	get_dx				; Get DX from stack
	mov	[di+14],ax			; Put DX to buffer
	get_f				; Get FLAG from stack
	mov	[di+16],ax		; Put FLAG to buffer

	add	count,18		; Increase count with block size

record2_done:

	popall				; Restore all the register values

	ret				; Return

record_reg2	endp

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