Thread: simple ALU

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Question simple ALU

    I am trying to write a simple ALU. I think I am on the right track, but don't really know how I can improve on it. I also dont fully understand what to do with the carryin value? I found this site on google and tried a search but could not find anything related(sorry if it's already been covered). Any tips?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string> 
    
    using namespace std;
    
    int main() {
    /*4-bit ALU*/
    		char A[5] = "0101";
    		char B[5] = "0011";
    		char zero[2] = "0";
    		int a[5];
    		int b[5];
    		char Cin[2] = "0";
    		int Result[5];
    		
    		/*Makes an int out of input A*/
    		for(int i=0; i<4; i++){
    			if(A[i] == zero[0]){
    				a[i] = 0;
    			}
    			else{
    				a[i] = 1;
    			}
    		}
    		/*Makes an int out of input B*/
    		for(int i=0; i<4; i++){
    			if(B[i] == zero[0]){
    				b[i] = 0;
    			}
    			else{
    				b[i] = 1;
    			}
    		}
    		cout << "4 bit ALU" << endl;
    		cout << "--------------------";
    		cout << "\nThe first input is:  ";
    		for(int i=0; i<4; i++){
    			cout << a[i];
    			}
    		cout << "\nThe second input is: ";
    		for(int i=0; i<4; i++){
    			cout << b[i];
    			}
    		cout << "\nThe carry-in is:        ";
    		for(int i=0;i<1;i++){
    			cout << Cin[i];
    		}
    		cout << "\n--------------------";
    		/*AND*/	
    		for(int i=0; i<4; i++){
    			Result[i] = a[i]&b[i];
    			}
    		cout << "\nThe Result of AND is: ";
    		for(int i=0;i<4;i++){
    			cout << Result[i];
    		}
    		cout << "\n";
    		/*OR*/
    		for(int i=0; i<4; i++){
    			Result[i] = a[i]|b[i];
    			}
    		cout << "The Result of OR is:  ";
    		for(int i=0;i<4;i++){
    			cout << Result[i];
    		}
    		cout << "\n";
    		/*XOR*/
    		for(int i=0; i<4; i++){
    			Result[i] = a[i]^b[i];
    			}
    		cout << "The Result of XOR is: ";
    		for(int i=0;i<4;i++){
    			cout << Result[i];
    		}
    		
    		cout << "\n";
    /*32 bit ALU*/
    		char A2[33] = "000011111010011001011001";
    		char B2[33] = "001100101101111000100101";
    		char zero2[2] = "0";
    		int a2[33];
    		int b2[33];
    		int bb[33];
    		char Cin2[2] = "0";
    		int Result2[33];
    		
    		/*Makes an int out of input A*/
    		for(int i=0; i<32; i++){
    			if(A2[i] == zero2[0]){
    				a2[i] = 0;
    			}
    			else{
    				a2[i] = 1;
    			}
    		}
    		/*Makes an int out of input B*/
    		for(int i=0; i<32; i++){
    			if(B2[i] == zero2[0]){
    				b2[i] = 0;
    			}
    			else{
    				b2[i] = 1;
    			}
    		}
    		/*Makes an int out of input B INVERSE using twos compliment*/
    		for(int i=0; i<32; i++){
    			if(B2[i] == zero2[0]){
    				bb[i] = 1;
    			}
    			else{
    				bb[i] = 0;
    			}
    		}
    		bb[31]++;
    		cout << "32 bit ALU" << endl;
    		cout << "--------------------";
    		cout << "\nThe first input is:                 ";
    		for(int i=0; i<32; i++){
    			cout << a2[i];
    			}
    		cout << "\nThe second input is:                ";
    		for(int i=0; i<32; i++){
    			cout << b2[i];
    			}
    		cout << "\nThe INVERSE of the second input is: ";
    		for(int i=0; i<32; i++){
    			cout << bb[i];
    			}
    		cout << "\nThe carry-in is:                    ";
    		for(int i=0;i<1;i++){
    			cout << Cin2[i];
    		}
    		cout << "\n--------------------";
    		/*AND*/	
    		for(int i=0; i<32; i++){
    			Result2[i] = a2[i]&b2[i];
    			}
    		cout << "\nThe Result of AND is: ";
    		for(int i=0;i<32;i++){
    			cout << Result2[i];
    		}
    		cout << "\n";
    		/*OR*/
    		for(int i=0; i<32; i++){
    			Result2[i] = a2[i]|b2[i];
    			}
    		cout << "The Result of OR is:  ";
    		for(int i=0;i<32;i++){
    			cout << Result2[i];
    		}
    		cout << "\n";
    		/*XOR*/
    		for(int i=0; i<32; i++){
    			Result2[i] = a2[i]^b2[i];
    			}
    		cout << "The Result of XOR is: ";
    		for(int i=0;i<32;i++){
    			cout << Result2[i];
    		}
    		cout << "\n";
    		/*Sub*/
    		for(int i=0; i<32; i++){
    			Result2[i] = a2[i]|bb[i];
    			}
    		cout << "The Result of Sub is: ";
    		for(int i=0;i<32;i++){
    			cout << Result2[i];
    		}
    		cout << "\n";
    	return 0;
    }
    Last edited by stewart; 04-20-2010 at 09:51 PM. Reason: Sorry about the title, I tried to put simple ALU but was careless!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks promising.

    Make some utility functions to clean main() up.
    Eg
    Code:
    void convertToInt ( int *result, const char *input, int len ) {
    		/*Makes an int out of input */
    		for(int i=0; i<len; i++){
    			if(input[i] == '0' ){
    				result[i] = 0;
    			}
    			else{
    				result[i] = 1;
    			}
    		}
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    yeah I know my main is messy, thanks for the tip :-) haha

    do you have anytips about the logic behind carryin and carryout?
    from my understanding 4bit doesnt need carryout but 32 does so that it can handle overflow?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Eg.
    01
    11

    For addition, you start with carry = 0

    Then take the two right-most bits
    r = 1 + 1 + carry

    In a single bit, r = 0 and the carry is now 1

    The next bits would be
    r = 0 + 1 + carry
    Again, r = 0 and carry is again 1

    So our complete result (in 2 bits) is 00 (and carry = 1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM