.data x: .space 1 # user input, >= 0 y: .space 1 # user input, no value restriction prod: .space 1 const1: .word 1 index: .space 1 scr: .space 1 .text mult: subz prod,prod,next # default result 0 subz scr,scr,next subz scr,x,zero_mult # if (x == 0) return 0; # else scr = -x subz index,index,next subz index,scr,next # index = x; subz scr,scr,next top: # loop invariant: (index * y) - scr = x * y # hold here... subz scr,y,next # scr += -y subz index,const1,done # if (--index == 0) goto done # The loop invariant holds after this instruction too # (regardless of whether the branch is taken or not) subz prod,prod,top # at loop termination, index == 0, so loop invariant # tells us (0 * y) - scr = x * y, so scr = -x*y done: # prod is zero, both at loop termination and from x == 0 test subz prod,scr,next zero_mult: