2.3 A Virtual Machine for Simple

The virtual machine VM1, given in C source code vml. c, executes a code given in VM1 “assembly” language form. It has a live display, which shows contents of all the machine registers and some portion of the memory, for execution of each instruction. It uses POSIX threads and ncurses, a CRT text screen handling package, which optimizes the operations for repeated updates. See Fig.2.4 for a typical display.

 

                    VM1 Machine Simulator
IP:      10000 IR: ef000000 A:    2345 M :         0 BP: 0 SP: 2345
500000a   501000c     54e0001 600000d  506000d ef000000 0      0
      0         0        2345    1234        a     2345 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
      0         0           0       0        0        0 0      0
Output:         4                                         IX1: a
                                                          IX2: 0
                                                          IX3: 0

Fig.2.4 Typical display provided by VM1 simulator

 

The structure and the machine code for VM1 were already described in Section 2.1.2. The source code for the simulator, vml.c, is available elsewhere. The structure of the simulator is shown in Fig.2.5 .

 

Structure of VM1 simulator. Two threads are running – cpumem and display

 

Fig. 2.5 Structure of VM1 simulator. Two threads are running – cpumem and display

 

The following are the major functions in the simulator:

fetch( ) get the next instruction from the memory as per IP.

operand( ) get the operand (and its address) specified in the instruction fetched.

execute( ) execute the instruction just fetched with the operand obtained, then sleep for a couple of seconds.

load( ) used in the main( ) to load the machine code from a file.

display( ) display registers and memory contents and wait till execute ( ) is ready for the next display. This will be a few seconds before the next instruction.

 

The simulator reads in the program file, having one line per instruction and six fields containing hexadecimal values, separated by blank/s, as shown below:

<load addrss> <OP> <M> <RP> <R> <ADDRS>

and then immediately starts “executing” the program. The display changes dynamically as various values are put in the machine registers.

For our example program, the content of the machine program file is:

0100   15 0 0 0 000A
0101   06 0 0 0 0110
0102   06 0 0 0 0000
0103   05 0 0 0 0110
0104   12 0 0 0    1
0105   0A 0 0 0 FFFB
0106   FF 0 0 0 0100
0110   0 0 0 0 0

This is what an “experienced machine language programmer” would write. Compare it with the output code of the compiler.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.239.118