Following are J models of the multiplier discussed in Section 4.6 of Patterson and Hennessy [Patt 1998]. First, an architectural model of a 32-bit ALU.
alu_32 =: monad define NB. a and b are each 32-bit summands NB. the result is a 32-bit sum ('a' ; 'b') =. y. (32#2) #: (#. x: a) + #. x: b )
Next, the multiplier:
mult3 =: monad define ('multiplicand' ; 'multiplier') =. y. product=. (32#0) , multiplier count=.32 while. 0 ~: count do. control =. (_1&{) product if. control do. product =. (alu_32 (32 {. product) ; multiplicand) , 32 }. product end. product =. 0 , _1 }. product count =. <: count end. product )
Finally, we use the multiplier to form the product 3 * 2.
mult3 ((30#0), 1 1); (30#0),1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0