How to resolve the algorithm Four bit adder step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Four bit adder step by step in the PL/I programming language

Table of Contents

Problem Statement

"Simulate" a four-bit adder. This design can be realized using four 1-bit full adders. Each of these 1-bit full adders can be built with two half adders and an   or   gate. ; Finally a half adder can be made using an   xor   gate and an   and   gate. The   xor   gate can be made using two   nots,   two   ands   and one   or. Not,   or   and   and,   the only allowed "gates" for the task, can be "imitated" by using the bitwise operators of your language. If there is not a bit type in your language, to be sure that the   not   does not "invert" all the other bits of the basic type   (e.g. a byte)   we are not interested in,   you can use an extra   nand   (and   then   not)   with the constant   1   on one input. Instead of optimizing and reducing the number of gates used for the final 4-bit adder,   build it in the most straightforward way,   connecting the other "constructive blocks",   in turn made of "simpler" and "smaller" ones.

Solutions should try to be as descriptive as possible, making it as easy as possible to identify "connections" between higher-order "blocks". It is not mandatory to replicate the syntax of higher-order blocks in the atomic "gate" blocks, i.e. basic "gate" operations can be performed as usual bitwise operations, or they can be "wrapped" in a block in order to expose the same syntax of higher-order blocks, at implementers' choice. To test the implementation, show the sum of two four-bit numbers (in binary).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Four bit adder step by step in the PL/I programming language

Source code in the pl/i programming language

/* 4-BIT ADDER */

TEST: PROCEDURE OPTIONS (MAIN);
   DECLARE CARRY_IN BIT (1) STATIC INITIAL ('0'B) ALIGNED;
   declare (m, n, sum)(4) bit(1) aligned;
   declare i fixed binary;

   get edit (m, n) (b(1));
   put edit (m, ' + ', n, ' = ') (4 b, a);

   do i = 4 to 1 by -1;
      call full_adder ((carry_in), m(i), n(i), sum(i), carry_in);
   end;
   put edit (sum) (b);

HALF_ADDER: PROCEDURE (IN1, IN2, SUM, CARRY);
   DECLARE (IN1, IN2, SUM, CARRY) BIT (1) ALIGNED;

   SUM = ( ^IN1 & IN2) | ( IN1 & ^IN2);
         /* Exclusive OR using only AND, NOT, OR. */
   CARRY = IN1 & IN2;

END HALF_ADDER;

FULL_ADDER: PROCEDURE (CARRY_IN, IN1, IN2, SUM, CARRY);
   DECLARE (CARRY_IN, IN1, IN2, SUM, CARRY) BIT (1) ALIGNED;
   DECLARE (SUM2, CARRY2) BIT (1) ALIGNED;

   CALL HALF_ADDER (CARRY_IN, IN1, SUM, CARRY);
   CALL HALF_ADDER (SUM, IN2, SUM2, CARRY2);
   SUM = SUM2;
   CARRY = CARRY | CARRY2;
END FULL_ADDER;

END TEST;

  

You may also check:How to resolve the algorithm Multifactorial step by step in the PL/I programming language
You may also check:How to resolve the algorithm Amb step by step in the PL/I programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the PL/I programming language
You may also check:How to resolve the algorithm Range expansion step by step in the PL/I programming language
You may also check:How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the PL/I programming language