;
; Program to compute the dot product of two vectors, which are inteleaved
; in memory.
;

out:		.equ	0
dim:		.equ	1
veca:		.equ	2
vecb:		.equ	3

		.text
main:		subge i,i,next
		subge out,out,next
loop:		subge tmp,tmp,next	; i < dim, so i-dim < 0
					; or not i-dim >= 0
		subge tmp2,tmp2,next
		subge tmp2,i,next
		subge tmp,tmp2,next
		subge tmp,dim,done

		subge tmp,tmp,next
		subge x,x,next
A0:		subge tmp,veca,next
		subge x,tmp,next

		subge tmp,tmp,next
		subge y,y,next
A1:		subge tmp,vecb,next
		subge y,tmp,next

		subge A0,nextElt,next
		subge A1,nextElt,next
		.data
nextElt:	.word neg(triple(0,2,0))
		.text

		subge negMultRet,negMultret,next
		subge negMultRet,L0,next
		subge tmp,tmp,negMult
L1:
		.data
L0:		.word neg(triple(tmp,tmp,L1))
		.text

		subge out,negProd,next
		subge i,neg1,loop	; assume never overflow

done:		subge tmp,tmp,done	; done w/ loop, 
		.word 0			; PATCH place holder
		.word 0

		.data
i:		.word 0
tmp:		.word 0
tmp2:		.word 0
negsum:		.word 0			; unused PATCH / place holder


		.text
negMult:	subge xx,xx,next
		subge xx,x,next			; xx = -x
		subge negProd,negProd,next	; negProd = 0
		;
		; loop invariant:  -x y = negProd + xx * y
		;
multLoop:	subge xx,zero,negMultRet	; xx = xx + 0
						;    = -x + 0 >= 0
						;        or 0 >= x.
		subge negProd,y,next		; negProd = negProd - y
		subge xx,neg1,next		; xx = xx + 1
		subge tmp,tmp,multLoop
		;
		; exit when xx = 0, so invariant tells us that
		; -x y = negProd + 0 * y = negProd
		; which is the desired return value
		;
negMultRet:	.word 0,0,0			; negProd = -(x * y)

		.data
zero:		.word 0
neg1:		.word neg(1)
negProd:	.word 0
x:		.word 0
y:		.word 0
xx:		.word 0
