Salve a tutti! sto compilando un programma in C che dovrebbe funzionare come simulatore di linguaggio macchina o qualcosa del genere...

Il programma è x scopo didattico quindi decisamente poco utile :P

cmq...il programma dovrebbe ricevere in imput delle sequenze di 4 cifre, le prime 2 servono ad identificare l'operazione da eseguire, le altre servono ad identificare la cella di memoria alla quale l'operazione dovrebbe fare riferimento...

in problema è che in output non mi restituisce nulla e, anzi, ho inserito dei comandi printf x capire come si comportasse il programma in esecuzione, e a volte ripete un "caso" della switch più volte, come se andasse in ciclo continuo...

il programma è:

codice:
#include<stdio.h>

int main ()
{
	printf("          *** Welcome to Simpletron! ***\n");
	printf("*** Please enter your program one instruction ***\n");
	printf("*** (or data word) at a time. I will type the ***\n");
	printf(" *** location number and a question mark(?). ***\n");
	printf("*** You then type the word for that location. ***\n");
	printf("*** Type the sentinel -9999 to stop entering ***\n");
	printf("              *** your program. ***\n");

	int memory [ 100 ];
	int accumulator = 0;
	int instructionRegister = 0;
	int operationCode [ 100 ];
	int operand [ 100 ];
	int i = 0;
	int d = 0;
	int f = 0;

	while ( instructionRegister != -9999 ) {
	printf("%.2d ? ",i);
	scanf("%d",&instructionRegister);
	memory [ i ] = instructionRegister;
	i++;
	}
	printf("*** Program loading completed ***\n");
	printf("*** Program execution begins ***\n");

	d = i;
	for ( i = 0; i < d - 1; i++ )                                (separa le 4 cifre in output)
	{                                                                 (in 2 da 2)
		while( ( memory [ i ] - f ) > 100 )
		{
			f = f + 100;
		}
	operationCode [ i ] = f / 100;
	operand [ i ] = memory [ i ] - f;
	}

	for ( i = 0; i < d - 1; i++ )
	{
		switch ( operationCode [ i ] )
		{
		case 0:
			printf("OK 0\n");
			break;
		case 10:
			scanf("%d", &memory [ operand [ i ] ]);
			break;
		case 11:
			printf("OK11\n");
			printf("%d", memory [ operand [ i ] ]);
			break;
		case 20:
			accumulator = memory [ operand [ i ] ];
			printf("OK20\n");
			break;
		case 21:
			memory [ operand [ i ] ] = accumulator;
			printf("OK21\n");
			break;
		case 30:
			printf("%d accumulator prima\n",accumulator);
			accumulator += memory [ operand [ i ] ];
			printf("%d accumulator dopo\n",accumulator);
			printf("OK30\n");
			break;
		case 31:
			accumulator -= memory [ operand [ i ] ];
			printf("OK31\n");
			break;
		case 32:
			accumulator = memory [ operand [ i ] ] * accumulator;
			printf("OK32\n");
			break;
		case 33:
			accumulator = memory [ operand [ i ] ] / accumulator;
			printf("OK33\n");
			break;
		case 40:
			i = operand [ i ];
			printf("OK40\n");
			break;
		case 41:
			if (accumulator < 0 )
			{i = operand [ i ];
			printf("OK41\n");}
			break;
		case 42:
			if (accumulator == 0 )
			{i = operand [ i ];
			printf("OK42\n");}
			break;
		case 43:
			printf("*** Simpletron execution terminated ***");
			i = d;
			printf("OK43\n");
			break;
		default:
			printf("*** +%d%d is an invalid command, program terminated ***", operationCode [ i ], operand [ i ]);
			i = d;
			break;
		}
	}
return 0;
}
inserendo in imput la sequenza:

codice:
00 ? 1007
01 ? 1008
02 ? 2007
03 ? 3008
04 ? 2109
05 ? 1109
06 ? 4300
07 ? 0000
08 ? 0000
09 ? 0000
10 ? -9999
questa è la sequenza x calcolare la somma di 2 numeri e stamparli in uscita...
però in output mi esce:

codice:
6                                       (primo num inserito da tastiera)
3                                       (secondo num inserito da tastiera)
OK20                                  (controllo del caso 20 ok)
6 accumulator prima              (il primo dato inserito in "accumulator")
9 accumulator dopo               (accumulator dopo aver sommato le 2 variabili)
OK30                                  (da qui si ripete e non capisco il motivo)
9 accumulator prima
9 accumulator dopo
OK30
9 accumulator prima
9 accumulator dopo
OK30
spero di essermi spiegato più o meno chiaramente... :\