- •Course project handbook on “Programming” Donetsk, DonNtu, 2012
- •Contents
- •Introduction
- •1 Technical task
- •Technical task
- •2 Analyses of virtual computer
- •2.1 General architecture
- •2.2 Memory organizations. Data formats. Addressing modes
- •3 Programming in code of virtual computer
- •3.1 Programming in code of One-address Virtual Computer
- •3.2 Programming in code of Two-address Virtual Computer
- •3.3 Programming in code of Three-address Virtual Computer
- •4 Simulation program developing
- •4.3 Alu simulation and visualization
- •Void alu(int cop)
- •Appendix a general requests to the reports’ preparing
- •1. Program documentation
- •1.2. Programmer’s Manual
- •Formal report example
- •Title Page example
3.3 Programming in code of Three-address Virtual Computer
y=a*b+d*c-f, where
a=5=5h=00000101
b=4=4h=00000100
c=20=14h=00010100
d=-5=-5h=11111011
f=-20=-14h=11101100
Example for VC: 32 bits memory word, 3-address computer, 256 words=>8 bits address
Operation: (A1)op(A2)=>A3
7…….0 |
31…28 |
27…24 |
23…16 |
15…….8 |
7……….0 |
Bit’s numeration |
Data definition: integer (fixed point notation), long (32 bits-double word), in BRC (binary radix complement or 2’d complement) | ||||||
10 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0000 0101 |
a=5 |
11 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0000 0100 |
b=4 |
12 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0001 0100 |
c=20=14h |
13 |
1111 |
1111 |
1111 1111 |
1111 1111 |
1111 1011 |
d=-5 |
14 |
1111 |
1111 |
1111 1111 |
1111 1111 |
1110 1100 |
f=-20 |
15 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0000 0000 |
y=? |
16 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0000 0000 |
w=? |
Address (hexadecimal) |
Code of Operation |
Mode |
A1 |
A2 |
A3 |
Comments |
20 |
0011 |
0000 |
0001 0011 |
0001 0010 |
0001 0110 |
d*c=>w |
21 |
0011 |
0000 |
0001 0000 |
0001 0001 |
0001 0101 |
a*b=>y |
22 |
0001 |
0000 |
0001 0101 |
0001 0110 |
0001 0101 |
y+w=>y |
23 |
0010 |
0000 |
0001 0101 |
0001 0100 |
0001 0101 |
y-f=>y |
24 |
0000 |
0000 |
0000 0000 |
0000 0000 |
0000 0000 |
End (stop) |
Characteristics:
Data volume Vd=7*32 bits = 28 bytes.
Program volume Vp=5*32 bits = 20 bytes.
Time of execution Tex=2(+/-)+2(*)+12 (access to memory).
4 Simulation program developing
Program to simulate the working of Virtual Computer can be written in any language. C++ programming language supports graphical output, different data types, classes and so. That is why the application of C++ is preferable.
4.1 User Interface
UI should support:
- main menu;
- menus for all modes;
- keyboard control.
Example below shows the main menu and keyboard control.
void main()
{
textbackground(0);
clrscr();
char ch='0';
while(ch!=27) //code of ESC
{
menu();
request();
ch=getch();
switch(ch)
{
case '1': about(); break;
case '2': load(); break;
case '3': mem(); break;
case '4': alu(1); break;
default: if (ch!=27)
{ window(30,10,50,13);
textbackground(GREEN);
clrscr(); gotoxy(2,2);
cprintf("Check key!!");
}
}
}
}
void menu()
{
window(1,1,80,3);
textbackground(BLUE);
clrscr();
textcolor(YELLOW);
gotoxy(2,2); cprintf("1 - about");
gotoxy(15,2); cprintf("2 - load file");
gotoxy(30,2); cprintf("3 - show memory");
gotoxy(50,2); cprintf("4 - show ALU");
gotoxy(70,2); cprintf("ESC - exit");
}
void request()
{
window(1,24,80,25);
textbackground(RED);
clrscr();
textcolor(YELLOW);
gotoxy(20,1); cprintf("Select mode and press corresponding key:");
}
Figure 4.1 – Example of a start window with a main menu and request
4.2 Memory simulation
There are 4 typical actions For memory
At first it’s necessary to define length of memory word, for example, 13 bits.
#define NW 13
Memory can be defined as array of characters:
char MEM[256][NW];
After this program for virtual computer have to be loaded to file, for example, with a name test1.dat. Let structure of file is:
- the first line consists 2 decimal numbers: start address of data (10h = 16) and number of data (5);
- next 5 lines – data in binary code: 7, 9, 10, -10 (for negative numbers a BRC is used), free memory word to store a result;
- the next line consists 2 decimal numbers: start address of program (20h = 32) and number of instructions (4);
- next 4 lines – instructions in binary code, last instruction (code of operation = 0) – STOP.
16 5
0000000000111
0000000001001
0000000001010
1111111110110
0000000000000
32 4
0001000010000
0011000010001
0010000011110
0000000000000
Let define working identifiers:
PC – start address of program;
DPTR – start address of data;
l_CSEG – number of instructions;
l_DSEG – number of data;
int PC,DPTR,l_CSEG,l_DSEG;
Function below shows an example of input test from a file to memory.
void inpt(FILE *f)
{
int i,j;
char ch;
fscanf(f,"%d",&DPTR);
fscanf(f,"%d",&l_DSEG);
for(i=0;i<l_DSEG; i++)
{
fscanf(f,"\n");
for(j=0;j<NW;j++)
{ ch=fgetc(f);
MEM[DPTR+i][j]=ch;
}
}
fscanf(f,"%d",&PC);
fscanf(f,"%d",&l_CSEG);
for(i=0;i<l_CSEG; i++)
{
fscanf(f,"\n");
for(j=0;j<NW;j++)
{ ch=fgetc(f);
MEM[PC+i][j]=ch;
}
}
}
#include <stdio.h>
#include <conio.h>
#define NW 13
int PC,DPTR,l_CSEG,l_DSEG;
char MEM[256][NW];
#include "inpt.cpp"
#include "mem_disp.cpp"
void main()
{ FILE *f1;
f1= fopen("test1.dat","r");
fr= fopen("Rez.dat","w+");
inpt(f1);//input test1
mem_disp(); //show CSEG and DSEG
getch();
fclose(f1);
}