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 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 NB. Model of Divider Hardware, Page 271 of CO&D (2ed). NB. Assume 32-bit Quotient (maintained in right half NB. of remainder register), Divisor and Dividend and64-bit NB. Remainder NB. div3 uses alu_32. div3 =: monad define ('dividend' ; 'divisor') =. y. 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 remainder =: (1 drop remainder) , 0 else. remainder =: (1 drop remainder) , 1 end. count =. <: count end. remainder =: (0 , _1 drop 32 take remainder) , 32 drop remainder ) NB. a test quotient of 3%2 div3 ((30#0), 1 1) ; (30#0) , 1 0 NB. a test quotient of 7%2 div3 ((29#0), 1 1 1) ; (30#0) , 1 0 NB. a test quotient of _6%2 div3 ((29#0),1 1 0) ; (30#0) , 1 0 NB. A Tracing Model of Divider Hardware, Page 271 of CO&D (2ed). NB. Assume 32-bit Quotient (maintained in right half NB. of remainder register), Divisor and Dividend and64-bit NB. Remainder NB. div3 uses alu_32. traced_div3 =: monad define ('dividend' ; 'divisor') =. y. display'rem-init';remainder=: (32#0) , dividend count=. 32 display'rem-init-shift';remainder =: (1 drop remainder) , 0 while. 0 not_equal count do. display'rem-sub';remainder =: (alu_32 (32 take remainder) ; divisor ; 1) , 32 drop remainder if. control =. first remainder do. display'less';remainder =: (alu_32 (32 take remainder) ; divisor ; 0) , 32 drop remainder display'less-shift';remainder =: (1 drop remainder) , 0 else. display'more-shift';remainder =: (1 drop remainder) , 1 end. count =. <: count end. remainder =: (0 , _1 drop 32 take remainder) , 32 drop remainder ) NB. a test quotient of 3%2 traced_div3 ((30#0), 1 1) ; (30#0) , 1 0 NB. a test quotient of 7%2 traced_div3 ((29#0), 1 1 1) ; (30#0) , 1 0 NB. a test quotient of _6%2 traced_div3 ((29#0),1 1 0) ; (30#0) , 1 0