Four bit adder: Difference between revisions

add RPL
(add RPL)
Line 5,758:
Sum...: 1 00110011001100110011001100110010
 
</pre>
 
=={{header|RPL}}==
{{trans|C}}
{{works with|Halcyon Calc|4.2.9}}
To make a long piece of code short, on a 4-bit machine equipped with an interpreter capable to handle 64-bit integers, we reduce their size to 1 bit so that we can simulate the working of the 4-bit CPU at a very high level of software abstraction.
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ → nibble
≪ { } 1 nibble SIZE '''FOR''' j
nibble j DUP SUB "1" == R→B + '''NEXT'''
≫ ≫ ‘-<span style="color:blue">→LOAD</span>’ STO
≪ → nibble carry
≪ "" 1 nibble SIZE '''FOR''' bit
nibble bit GET B→R →STR + '''NEXT'''
"→" + carry B→R →STR +
≫ ≫ ‘<span style="color:blue">→DISP</span>’ STO
≪ → a b
≪ a b NOT AND b a NOT AND OR
≫ ≫ ‘<span style="color:blue">→XOR</span>’ STO
≪ → a b
≪ a b <span style="color:blue">→XOR</span> a b AND
≫ ≫ ‘<span style="color:blue">→HALF</span>’ STO
≪ → a b c
≪ c a <span style="color:blue">→HALF</span> SWAP b <span style="color:blue">→HALF</span> ROT OR
≫ ≫ ‘<span style="color:blue">→FULL</span>’ STO
1 STWS
<span style="color:blue">→LOAD</span> SWAP <span style="color:blue">→LOAD</span> → n2 n1
≪ { } #0h 4 1 '''FOR''' bit
n1 bit GET n2 bit GET
ROT <span style="color:blue">→FULL</span>
SWAP ROT + SWAP
-1 '''STEP''' <span style="color:blue">→DISP</span>
≫ ≫ ‘<span style="color:blue">→ADD</span>’ STO
|
<span style="color:blue">→LOAD</span> ''( "nibble" → { #bits } ) ''
turn the input format into a list of bits,
easier to handle
<span style="color:blue">→DISP</span> ''( { #bits } c → "nibble→c" ) ''
convert the bit list to a string
<span style="color:blue">→XOR</span> ''( a b → xor(a,b) ) ''
= (not a and b) or (not b and a)
<span style="color:blue">→HALF</span> ''( a b → a+b carry ) ''
s = (a xor b), c = (a and b)
<span style="color:blue">→FULL</span> ''( a b c → a+b+c carry ) ''
<span style="color:blue">→ADD</span> ''( "nibble" "nibble" → "nibble→c" ) ''
set unsigned integer size to 1 bit
convert strings into lists
from lower to higher bit
read bits
add them
store result, keep carry
show result
|}
"0101" "0011" <span style="color:blue">→ADD</span>
"1111" "1111" <span style="color:blue">→ADD</span>
{{out}}
<pre>
2: "1000→0"
1: "1110→1"
</pre>
 
1,150

edits