Assignment 2 : SMAC0 simulator
Software Description – A simple instruction Computer(SMAC0) is a hypothetical machine with a small but effective instruction set that can be used to illustrate the design of simple software processors involved in development of programs such as Assembler, Macro processor etc.
The machine will incorporate the most commonly encountered hardware features and concepts, while avoiding irrelevant complexities.
A simulator program is required that simulates the function of simple instruction computer such as fetching an instruction, decoding and executing it.
The hypothetical Simple machine (SMAC0) has following features.
# Memory – Memory consist of 6 digit words (decimal).
# Total size of memory is 1000 words (103 ) indicates the address size is 3 digits ( address ranges from 0 to 999).
# Registers – There are in all six registers four general purpose registers AREG, BREG, CREG and DREG numbered 1,2,3 and 4.
# A program counter (PC) storing the address of the next instruction to be fetched and a status register storing condition codes.
# There are SIX condition codes LT, LE, EQ, GT , GE and ANY numbered 0,1,2,3,4 and 5. Each bit in the status register can be set to 1
# Data Format – Supports only six digit integer data stored in decimal form.
# Instruction Format – It has single instruction format. Each instruction is of six digit length. The opcode, register operand and memory operand occupy 2, 1 and 3 digits in that order.
Instruction Set
Opcode Mnemonic Instruction Operands
00 ---STOP ----Stop or Halt execution Operands unused
01 ----ADD ----Add memory operand to register operand Register and memory operand
02---- SUB---- Subtract memory operand from register operand Register and memory operand
03---- MULT---- Multiply memory operand to register operand Register and memory operand
08---- DIV ----Divide register operand by memory operand Register and memory operand
04---- MOVER ----Move memory operand contents to register operand Register and memory operand
05---- MOVEM---- Move register operand contents to memory Register and memory operand
06 ----COMP---- Compare register and memory operands to set condition code appropriately Register and memory operand
07---- BC---- Branch to second operand depending on condition code specified as first operand Register and memory operand
09 ----READ ----Read into memory operand Only memory operand
10 ----PRINT ----Print contents of memory Operand Only memory operand
Condition code Mnemonic Description
0 ---LT---- Less than
1--- LE ----Less than or equal to
2--- EQ ---Equal to
3 ---GT--- Greater than
4--- GE ---Greater than or equal to
5---- ANY ----Unconditional
Slot 1
i) Answer the following questions after carefully reading the description and program structure.
a) What is the size of memory in Hypothetical Simple Instruction Computer(SMAC0)? How memory will be represented in simulation program?
Answer:-
Memory – Memory consist of 6 digit words (decimal). Total size of memory is 1000 words (103 ) indicates the address size is 3 digits ( address ranges from 0 to 999).
b) How many registers are there in SMAC0? How registers are represented in simulation program?
Answer:-
Registers – There are in all six registers four general purpose registers AREG, BREG, CREG and DREG numbered 1,2,3 and 4.
A program counter (PC) storing the address of the next instruction to be fetched and a status register storing condition codes. There are SIX condition codes LT, LE, EQ, GT , GE and ANY numbered 0,1,2,3,4 and 5. Each bit in the status register can be set to 1
c) From the contents of the memory at pc, how will you separate opcode, register operand and memory operand?
Answer:-
mem[pc] // contains the instruction
opcode=mem[pc]/10000; // separate register and memory operand
ii) Implement a menu driven simulator for hypothetical Simple Instruction Computer that provides the following functionalities
a) Load - Loading of the program from file into memory
b) Print - Printing the program loaded in memory
c) Run - Executing the loaded program
The machine has the basic instruction set comprising of add, mover, movem, read, print and hlt commands as given in Table 1. Create a file sum.sm containing the machine code for sum of two numbers. Test the program using the machine code programs sum.sm.
Program :-
#include<stdio.h>
#include<stdlib.h>
int mem[100],reg[4],cond[]={0,0,0,0,0,1},opc,op1,op2,pc;
FILE *fp;
char fname[20];
void load()
{
printf("Enter file name:");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}
while(fscanf(fp,"%d",&mem[pc])!=-1)
pc++;
fclose(fp);
}
void print()
{
int i;
for(i=0;i<pc;i++)
printf("%06d\n",mem[i]);
}
void run()
{ int i;
pc = 0;
while(1)
{ opc = mem[pc]/10000;
op1 = mem[pc]%10000/1000-1;
op2 = mem[pc]%1000;
switch(opc)
{
case 0: // STOP
return;
case 1: // ADD
reg[op1]+=mem[op2];
break;
case 2: // SUB
reg[op1]-=mem[op2];
break;
case 3: // MULT
reg[op1]*=mem[op2];
break;
case 4: // DIV
reg[op1]/=mem[op2];
break;
case 5: // MOVER
reg[op1]=mem[op2];
break;
case 6: // MOVEM
mem[op2]=reg[op1];
break;
case 7: // COMP
if(reg[op1] < mem[op2])
cond[0]=1;
if(reg[op1] <= mem[op2])
cond[1]=1;
if(reg[op1] == mem[op2])
cond[2]=1;
if(reg[op1] > mem[op2])
cond[3]=1;
if(reg[op1] >= mem[op2])
cond[4]=1;
break;
case 8: // BC
if(cond[op1]==1)
pc = op2-1;
for(i=0;i<5;i++)
cond[i]=0;
break;
case 9: // READ
scanf("%d",&mem[op2]);
break;
case 10:// PRINT
printf("%d\n",mem[op2]);
}
pc++;
}
}
int main()
{ int ch;
while(1)
{
printf("1.Load\n2.Print\n3.Run\n4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
load();
break;
case 2:
print();
break;
case 3:
run();
break;
case 4:
exit(0);
}
}
return 0;
}
-------------------------------------------------------------------------------------------
OUTPUT:-
1.Load
2.Print
3.Run
4.Exit
Enter your choice (1-4):1
Enter file name:sum.x
1.Load
2.Print
3.Run
4.Exit
Enter your choice (1-4):2
090014
051017
061016
061015
071014
084012
052016
012015
062016
051015
011018
086003
100016
000000
000000
000000
000000
000000
000001
1.Load
2.Print
3.Run
4.Exit
Enter your choice (1-4):3
1
2
3
1.Load
2.Print
3.Run
4.Exit
Enter your choice (1-4):4
------------------------------------------------------------------
sum.x
090014
051017
061016
061015
071014
084012
052016
012015
062016
051015
011018
086003
100016
000000
0
0
0
0
1
Hello.
ReplyDeleteI need the machine code Launguage for upper program in factorial test