Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//C code for the ISS /* Instruction Set Simulator * Key * Assembly Inst. First B

ID: 3581092 • Letter: #

Question

//C code for the ISS

/* Instruction Set Simulator

* Key

* Assembly Inst.     First Byte Second Byte

*

* MOV Rn, direct     0000 Rn          direct

* MOV direct,Rn 0001 Rn          direct

* MOV @Rn, Rm        0010 Rn          Rm ____

* MOV Rn, #immed.    0011 Rn          immed.

* ADD Rn, Rm         0100 Rn          Rm ____

* SUB Rn, Rm         0101 Rn          Rm ____

* JZ Rn, relative    0110 Rn          relative

*

*/

#include <stdio.h>

typedef struct {

   unsigned char first_byte, second_byte;

} instruction;

instruction program[1024]; //instruction memory

unsigned char memory[256], reg[16]; //data memory

int run_program(int num_bytes) {

   int pc = -1;

   unsigned char fb, sb;

   while( ++pc < (num_bytes / 2) ) {

      fb = program[pc].first_byte;

      sb = program[pc].second_byte;

      switch( fb >> 4 ) { // bit wise shift by 4 in the while loop

         case 0: // MOV Rn, direct

           reg[fb & 0x0f] = memory[sb];

           break;

         case 1: // MOV direct, Rn

           memory[sb] = reg[fb & 0x0f];

           break;

         case 2: // MOV @Rn, Rm

           memory[reg[fb & 0x0f]] = reg[sb >> 4];

           break;

         case 3: // MOV Rn, #immed.

           reg[fb & 0x0f] = sb;

           break;

         case 4: // ADD Rn, Rm

           reg[fb & 0x0f] += reg[sb >> 4];

           break;

         case 5: // SUB Rn, Rm

           reg[fb & 0x0f] -= reg[sb >> 4];

           break;

         case 6: // JZ Rn, relative

           if (reg[fb & 0x0f] == 0) {

                pc = (0x0f & sb) - 1;

           }

           else

                pc = pc;

           break;

         default: return -1;

      }

   }

   return 0;

}

void print_memory_contents(){

      printf("Printing memory ");

      int i;

      for(i=0;i<256;i++){

           printf("%i ",memory[i]);

           if ((i+1)%8 == 0){

                 printf(" ");

           }

      }

      return;

}

void print_register_contents(){

      printf("Printing registers ");

      int i;

      for(i=0;i<16;i++){

           printf("R%i : %i ",i,reg[i]);

      }

      return;

}

void initialize_processor(){

      int i;

      for(i=0;i<1024;i++){

           program[i].first_byte = 0x00;

           program[i].second_byte = 0x00;

      }

      for(i=0;i<256;i++){

           memory[i] = 0x00;

      }

      for(i=0;i<16;i++){

           reg[i] = 0x00;

      }

}

int main() {

      // initializes the data and program memory, and the registers

      initialize_processor();

      // load program into program memory

      // Instruction Binary                      Hex

      // MOV R0, #0 0010000000000000 3000

      program[0].first_byte = 0x30;

      program[0].second_byte = 0x00;

      program[1].first_byte = 0x31;

      program[1].second_byte = 0x0A;

      program[2].first_byte = 0x32;

      program[2].second_byte = 0x01;

      program[3].first_byte = 0x33;

      program[3].second_byte = 0x00;

      program[4].first_byte = 0x61;

      program[4].second_byte = 0x08;

      program[5].first_byte = 0x40;

      program[5].second_byte = 0x10;

      program[6].first_byte = 0x51;

      program[6].second_byte = 0x20;

      program[7].first_byte = 0x63;

      program[7].second_byte = 0x04;

      program[8].first_byte = 0x38;

      program[8].second_byte = 0xff;

      run_program(18);

      print_memory_contents();

      print_register_contents();

      return 0;

}

You must explain, in your own words, how the design of ISS, the improvements made by adding instructions, as well as the two assembly programs and binary decoding. Please include the contents of the data and program memory for each of the assembly programs

Explanation / Answer

Soltuion In this C ISS code there are four function named

In addition to these four function one main function is defined that start the actual execution of the program in C programming language.

Globally one user defined data type is created using typedef struct statement with two elements to store the two bytes data in character form. The name of UDT (User Defined Type) is instruction and a variable is created for this data type as name program, which is capable to store 1024 bytes having two sub elements – first_byte, second_byte.

Another two global variables are defined with name memory having capacity of 256 character and reg having capacity of 16 characters

----------------------------description of main function --------------

In case of main() function it first calls the function initialilze_processor();

After calling of initialize_processor function, which initialize bytes, memory and reg to 0, it assign values to program[0].first_byte=0X30 and program[0].second_byte=0X00


….

program[8].first_byte=0X38
program[8].second_byte=0Xff

after performing the assignment to program bytes it call run_program function by passing the 18 value because there are 9 bytes with two parts first_byte and second_byte.

Then calls function print_memory_contents() and print_register_content()

After calling the return 0 statement it terminate the function main.

---------- end of main function description -----------------

-------------description of function print_memory_contents-----------------

In this function it first print message “Printing Memory” then execute a loop with variable i from 0 to 255 and print the content on the basis of 8character and after printing one byte it moves to the next line using login mod of (i+1) = 0 then print(“ ”)

---------------end of description for print_memory_contents-----------------

------------description of print_register_contents function------------

In this function it shows message “Printing Register” then print the content of register with the help of i variable, varying its value from 0 to 15.

---------------end of description for print_register_contents-----------------

------------------------------ description of initialize_processor function -------------

Which perform the task of initialization task for memory, register and program bytes i.e. first_byte and second_byte.

This function use the loop from i=0 to 1023 for initialization of byte. It assign 0 to first_bytes and second_byte.

It initialize memory with loop i =0 to 255. It also assign 0 to memory

It initialize register with loop i =0 to 15. It also assign 0 to reg

------------------ end of initialize_processor function -----------------

------------------description of run_program(18) function------------

In this function a ProgramCounter is managed with the help of pc with initialized value to -1 because array value start in c from 0.

Two variable fb (for First Byte) and sb (for Second bytes) declared.

While loop is used to execute the statements on the basis of first increment the PC and run the loop until PC value is not reaches to 9 i.e. 18 /2 (for two bytes for each statement).

Under the while loop case statement test the value on the basis of right shift 4 bits of first byte and execute statement accordingly.

If it is 0 then execute MOV Rn, direct using bitwise AND on fb and 0f with reference to the location in reg variable from value of memory at location sb
as
reg[fb & 0x0f] = memory[sb]

If is 1 then execute MOV direct, rn using the assignment of value in memory at sb location from reg location calculated with bitwise AND on on fb and 0f

If it is 2 then execute MOV @rn,rm using indirect reference
as

memory[reg[fb & 0x0f]] = reg[ sb >> 4]

if it is 3 then execute RN,#immed

if it is 4 then execute ADD Rn, Rm

if it is 5 then execute SUB Rn,Rm

if it is 6 then execute JZ Rn, relative by testing the condition of pc is not 0 then it decrement otherwise just assign the pc value again to pc

default case is considered and return -1 to calling point.

--------end of run_program function description --------------------