TITLE  ZERODIV.ASM--Divide by Zero Handler
;To assemble,link and conver into a com file:
;C>MASM ZERODIV
;C>LINK ZERODIV
;C>EXE2BIN ZERODIV.EXE ZERODIV.COM
;C>DEL ZERODIV.EXE
cseg segment para public 'code'
	org 100h
	assume cs:cseg,es:cseg,ss:cseg
init proc near
	mov	dx,offset zdiv	;reset interrupt 0 vector
	mov	ax,seg zdiv
	mov	ds,ax
	mov	al,0
	mov	ah,25h		
	int	21h

	mov	dx,offset signon;print message
	mov	ah,9
	int	21h

	mov	ax,20		;assume the case
	mov	dl,0		; of divide by zero
	div	dl

	mov	ah,02		;if entered 'c',
	mov	dl,'#'		; return the main
	int	21h		;  program

	mov	ah,31h		;exit & stay residente
	mov	al,0		; return code = 0
	mov	dx,((pgm_len+15)/16)+10h
	int	21h		;DX=paragraphs of 
				; memory to reserve

init endp
;------------------------------------------------------
zdiv proc far		;this is the interrupt handler
	push	ax		;save general registers
	push	bx
	push	cx
	push	dx
	push	si
	push	di
	push	bp
	push	ds
	push	es
	sti
zdiv0:	mov	ax,cs		;print warning 
	mov	ds,ax		;
	mov	dx,offset warn
	mov	ah,9
	int	21h

zdiv1:	mov	ah,1		;read keyboard
	int	21h
	cmp	al,'c'		;is it c or q ?
	je	zdiv3		;jump,it's a c
	cmp	al,'q'
	je	zdiv2		;jump, it's a q

	mov	dx,offset bad	;illegal entry
	mov	ah,09		;send a beep
	int	21h
	jmp	zdiv0		;try again
zdiv2:	mov	ax,4cffh	;exit 
	int	21h

zdiv3:	mov	dx,offset crlf	;send CR and LF
	mov	ah,09
	int	21h

	cli
	pop	es
	pop	ds
	pop	bp
	pop	di
	pop	si
	pop	dx
	pop	cx
	pop	bx
	pop	ax

	iret
zdiv endp
;-----------------------------------------------
signon  db 0dh,0ah,'*Divide by zero Interrupt '
	db 'Handler installed.*'
	db 0dh,0ah,'$'
warn	db 0dh,0ah,'Divide by zero detected:'
	db 0dh,0ah,'Continue or Quit (c/q)?$'

bad	db 07h,'$'
crlf	db 0dh,0ah,'$'
pgm_len equ $-init
;-----------------------------------------------
cseg    ends
	end init
	