MULT: MOV R6, R0
MOV R1, -(R6)
MOV R2, -(R6)
INC R0
MOV (R0)+, R1
MOV (R0)+, R2
CLR R0
TST R1
BEQ RET
LOOP: ADD R2, R0
DEC R1
BGT LOOP
RET: MOV (R6)+, R2
MOV (R6)+, R1
RTS
version including a main program and in a separate file for
easy testing
b)
MULT2: MOV R6, R0
MOV R1, -(R6)
MOV R2, -(R6)
MOV R3, -(R6)
INC R0
MOV (R0)+, R1
MOV (R0)+, R2
CLR R3 ; stores how many operands were negative
; arithmetically-negate R1 if necessary, and store count in R3
TST R1
BGE CONT1
NEG R1
INC R3
; arithmetically-negate R2 if necessary, and adjust count in R3
CONT1: TST R2
BGE CONT2
NEG R2
INC R3
CONT2: ; call other MULT subroutine
MOV R1, -(R6)
MOV R2, -(R6)
JSR MULT
INC R6
INC R6
; arithmetically-negate result if appropriate
MOV# 1, R1
BIT R1, R3
BEQ RET2
NEG R0
RET2: MOV (R6)+, R3
MOV (R6)+, R2
MOV (R6)+, R1
RTS
version including a main program and the part 'a' subroutine and in a separate file for
easy testing, and also using VELMA instructions instead of NEG
c)
MOV X, R1 MOV R2, -(R6) ; y MOV R3, -(R6) ; z JSR MULT2 INC R6 INC R6 SUB R0, R1 MOV R4, -(R6) ; a MOV R5, -(R6) ; b JSR MULT2 INC R6 INC R6 ADD R0, R1 HALTversion including the subroutines and in a separate file for easy testing, and also using VELMA instructions instead of NEG
d)
First of all,
terminology:
one parameter is the addend and the other is the repeat count.
In the
above, it's the second parameter which is the repeat count, but if you did it
the other way around, the following comments need to be adjusted
appropriately.
So.
Consider the case where
the addend is the most negative integer and the repeat count is 0, 1, or
-1 (i.e. this is three example parameter pairs; you could list any one of
these, but I think that these are the only possible examples).
Then the NEG statement in the part (b) code overflows. And it calls the part
(a) code with an addend of minint (the incorrect (overflowed) negation of
minint), with a repeat count of 0 or 1. The part (a) code either returns
zero immediately (if the repeat count is 0), or adds 0 plus minint (if the
repeat count is 1), but in either case it does not overflow. (Then, in two
of these three cases another overflow occurs in the part (b) subroutine when
it tries to NEG the return value from part (a).)
Part (d) was worth only 2 out of the 16 marks for question 7.