NB. Models of Division Algorithms discussed in Section 3.5 of NB. Computer Organization & Design by Patterson and Hennessy NB. Binary To Decimal btd =: monad define digits =. # y. if. 1 = 1 take y. do. ((digits copy 2) base x: y. ) - 2^ x: digits else. (digits copy 2) base x: y. end. ) NB. Non-circuit based 64-bit ALU alu_64 =: monad define NB. a and b are each 64-bit summands NB. the result is a 64-bit sum ('a' ; 'b' ; 'sub') =. y. if. sub do. (64#2) rep (base x: a) - base x: b else. (64#2) rep (base x: a) + base x: b end. ) NB. Some alu_64 tests. a =: (63#1),0 b=: (62#0), 1 0 alu_64 a;b;0 alu_64 a;b;1 c=:64#1 alu_64 b;c;1 NB. Note that decimal args may be used with alu_64 alu_64 3 4 0 alu_64 100 ; 28 ; 0 base alu_64 100 ; 28 ; 0 NB. Model of Divider Hardware, Page 184 of CO&D. NB. Assume 32-bit Quotient and Divisor and 64-bit Dividend NB. and Remainder NB. div1 uses alu_64. div1 =: monad define ('dividend' ; 'divisor') =. y. divisor =. divisor, 32#0 quotient =. 32#0 remainder=: dividend count=. 33 while. 0 not_equal count do. remainder =: alu_64 remainder ; divisor ; 1 control =. first remainder if. control do. remainder =: alu_64 remainder ; divisor ; 0 quotient =. (1 drop quotient) , 0 else. quotient =. (1 drop quotient) , 1 end. divisor =. 0 , _1 drop divisor count =. <: count end. quotient ) NB. a test quotient of 3%2 div1 ((62#0), 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of 7%2 div1 ((61#0), 1 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of _6%2 div1 ((61#0),1 1 0) ; (30#0) , 1 0 remainder NB. A tracing Model of Divider Hardware, Page 187 of CO&D. NB. Assume 32-bit Quotient and Divisor and 64-bit Dividend NB. and Remainder NB. traced_div1 uses alu_64. traced_div1 =: monad define ('dividend' ; 'divisor') =. y. display 'init-divisor';divisor =. divisor, 32#0 display 'init-quotient';quotient =. 32#0 display 'init-remainder';remainder=: dividend count=. 33 while. 0 not_equal count do. display 'sub-alu-remainder';remainder =: alu_64 remainder ; divisor ; 1 control =. first remainder if. control do. display 'add-alu-remainder';remainder =: alu_64 remainder ; divisor ; 0 display 'add-quotient';quotient =. (1 drop quotient) , 0 else. display'sub-quotient';quotient =. (1 drop quotient) , 1 end. display'divsor';divisor =. 0 , _1 drop divisor count =. <: count end. quotient ) NB. a test quotient of 3%2 traced_div1 ((62#0), 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of 7%2 traced_div1 ((61#0), 1 1 1) ; (30#0) , 1 0 remainder NB. Non-circuit based 32-bit ALU alu_32 =: monad define NB. a and b are each 32-bit summands NB. the result is a 32-bit sum NB. sub = 0 for addition NB. sub = 1 for subtraction ('a' ; 'b' ; 'sub') =. y. if. sub do. (32#2) rep (base x: a) - base x: b else. (32#2) rep (base x: a) + base x: b end. ) NB. Some alu_32 tests. a =: (31#1),0 b=: (30#0), 1 0 alu_32 a;b;0 alu_32 a;b;1 c=:32#1 alu_32 b;c;1 NB. Note that decimal args may be used with alu_32 alu_32 3 4 0 alu_32 100 ; 28 ; 0 base alu_32 100 ; 28 ; 0 NB. Model of Divider Hardware, Page 269 of CO&D (2ed). NB. Assume 32-bit Quotient, Divisor and Dividend and64-bit NB. Remainder NB. div2 uses alu_32. div2 =: monad define ('dividend' ; 'divisor') =. y. quotient =. 32#0 remainder=: (32#0) , dividend count=. 32 remainder =: (1 drop remainder) , 0 while. 0 not_equal count do. remainder =: (alu_32 (32 take remainder) ; divisor ; 1) , 32 drop remainder if. control =. first remainder do. remainder =: (alu_32 (32 take remainder) ; divisor ; 0) , 32 drop remainder quotient =. (1 drop quotient) , 0 else. quotient =. (1 drop quotient) , 1 end. remainder =: (1 drop remainder) , 0 count =. <: count end. remainder =: (0 , _1 drop 32 take remainder) , 32 drop remainder quotient ) NB. a test quotient of 3%2 div2 ((30#0), 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of 7%2 div2 ((29#0), 1 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of _6%2 div2 ((29#0),1 1 0) ; (30#0) , 1 0 remainder NB. A Tracing Model of Divider Hardware, Page 269 of CO&D (2ed). NB. Assume 32-bit Quotient, Divisor and Dividend and64-bit NB. Remainder NB. div2 uses alu_32. traced_div2 =: monad define ('dividend' ; 'divisor') =. y. display'init-quo';quotient =. 32#0 display'init-rem';remainder=: (32#0) , dividend count=. 32 display'init-rem-shifted';remainder =: (1 drop remainder) , 0 while. 0 not_equal count do. display'sub-rem';remainder =: (alu_32 (32 take remainder) ; divisor ; 1) , 32 drop remainder if. control =. first remainder do. display'add-rem';remainder =: (alu_32 (32 take remainder) ; divisor ; 0) , 32 drop remainder display'add-quo';quotient =. (1 drop quotient) , 0 else. display'sub-quo';quotient =. (1 drop quotient) , 1 end. display'shift-rem';remainder =: (1 drop remainder) , 0 count =. <: count end. display'adjust-rem';remainder =: (0 , _1 drop 32 take remainder) , 32 drop remainder quotient ) NB. a test quotient of 3%2 traced_div2 ((30#0), 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of 7%2 traced_div2 ((29#0), 1 1 1) ; (30#0) , 1 0 remainder NB. a test quotient of _6%2 traced_div2 ((29#0),1 1 0) ; (30#0) , 1 0 remainder