My Youtube Channel

Please Subscribe

Flag of Nepal

Built in OpenGL

Word Cloud in Python

With masked image

Showing posts with label MATLAB Programs - COA. Show all posts
Showing posts with label MATLAB Programs - COA. Show all posts

Saturday, January 20, 2018

Computer Organisation & Architecture

PROGRAM IN MATLAB FOR DIVISION OF TWO UNSIGNED INTEGER BINARY NUMBERS
(N-BIT NUMBERS)
To implement non-restoring division algorithm in digital computer


function lab5_coa
n=input('enter n:');
a=input('enter a:');
b=input('enter b:');
c=b;
q=a;
carry=0;
for i=1:1:n
    a(1,i)=0;
end
for i=n:-1:1
    if(c(1,i)==1)
        c(1,i)=0;
    
    else
        c(1,i)=1;
    end
end
for i=1:1:n-1
    x(1,i)=0;
end
x(1,n)=1;
for i=n:-1:1
sum=xor(xor(x(1,i),c(1,i)),carry);
carry=or(and(x(1,i),c(1,i)),and(xor(x(1,i),c(1,i)),carry));
r(1,i)=sum;
end
c=r;

    for i=1:1:n-1
        a(1,i)=a(1,i+1);
    end
    a(1,n)=q(1,1);
    for i=1:1:n-1
        q(1,i)=q(1,i+1);
    end
    carry=0;
    for i=n:-1:1
        sum=xor(xor(a(1,i),c(1,i)),carry);
        carry=or(and(a(1,i),c(1,i)),and(xor(a(1,i),c(1,i)),carry));
        a(1,i)=sum;
    end
%LOOPING CONDITION    
 for count=1:1:n-1
    if (a(1,1)==1)
          q(1,n)=0;
        for i=1:1:n-1
        a(1,i)=a(1,i+1);
        end
    a(1,n)=q(1,1);
    for i=1:1:n-1
        q(1,i)=q(1,i+1);
    end
    
       carry=0;
        for i=n:-1:1
        sum=xor(xor(a(1,i),b(1,i)),carry);
        carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),carry));
        a(1,i)=sum;
        end
    else
        q(1,n)=1;
          for i=1:1:n-1
           a(1,i)=a(1,i+1);
          end
             a(1,n)=q(1,1);
            for i=1:1:n-1
             q(1,i)=q(1,i+1);
            end
             carry=0;
    for i=n:-1:1
        sum=xor(xor(a(1,i),c(1,i)),carry);
        carry=or(and(a(1,i),c(1,i)),and(xor(a(1,i),c(1,i)),carry));
        a(1,i)=sum;
    end
    end
 end
 % After completion of loop for count n-1
 if(a(1,1)==1)
       q(1,n)=0;
      carry=0;
        for i=n:-1:1
        sum=xor(xor(a(1,i),b(1,i)),carry);
        carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),carry));
        a(1,i)=sum;
        end
else
 q(1,n)=1;
 end
fprintf('Remainder =');
disp(a);
fprintf('Quotient=');
disp(q);
end

Computer Organisation & Architecture

PROGRAM IN MATLAB FOR DIVISION OF TWO UNSIGNED INTEGER BINARY NUMBERS (N-BIT NUMBERS)
. To implement restoring division algorithm in digital computer  


function div
n=input('enter n:');
a=input('enter a:');
b=input('enter b:');
c=b;
q=a;
carry=0;
for i=1:1:n
    a(1,i)=0;
end
for i=n:-1:1
    if(c(1,i)==1)
        c(1,i)=0;
    
    else
        c(1,i)=1;
    end
end
for i=1:1:n-1
    x(1,i)=0;
end
x(1,n)=1;
for i=n:-1:1
sum=xor(xor(x(1,i),c(1,i)),carry);
carry=or(and(x(1,i),c(1,i)),and(xor(x(1,i),c(1,i)),carry));
r(1,i)=sum;
end
c=r;
for count=0:1:n-1
    for i=1:1:n-1
        a(1,i)=a(1,i+1);
    end
    a(1,n)=q(1,1);
    for i=1:1:n-1
        q(1,i)=q(1,i+1);
    end
    carry=0;
    for i=n:-1:1
        sum=xor(xor(a(1,i),c(1,i)),carry);
        carry=or(and(a(1,i),c(1,i)),and(xor(a(1,i),c(1,i)),carry));
        a(1,i)=sum;
    end
    if (a(1,1)==1)
       q(1,n)=0;
       carry=0;
        for i=n:-1:1
        sum=xor(xor(a(1,i),b(1,i)),carry);
        carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),carry));
        a(1,i)=sum;
        end
    else
        q(1,n)=1;
    end
end
fprintf('Remainder =');
disp(a);
fprintf('Quotient=');
disp(q);
end

Computer Organisation & Architecture

PROGRAM IN MATLAB FOR MULTIPLICATION OF TWO UNSIGNED INTEGER BINARY NUMBERS BY PARTIAL-PRODUCT METHOD (4BITS NUMBERS)


function lab2_coa
a=input('enter a:');
b=input('enter b:');
r=[0 0 0 0 0 0 0 0];
c=0;
for j=4:-1:1
    if(b(1,j)==0)
        continue;
    end
    for i=4:-1:1
    sum=xor(xor(r(1,i+j),a(1,i)),c);
    carry=or(and(r(1,i+j),a(1,i)),and(xor(r(1,i+j),a(1,i)),c));
    c=carry;
    r(1,i+j)=sum;
 
    end
 
end

disp(r);
disp(c);

end

Computer Organisation & Architecture

PROGRAM IN MATLAB FOR SUBTRACTION OF TWO UNSIGNED INTEGER BINARY NUMBER (4BITS NUMBERS)


function lab3_coa
a=input('enter a:');
b=input('enter b:');
c=0;
temp=a;
for i=4:-1:1
    if(b(1,i)==1)
        b(1,i)=0;
 
    else
        b(1,i)=1;
    end
end
a=[0 0 0 1];
for i=4:-1:1
sum=xor(xor(a(1,i),b(1,i)),c);
carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),c));
c=carry;
result(1,i)=sum;
end
a=temp;
b=result;
c=0;
for i=4:-1:1
sum=xor(xor(a(1,i),b(1,i)),c);
carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),c));
c=carry;
result(1,i)=sum;
end
if(c==0)
    fprintf('The number is negative.');
    for i=4:-1:1
    if(result(1,i)==1)
        result(1,i)=0;
 
    else
        result(1,i)=1;
    end
    end
a=[0 0 0 1];
for i=4:-1:1
sum=xor(xor(a(1,i),result(1,i)),c);
carry=or(and(a(1,i),result(1,i)),and(xor(a(1,i),result(1,i)),c));
c=carry;
result(1,i)=sum;
end
end
disp(result);
disp(c);

end

Computer Organisation & Architecture

PROGRAM IN MATLAB FOR  ADDITION OF TWO UNSIGNED INTEGER BINARY NUMBER (4-BITS NUMBERS)




function lab0_coa
a=input('enter a:');
b=input('enter b:');
c=input('enter c:');
for i=4:-1:1
sum=xor(xor(a(1,i),b(1,i)),c);
carry=or(and(a(1,i),b(1,i)),and(xor(a(1,i),b(1,i)),c));
c=carry;
result(1,i)=sum;
end
disp(result);
disp(c);
end