memory:
mem[ 0 ] 8454149
mem[ 1 ] 71827456
mem[ 2 ] 75497476
mem[ 3 ] 8650757
mem[ 4 ] 25165824
mem[ 5 ] 8
registers:
reg[ 0 ] 0
reg[ 1 ] -2147483648
reg[ 2 ] 0
reg[ 3 ] 0
reg[ 4 ] 0
reg[ 5 ] 0
reg[ 6 ] 0
reg[ 7 ] 0
end state
12. push, pop: push 2, push 3, pop, pop.
Програма:
lw 0 1 num1
push
lw 0 1 num2
push
pop
pop
done halt
num1 .fill 2
num2 .fill 3
Машинний код:
8454150
79691776
8454151
79691776
83886080
83886080
25165824
2
3
Проміжний стан, після двох push:
@@@
state:
pc 4
ZF = 0
stack:
stk[ 0 ] 2
stk[ 1 ] 3
memory:
mem[ 0 ] 8454151
mem[ 1 ] 79691776
mem[ 2 ] 8454152
mem[ 3 ] 79691776
mem[ 4 ] 83886080
mem[ 5 ] 83886080
mem[ 6 ] 25165824
mem[ 7 ] 2
mem[ 8 ] 3
registers:
reg[ 0 ] 0
reg[ 1 ] 3
reg[ 2 ] 0
reg[ 3 ] 0
reg[ 4 ] 0
reg[ 5 ] 0
reg[ 6 ] 0
reg[ 7 ] 0
end state
Кінцевий стан:
@@@
state:
pc 7
ZF = 0
stack:
memory:
mem[ 0 ] 8454151
mem[ 1 ] 79691776
mem[ 2 ] 8454152
mem[ 3 ] 79691776
mem[ 4 ] 83886080
mem[ 5 ] 83886080
mem[ 6 ] 25165824
mem[ 7 ] 2
mem[ 8 ] 3
registers:
reg[ 0 ] 0
reg[ 1 ] 2
reg[ 2 ] 0
reg[ 3 ] 0
reg[ 4 ] 0
reg[ 5 ] 0
reg[ 6 ] 0
reg[ 7 ] 0
end state
При виконанні даного курсового проекту було реалізовано прототипний CISC - комп’ютер згідно із поставленим завданням. Створений комп’ютер пройшов тестування на коректність виконуваних операцій та на відловлювання помилок у вхідному асемблерному коді при синтаксичному та семантичному аналізі. Засвоєно принципи дії та архітектуру прототипних варіантів CISC - комп’ютера. Було внесено зміни в структуру існуючого симулятора CISC - комп’ютера, а саме, доповнена система команд заданими інструкціями, змінено формат усіх команд в частині КОП. До існуючих типів адресації CISC - комп’ютера було добавлено безадресний тип адресації, що в свою чергу призвело до створення стеку всередині структури комп’ютера. Було проведено аналіз роботи команд усіх типів та написано тести з поданням результату роботи симулятора у вигляді виведеного стану машини.
1. Мельник А.О. Архітектура комп’ютера. Наукове видання. - Луцьк: Волинська обласна друкарня, 2008. - 470 с.
2. Жмакин А.П. Архитектура ЭВМ. - СПб.: БХВ-Петербург, 2006. - 320 с.
3. Таненбаум Э. Архитектура компьютера.5-е изд. (+CD). - СПб.: Питер, 2007. - 844 с.
4. Patterson D., and Hennessy J.computer Architecture. A quantitative Approach. Second Edition. - Morgan Kaufmann Publishers, Inc., San Francisco, California, 1996. - 760 p.
Доаток I (код програми-асемблера):
/* Assembler for LC */
#include <stdlib. h>
#include <stdio. h>
#include <string. h>
#define MAXLINELENGTH 1000
#define MAXNUMLABELS 65536
#define MAXLABELLENGTH 7 /* includes the null character termination */
#define ADD 0
#define NAND 1
#define LW 2
#define SW 3
#define BEQ 4
#define JALR 5
#define HALT 6
#define NOOP 7
#define div 8
#define imul 9
#define xidiv 10
#define andf 11
#define xorf 12
#define cmpge 13
#define jmae 14
#define jmnae 15
#define bsf 16
#define bsr 17
#define jne 18
#define push 19
#define pop 20
int readandfParse (FILE *, char *, char *, char *, char *, char *);
int translateSymbol (char labelArray [MAXNUMLABELS] [MAXLABELLENGTH], int labelAddress [MAXNUMLABELS], int, char *);
int isNumber (char *);
void testRegArg (char *);
void testAddrArg (char *);
int main (int argc, char *argv [])
{
char *inFileString, *outFileString;
FILE *inFilePtr, *outFilePtr;
int address;
char label [MAXLINELENGTH], opcode [MAXLINELENGTH], arg0 [MAXLINELENGTH],
arg1 [MAXLINELENGTH], arg2 [MAXLINELENGTH], argTmp [MAXLINELENGTH];
int i;
int numLabels=0;
int num;
int addressField;
char labelArray [MAXNUMLABELS] [MAXLABELLENGTH];
int labelAddress [MAXNUMLABELS];
if (argc! = 3) {
printf ("error: usage: %s <assembly-code-file> <machine-code-file>\n",
argv [0]);
exit (1);
}
inFileString = argv [1];
outFileString = argv [2];
inFilePtr = fopen (inFileString, "r");
if (inFilePtr == NULL) {
printf ("error in opening %s\n", inFileString);
exit (1);
}
outFilePtr = fopen (outFileString, "w");
if (outFilePtr == NULL) {
printf ("error in opening %s\n", outFileString);
exit (1);
}
/* map symbols to addresses */
/* assume address start at 0 */
for (address=0; readandfParse (inFilePtr, label, opcode, arg0, arg1, arg2);
address++) {
/*
printf ("%d: label=%s, opcode=%s, arg0=%s, arg1=%s, arg2=%s\n",
address, label, opcode, arg0, arg1, arg2);
*/
/* check for illegal opcode */
if (strcmp (opcode, "add") && strcmp (opcode, "nand") &&
strcmp (opcode, "lw") && strcmp (opcode, "sw") &&
strcmp (opcode, "beq") && strcmp (opcode, "jalr") &&
strcmp (opcode, "halt") && strcmp (opcode, "noop") &&
strcmp (opcode,". fill") && strcmp (opcode, "div") &&
strcmp (opcode, "imul") && strcmp (opcode, "xidiv") &&
strcmp (opcode, "andf") && strcmp (opcode, "xorf") &&
strcmp (opcode, "cmpge") && strcmp (opcode, "jmae") &&
strcmp (opcode, "jmnae") && strcmp (opcode, "bsr") &&
strcmp (opcode, "jne") && strcmp (opcode, "bsf") &&
strcmp (opcode, "push") && strcmp (opcode, "pop"))
{
printf ("error: unrecognized opcode %s at address %d\n", opcode,
address);
exit (1);
}
/* check register fields */
if (! strcmp (opcode, "add") ||! strcmp (opcode, "nand") ||
! strcmp (opcode, "lw") ||! strcmp (opcode, "sw") ||
! strcmp (opcode, "beq") ||! strcmp (opcode, "jalr") ||
! strcmp (opcode, "div") ||! strcmp (opcode, "imul") ||
! strcmp (opcode, "xidiv") ||! strcmp (opcode, "andf") ||
! strcmp (opcode, "xorf") ||! strcmp (opcode, "cmpge") ||
! strcmp (opcode, "bsf") ||! strcmp (opcode, "bsr"))
{
testRegArg (arg0);
testRegArg (arg1);
}
if (! strcmp (opcode, "nand") ||! strcmp (opcode, "add") ||
! strcmp (opcode, "div") ||! strcmp (opcode, "imul") ||
! strcmp (opcode, "xidiv") ||! strcmp (opcode, "andf") ||
! strcmp (opcode, "xorf") ||! strcmp (opcode, "cmpge"))
{
testRegArg (arg2);
}
/* check addressField */
if (! strcmp (opcode, "lw") ||! strcmp (opcode, "sw") ||
! strcmp (opcode, "beq") ||! strcmp (opcode, "jmae") ||
! strcmp (opcode, "jmnae") ||! strcmp (opcode, "jne"))
{
testAddrArg (arg2);
}
if (! strcmp (opcode,". fill"))
{
testAddrArg (arg0);
}
/* check for enough arguments */
if ( (strcmp (opcode, "halt") && strcmp (opcode, "noop") &&
strcmp (opcode,". fill") && strcmp (opcode, "jalr") &&
strcmp (opcode, "bsf") && strcmp (opcode, "bsr") &&
strcmp (opcode, "pop") && strcmp (opcode, "push") &&
strcmp (opcode, "je") && arg2 [0] =='\0') ||
(! strcmp (opcode, "jalr") && arg1 [0] =='\0') ||
(! strcmp (opcode,". fill") && arg0 [0] =='\0'))
{
printf ("error at address %d: not enough arguments\n", address);
exit (2);
}
if (label [0]! = '\0') {
/* check for labels that are too long */
if (strlen (label) >= MAXLABELLENGTH) {
printf ("label too long\n");
exit (2);
}
/* make sure label starts with letter */
if (! sscanf (label, "% [a-zA-Z]", argTmp)) {
printf ("label doesn't start with letter\n");
exit (2);
}
/* make sure label consists of only letters andf numbers */
sscanf (label, "% [a-zA-Z0-9]", argTmp);
if (strcmp (argTmp, label)) {
printf ("label has character other than letters andf numbers\n");
exit (2);
}
/* look for duplicate label */
for (i=0; i<numLabels; i++) {
if (! strcmp (label, labelArray [i])) {
printf ("error: duplicate label %s at address %d\n",
label, address);
exit (1);
}
}
/* see if there are too many labels */
if (numLabels >= MAXNUMLABELS) {
printf ("error: too many labels (label=%s) \n", label);
exit (2);
}
strcpy (labelArray [numLabels], label);
labelAddress [numLabels++] = address;
}
}
for (i=0; i<numLabels; i++) {
/* printf ("%s = %d\n", labelArray [i], labelAddress [i]); */
}
/* now do second pass (print machine code, with symbols filled in as
addresses) */
rewind (inFilePtr);
for (address=0; readandfParse (inFilePtr, label, opcode, arg0, arg1, arg2);
address++) {
if (! strcmp (opcode, "add")) {
num = (ADD << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "nand")) {
num = (NAND << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "div")) {
num = (div << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "imul")) {
num = (imul << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "xidiv")) {
num = (xidiv << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "andf")) {
num = (andf << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "xorf")) {
num = (xorf << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "cmpge")) {
num = (cmpge << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16) | atoi (arg2);
} else if (! strcmp (opcode, "jalr")) {
num = (JALR << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16);
} else if (! strcmp (opcode, "bsf")) {
num = (bsf << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16);
} else if (! strcmp (opcode, "push")) {
num = (push << 22);
} else if (! strcmp (opcode, "pop")) {
num = (pop << 22);
} else if (! strcmp (opcode, "halt")) {
num = (HALT << 22);
} else if (! strcmp (opcode, "noop")) {
num = (NOOP << 22);
} else if (! strcmp (opcode, "bsr")) {
num = (bsr << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16);
} else if (! strcmp (opcode, "lw") ||! strcmp (opcode, "sw") ||
! strcmp (opcode, "beq") ||! strcmp (opcode, "jmae") ||
! strcmp (opcode, "jmnae") ||! strcmp (opcode, "jne")) {
/* if arg2 is symbolic, then translate into an address */
if (! isNumber (arg2)) {
addressField = translateSymbol (labelArray, labelAddress,
numLabels, arg2);
/*
printf ("%s being translated into %d\n", arg2, addressField);
*/
if (! strcmp (opcode, "beq") ||! strcmp (opcode, "jmae") ||! strcmp (opcode, "jmnae")) {
addressField = addressField-address-1;
}
} else {
addressField = atoi (arg2);
}
if (addressField < - 32768 || addressField > 32767) {
printf ("error: offset %d out of range\n", addressField);
exit (1);
}
/* truncate the offset field, in case it's negative */
addressField = addressField & 0xFFFF;
if (! strcmp (opcode, "beq")) {
num = (BEQ << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16)
| addressField;
} else if (! strcmp (opcode, "jmae")) {
num = (jmae << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16)
| (addressField);
} else if (! strcmp (opcode, "jmnae")) {
num = (jmnae << 22) | (atoi (arg0) << 19) | (atoi (arg1) << 16)
| (addressField);
} else if (! strcmp (opcode, "jne")) {
num = (jne << 22) | (addressField);
} else {
/* lw or sw */
if (! strcmp (opcode, "lw")) {