63 lines
1.1 KiB
Plaintext
63 lines
1.1 KiB
Plaintext
DEFINE Bit="BYTE"
|
|
|
|
TYPE FourBit=[Bit b0,b1,b2,b3]
|
|
|
|
Bit FUNC Not(Bit a)
|
|
RETURN (1-a)
|
|
|
|
Bit FUNC MyXor(Bit a,b)
|
|
RETURN ((Not(a) AND b) OR (a AND Not(b)))
|
|
|
|
Bit FUNC HalfAdder(Bit a,b Bit POINTER c)
|
|
c^=a AND b
|
|
RETURN (MyXor(a,b))
|
|
|
|
Bit FUNC FullAdder(Bit a,b,c0 Bit POINTER c)
|
|
Bit s1,c1,s2,c2
|
|
|
|
s1=HalfAdder(a,c0,@c1)
|
|
s2=HalfAdder(b,s1,@c2)
|
|
|
|
c^=c1 OR c2
|
|
RETURN (s2)
|
|
|
|
PROC FourBitAdder(FourBit POINTER a,b,s Bit POINTER c)
|
|
Bit c1,c2,c3
|
|
|
|
s.b3=FullAdder(a.b3,b.b3,0,@c3)
|
|
s.b2=FullAdder(a.b2,b.b2,c3,@c2)
|
|
s.b1=FullAdder(a.b1,b.b1,c2,@c1)
|
|
s.b0=FullAdder(a.b0,b.b0,c1,c)
|
|
RETURN
|
|
|
|
PROC InitFourBit(BYTE a FourBit POINTER res)
|
|
res.b3=a&1 a==RSH 1
|
|
res.b2=a&1 a==RSH 1
|
|
res.b1=a&1 a==RSH 1
|
|
res.b0=a&1
|
|
RETURN
|
|
|
|
PROC PrintFourBit(FourBit POINTER a)
|
|
PrintB(a.b0) PrintB(a.b1)
|
|
PrintB(a.b2) PrintB(a.b3)
|
|
RETURN
|
|
|
|
PROC Main()
|
|
FourBit a,b,s
|
|
Bit c
|
|
BYTE i,v
|
|
|
|
FOR i=1 TO 20
|
|
DO
|
|
v=Rand(16) InitFourBit(v,a)
|
|
v=Rand(16) InitFourBit(v,b)
|
|
|
|
FourBitAdder(a,b,s,@c)
|
|
|
|
PrintFourBit(a) Print(" + ")
|
|
PrintFourBit(b) Print(" = ")
|
|
PrintFourBit(s) Print(" Carry=")
|
|
PrintBE(c)
|
|
OD
|
|
RETURN
|