; ; Program to compute the dot product of two vectors, which are inteleaved ; in memory. ; ; This uses the negMult subroute from assignment 2. The vector access code ; is just a simple variant of what was shown in class: we have two move ; instruction blocks, and we modify the instructions after we use them so ; that the negated copies will read from memory two locations further down. ; The move blocks just grab an element each from the input vectors and ; copies them to the two input parameters of the negMult routine. ; 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 ; copy veca[i] to x subge x,x,next A0: subge tmp,veca,next subge x,tmp,next subge tmp,tmp,next ; copy vecb[i] to y subge y,y,next A1: subge tmp,vecb,next subge y,tmp,next subge A0,nextElt,next ; bump the instructions subge A1,nextElt,next ; to get the next pair of elts .data nextElt: .word neg(triple(0,2,0)) .text call negMult, negMultRet 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 goto 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