; ADD/SUBTRACT n1, n2
; Add two 16-bit numbers to produce a 16-bit result, permitting overflow. 
; Add the two's complement of one 16-bit number to another 16-bit number
;  to perform subtraction, permitting underflow. 

; Addition: n1 = n1 + n2
; Subtraction: n1 = n1 - n2

; Device data and reset vector
        device  pic16c55,xt_osc,wdt_off,protect_off
        reset   start

        org     8
n1H     ds      1       ; MSB of n1. 
n1L     ds      1       ; LSB of n1. 
n2H     ds      1       ; MSB of n2.
n2L     ds      1       ; LSB of n2.

        org     0

start   mov     !rb,#0  	; Outputs for LEDs to 
        mov     !rc,#0  	; display 16-bit result.
        mov     n1H,#0C8h       ; Problem: C836h-2575h
        mov     n1L,#36h
        mov     n2H,#25h
        mov     n2L,#75h
        call    Sub16   	; n1 = n1 - n2
        mov     rb,n1L  	; Show result in binary on 
        mov     rc,n1H  	; LEDs connected to ports.
        jmp     $       	; Endless loop

Sub16   NOT	n2H     	; Take two's complement
	NOT	n2L     	; of n2. If zero, n2L
	snz			; overflowed, so carry 
	inc	n2H		; into n2H.
Add16	ADD	n1L,n2L		; Add the LSBs. If carry,
	snc			; increment MSB of sum. 
	inc	n1H
	ADD	n1H,n2H 	; Add the MSBs  
	ret