Shift Add Using RTL

 avatar
mdaalam22
c_cpp
2 years ago
2.9 kB
22
Indexable
Never
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main() {
	int dec1,dec2,i=0,counter,j,carry,temp=0,c=0,temporary,decimalEqv;
	int x[20]={0},y[20]={0},u[20]={0},v[20]={0};
	printf("\n\tEnter multiplier and multiplicant:");
	scanf("%d %d",&dec1,&dec2);
	
	while(dec1 != 0 || dec2 != 0) {
		y[i] = dec1%2;
		dec1/=2;
		
		x[i] = dec2%2;
		dec2/=2;
		
		i++;	
	}

	counter = i-1;
	
	//output formating start
	printf("\n---------------------------------------------------------------------------------------------------------------------------------\n");
	printf("| \tCondn\t|\tmicro-operations\t|\ti\t|\tC\t|\tU\t|\tV\t|\tY\t|");
	printf("\n---------------------------------------------------------------------------------------------------------------------------------\n");
	printf("|\t 1 \t\t C<-0,U<-0,i<-%d",i);
	printf("\t\t\t%d \t\t %d \t\t",i,c);
	
	for(j=counter;j>=0;j--){
		printf("%d",u[j]);
	}
	printf("\t\t");
	
	for(j=counter;j>=0;j--){
		printf("%d",v[j]);
	}
	printf("\t\t");
	
	for(j=counter;j>=0;j--){
		printf("%d",y[j]);
	}

	//output formating end
	
	do{
	
		printf("\n|-------------------------------------------------------------------------------------------------------------------------------|\n");
		
		if(y[0]){
			//cu <- u+x
			printf("|\t");
			printf("Yo2 \t\t CU <- U+X\t\t\t\t\t\t\t");
			carry = 0;
			for(j=0;j<=counter; j++){
				temp = u[j] + x[j] + carry;
				u[j] = temp%2;
				printf("%d",u[j]);
				carry = temp/2;
			}	
			c = carry;
			printf("\t\t");
		}
		
		printf("\n|\t 2 \t\t i <- i-1 \t\t\t");
		
		i--; 
		
		//output formating start
		printf("%d \t\t %d \t\t",i,c);
		//output formating end
		

		for(j=0; j<counter;j++){
			v[j] = v[j+1];
		}
		v[j] = u[0];
		for(j=0; j<counter;j++){
			u[j] = u[j+1];
		}
		u[j] = c;
		c=0;
		
		//output formating start
		printf("\n");
		printf("|\t 3 \t\t shr(CUV),cir(Y) \t\t");
		printf("%d \t\t %d \t\t",i,c);
		for(j=counter;j>=0;j--){
			printf("%d",u[j]);
		}
		printf("\t\t");
		for(j=counter;j>=0;j--){
			printf("%d",v[j]);
		}
		printf("\t\t");
		for(j=counter;j>=0;j--){
			printf("%d",y[j]);
		
		}
		printf("\t|\n\n");
		//output formating end
		
		//cir(Y)
		temporary = y[0];
		for(j=0;j<counter;j++) {
			y[j] = y[j+1];
		}
		y[j] = temporary;
		
		if(i>0)
			printf("|\tZ'3 \t\t GOTO 2");
		else
			printf("|\tZ3 \t\t FINISH <- 1");
			
	}while(i>0);
	printf("\n|-------------------------------------------------------------------------------------------------------------------------------|\n");
	
	for(j=counter;j>=0;j--){
		decimalEqv += u[j]*pow(2,j+counter+1);
	}
	printf("\t");
	for(j=counter;j>=0;j--){
		decimalEqv += v[j]*pow(2,j);
	}
	
	printf("\n\n\tThe decimal equivalent result = %d\n\n",decimalEqv);
	
	
}