My program fails these two tests
Hi folks,
My program fails these tests. Test 9 is for encode_instruction and the test 17 is for valid_instructions
if anyone can help me to find the problem on these two functions I really appreciate that.
My code:
Code:
void print_instruction(Word instruction)
{
unsigned int opcode = instruction >> 28 ;
unsigned int reg1 = ( instruction >> 24 ) & 0xf;
unsigned int reg2 = ( instruction >> 20 ) & 0xf;
unsigned int reg3 = ( instruction >> 16 ) & 0xf;
unsigned int memory_addr = ( instruction & 65535 );
switch( opcode )
{
case HALT:
printf( "halt\n" );
break;
case ADD:
printf( "%s\tR%02d\tR%02d\tR%02d\n", "add", reg1, reg2, reg3 );
break;
case MUL:
printf( "%s\tR%02d\tR%02d\tR%02d\n", "mul", reg1,reg2,reg3);
break;
case NEG:
printf("%s\tR%02d\tR%02d\n","neg", reg1,reg2);
break;
case SHL:
printf("%s\tR%02d\tR%02d\t%05d\n","shl", reg1,reg2, memory_addr);
break;
case AND:
printf("%s\tR%02d\tR%02d\tR%02d\n","and", reg1,reg2,reg3);
break;
case NOT:
printf("%s\tR%02d\tR%02d\n","not", reg1,reg2);
break;
case LW:
printf("%s\tR%02d\t%05d\n","lw", reg1,memory_addr);
break;
case LI:
printf("%s\tR%02d\t%05d\n","li", reg1,memory_addr);
break;
case SW:
printf("%s\tR%02d\t%05d\n","sw", reg1,memory_addr);
break;
case MOVE:
printf("%s\tR%02d\tR%02d\n","move", reg1,reg2);
break;
case BNE:
printf("%s\tR%02d\tR%02d\t%05d\n","bne", reg1,reg2,memory_addr);
break;
case READ:
printf("%s\tR%02d\n","read", reg1);
break;
case WRITE:
printf("%s\tR%02d\n","write", reg1);
break;
}
}
int disassemble(const Word memory[], int starting_address, int num_words){
if((starting_address%4!=0 )|| (memory==NULL))
return -1;
else if((starting_address<0) || (starting_address>12287) ||
(starting_address+ 4*num_words > 12287))
return -1;
else if(num_words<=0)
return 0;
else
{
int pos=starting_address/4;
int col=0;
while(col<num_words && valid_instruction(memory[pos]))
{
printf("0x%04x: ", starting_address);
print_instruction(memory[pos]);
pos++;
col++;
starting_address+=4;
}
return col;
}
}
int encode_instruction(unsigned int opcode, unsigned int reg1,
unsigned int reg2, unsigned int reg3,
unsigned int memory_addr, Word *const instruction)
{
Word temp=0x00000000;
temp = (opcode<<28) | temp;
temp = (reg1<<24) | temp;
temp = (reg2<<20) | temp;
temp = (reg3<<16) | temp;
temp = memory_addr | temp;
if(( instruction != NULL ) && ( valid_instruction( temp ) ) )
{
*instruction = temp;
return 1;
}
else
return 0;
}
int valid_instruction(Word instruction){
unsigned int opcode = instruction >> 28 ;
unsigned int reg1 = ( instruction >> 24 ) & 0xf;
unsigned int reg2 = ( instruction >> 20 ) & 0xf;
unsigned int reg3 = ( instruction >> 16 )& 0xf;
unsigned int memory_addr = ( instruction & 65535 );
if((opcode<0) || (opcode>14))
return 0;
else if(opcode==0)
return 1;
else if((reg1<=0) || (reg1>=11))
return 0;
else if(opcode<12)
{
if((opcode>6) && (opcode<10))
{
if((memory_addr>49151) || (memory_addr%4!=0))
return 0;
}
else if((reg2<0) || (reg2>11))
return 0;
else if(opcode==4)
{
if((memory_addr>31) || (memory_addr%4!=0))
return 0;
}
else if(opcode==11)
{
if((memory_addr>49151) || (memory_addr%4!=0))
return 0;
}
else
if((reg3<0) || (reg3>11))
return 0;
}
return 1;
}
Test 9 that is written for encode_instructions
my code fails this test
Code:
#include <assert.h>
#include "machine.h"
int main() {
Word word= 0;
/* invalid opcode */
assert(encode_instruction(WRITE + 1, R00, R00, R00, 0, &word) == 0);
/* invalid first register operand- too large */
assert(encode_instruction(WRITE, R11 + 1, R00, R00, 0, &word) == 0);
/* invalid second register operand- too large */
assert(encode_instruction(NEG, R02, R11 + 2, R00, 0, &word) == 0);
/* invalid third register operand- too large */
assert(encode_instruction(MUL, R05, R02, R11 + 3, 0, &word) == 0);
/* invalid memory address- too large */
assert(encode_instruction(LW, R04, R00, R00, 0xfdc8, &word) == 0);
/* invalid memory address- not a multiple of 4 */
assert(encode_instruction(LW, R07, R00, R00, 0x4321, &word) == 0);
/* invalid destination register of instruction (special register) */
assert(encode_instruction(LI, R00, R00, R00, 0x0004, &word) == 0);
/* invalid destination register of instruction (special register) */
assert(encode_instruction(LW, R11, R00, R00, 0x0004, &word) == 0);
/* invalid shift amount */
assert(encode_instruction(SHL, R03, R02, R00, 32, &word) == 0);
assert(word == 0); /* ensure it's still unmodified */
printf("All assertions succeeded!\n");
return 0;
}
test 17 is for valid_instruction function my code fails this one too
Code:
#include <stdio.h>
#include <assert.h>
#include "machine.h"
/* Tests whether valid_instruction() ignores invalid values in fields
* that are not used by instructions, meaning that they have no effect on
* the instructions' validity.
*/
int main() {
/* halt instruction with invalid first register operand */
assert(valid_instruction(0x0f000000) == 1);
/* read instruction with invalid second register operand */
assert(valid_instruction(0xc1d00000) == 1);
/* neg instruction with invalid third register operand */
assert(valid_instruction(0x333d0000) == 1);
/* add instruction with invalid memory address (not divisible by 4) */
assert(valid_instruction(0x1a980001) == 1);
/* add instruction with invalid memory address (too large) */
assert(valid_instruction(0x1a98ddd1) == 1);
printf("All assertions succeeded!\n");
return 0;
}