	org 	8000h
        jmp     8100h
	
	$MOD51GB

        org     8003h
        jmp     INT0_ISR

        org     8013h
        jmp     INT1_ISR

        org     8053h
        jmp     INT2_ISR


        org     8100h
main:   setb    IT0     ;set INT0 to trigger on falling edge
        setb    IT1     ;set INT1 to trigger on falling edge
        mov     A,#00   ;clr INT2 to trigger on falling edge
        mov     EXICON,A
        setb    P5.2
        setb    EX0     ;enable external interrupt 0
        setb    EX1     ;enable external interrupt 1
        mov     A,#04h  ;load Acc with value
        mov     IEA,A   ;to set the EX2 external interrupt 2
        setb    EA      ;enable interrupts
wait:   jmp     wait

	
INT0_ISR:
        call    print
        db      '\n \n  INT0 \n',0,
	reti

INT1_ISR:
        call    print
        db      '\n \n  INT1 \n',0,
	reti

INT2_ISR:
        call    print
        db      '\n \n  INT2 \n',0,
        reti

SEND_BYTE:
       	 JNB	TI, SEND_BYTE		; WAIT FOR THE TI FLAG TO SET
	CLR     	TI		                  ; CLEAR THE TI FLAG ONCE SENT
        	MOV   	SBUF,A 			; SEND THE CONTENTS OF REGISTER A
	RET           			; RETURN
;*****************************************************************************



PRINT:
        PUSH    ACC      ; push the A and DPTR registers so their contents  
        PUSH    DPH      ; don't get lost in case they are being used
        PUSH    DPL      ; before calling this function. 

; move the stack pointer down low in order to pop the first string's byte
; address
        MOV     A,SP     
        ADD     A,#0FDH
        MOV     SP,A
        POP     DPH
        POP     DPL

; send (PRINT) the string back to the PC computer
READ_TX:
        CLR     A
        MOVC    A,@A+DPTR
        INC     DPTR             

        CJNE    A,#'\',SND

        CLR     A
        MOVC    A,@A+DPTR
        INC     DPTR             

        CJNE    A,#'n',SND

        MOV     A,#0DH
        ACALL   SEND_BYTE
        MOV     A,#0AH

SND:    ACALL   SEND_BYTE
        CJNE    A,#0,READ_TX

; place the new return addres in stack
        PUSH    DPL
        PUSH    DPH

; move the stack pointer up high in order to pop the A and DPTR registers 
        MOV     A,SP
        ADD     A,#3
        MOV     SP,A
        POP     DPL
        POP     DPH
        POP     ACC
        RET



	end
