Esempio n. 1
0
/*=============================================================================
Function multm

Purpose:  multiplies two numbers mod m
          
Parameters:
     ul a (IN) - first operand
     ul b (IN) - second operand
     ul m (IN) - modulus

Returns:  a * b mod m
=============================================================================*/
ul multm(ul a, ul b, ul m) {
  ul mask = 0x80000000;
  ul i;
  ul t=0;
  for (i=0;i<32;i++) {
    t = mult2m(t,m);
    if (b&mask) t=addm(t,a,m);
    mask>>=1;
  }
  return t;
}
Esempio n. 2
0
int instruction_cycle(CPU *cpu) {
	int halted = 1;
	
	(*cpu).ir = (*cpu).mem[(*cpu).pc];
	handleInstruction(cpu);
	switch ((*cpu).opcode){
		case 0:
		  halted = 0;
		  printf("Halt!");
		  break;
		case 1:
		  ld(cpu);
		  break;
		case 2:
		  st(cpu);	
		  break;
		case 3:
		  add(cpu);
		  break;
		case 4:
		  neg(cpu);
		  break;
		case 5:
		  ldm(cpu);
		  break;
		case 6:
		  addm(cpu);
		  break;
		case 7:
		  br(cpu);
		  break;
		case 8:
		  brp(cpu);
		  break;
		case 9:
		  io(cpu);
		  break;
		default:
		  printf("Error:Invalid opcode at address %d,Invalid number: %d",(*cpu).pc,(*cpu).mem[(*cpu).pc]);
		  break;
	}
	(*cpu).pc++;
	return halted;
	// For Lab 7, we just print a message and halt after the 10th call
	//
	//char suffix[][4] = {"", "st","nd","rd","th"};
	//printf("Calling instruction_cycle for %d%s time\n", call_nbr, suffix[min(call_nbr,4)]);

}
Esempio n. 3
0
void main()
{
	clrscr();
	int m,n,p,q,a[20][20],b[20][20],c[20][20];
	cout<<"Enter no:of rows & columns of first matrix:";
	cin>>m>>n;
	cout<<"Enter no:of rows & columns of second matrix:";
	cin>>p>>q;
	if(m!=p||n!=q)
		cout<<"Matrix addition and substraction not possible.\n";
	else
	{
		cout<<"Enter elements of First matrix:\n";
		getm(m,n,a);
		cout<<"Enter elements of Second matrix:\n";
		getm(p,q,b);
		clrscr();
		cout<<"First matrix:\n";
		prim(m,n,a);
		cout<<"Second matrix:\n";
		prim(p,q,b);
		cout<<"Matrix Sum:\n";
		addm(m,n,a,b);
		cout<<"Matrix Difference:\n";
		subm(m,n,a,b);
	}
	if(m!=q)
		cout<<"Matrix multiplication not possible.";
	else
	{
		if(m!=p||n!=q)
		{
			cout<<"Enter elements of First matrix:\n";
			getm(m,n,a);
			cout<<"Enter elements of Second matrix:\n";
			getm(p,q,b);
			clrscr();
			cout<<"First matrix:\n";
			prim(m,n,a);
			cout<<"Second matrix:\n";
			prim(p,q,b);
		}
		cout<<"Matrix Product:\n";
		mulm(m,n,q,a,b,c);
	}
	getch();
}