Hi
ive had to write an emulator for a specific MIPS program in C, but i also need to either single step through the emulator one instruction at a time or simply let it run, at the moment its running, but im not sure how i can step through it one instruction at a time, the program is as follows:

#include <stdio.h>

int main()
{
	long int DM[512];
	
	unsigned long int IR;
	
	unsigned int k, op,rs,rt,rd,funct,shamt, PC;
	
	long int R[32];
	int immediate;
	
	// loading instructions into array
	unsigned long int IM[512]={0x20020000, 
	0x20030020,
	0xac430000,
	0xac430080,
	0x20420004,
	0x2063ffff,
	0x1460fffb,
	0x20020000,
	0x20030020,
	0x20070000,
	0x8c440000,
	0x8c450080,
	0x00850018,
	0x00003012,
	0x00e63820,
	0x20420004,
	0x2063ffff,
	0x1460fff8,
	0x1000ffff};

	PC=0; 
	R[0]=0;
	
	while (PC <= 68)
	{
		IR=IM[PC>>2];
		
		op=(IR>>26)&63;
		rs=(IR>>21)&31;
		rt=(IR>>16)&31;
		rd=(IR>>11)&31;
		shamt=(IR>>6)&31;
		funct=IR&63;
		immediate=IR&0xffff; //sign extended

		if (immediate>32767) 
		
			immediate-=65536;

//		printf("%i  %i  %i  %i  %i  %i  %i %i\n",PC,op,rs,rt,rd,shamt,funct,immediate);

		PC = PC + 4;

		switch (op) 
		{
			case 0:
			{
				switch (funct) 
				{
					case 24:
					{
						k = R[rs] * R[rt];
						break;	
					}
					case 18:
					{
						R[rd] = k;
						break;
					}
					case 32:
					{
						R[rd] = R[rs] + R[rt];
						break;
					}
				
				}
				break;
			}
			case 4:
			{
				if(R[rs] == R[rt])
					PC = PC + immediate*4;
				break;
			}
			case 5:
			{
				if(R[rs]!=R[rt])
					PC = PC + immediate*4;
				break;
			}
			case 8:
			{
				R[rt] = R[rs] + immediate;
				break;
			}
			case 35:
			{
				R[rt] = DM[R[rs] + immediate];
				break;
			}
			case 43:
			{
				printf("%i\n", R[rs]);
				DM[R[rs] + immediate] = R[rt];
				break;
			}
		
		}
	
	}
	printf("the result of dot product is %i\n", R[7]);
	return 0;

}

if you could just give me an idea of how i could step through that would be great
thnx

I'm sorry to critisize your code, but it's design is quite poor. Now that you understand how to execute the code, what about cleaning this mess, in order to make the thing a little more reusable an understandable ?
if you want to step the execution, put a function that stops the execution of your while() statement, for example fgetc(stdin) or something like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.