首页 > > 详细

program留学生讲解、C/C++编程语言辅导、讲解Java,Python 解析Haskell程序|辅导Python程序

Hmk5: Running target code
Macau University of Science and Technology
2019 FAll
Purpose
We will write a program that works as a virtual machine that can process a sequence of instructions
of P-code. Instructions of P-code have been introduced in class, which are based on a stack. The
above picture is found from the internet,
Preparation
The file p-code.pdf lists the instructions of P-code.
The P-code Machine
The runtime environment of a P-code virtual machine (PVM) has 5 parts.
Code area: It is an array of P--code instructions.
PC: A special place in memory, which records the address of the next instruction to be executed.
By default, PC = PC + 1, after the current instruction is executed. A special case is a jumping
instruction: when the current is a jump instruction to some label, and the corresponding labeled
instruction is at address j, then PC will be changed to j.
Data area: It is an array. Each slot in the array is dedicated to a name (variable or label), in the
slot is the value of the name. For a variable, its value is an integer (assume ). For a label, its value
is an address (index in the code area).
Symbol table: It is a data structure that supports the following operations:
lookup(x) : Given a name (a string) return its address in the data area. If x does not exists,
some special signal is returned. The names include variable names and label names.
insert(x) : Allocate a new slot in the data area for the value of the name x (x is not
initialized at the moment). A simple way is that the address of the next available slot of the
data area is reserved for x, and the address of x should be registered in the symbol table so
that x can be looked up later.
When should insert(x) be called?
When a name x is used by an instruction (lod or lda), if x is new (lookup(x) cannot find it), x is
inserted into the symbol table. When a label instruction is executed, the label should be
inserted into the data area, and its address should be registered in the symbol table.
stack: A stack is needed by the P-code instructions. It is an array of integer.
A stack remembers:
the stack top, which is an index in the array, it is the address where the latest data item is
saved. Data are added or removed from the stack at the top. Initially, when the stack is
empty, the top could be a special value like -1.
The stack supports 2 operations:
pop() : When the stack is not empty, return the data (integer) at the top, and top = top 1.
When the stack is empty, nothing is popped, and some error message is printed out.
push(c) : When the stack is not full, top = top + 1, and c is saved at the top in the data
area. When the stack is already full, nothing is pushed, and some error message is printed.
Behavior of the PVM.
Given a P-code program. The PVM executable file should open the program and executes the
instructions one by one. The output of of the wri instruction should be be printed on the screen. A

P--code program will stop when there is no more instruction to run, or the halting instruction ( stp ) is
executed.
How to submit
Submit before Monday, January 6, 2020.
Attach all of your source code file. If you created some P-code files ( .p files) for testing, also
attached them. Please use a file readme.txt to describe your work, like the achievements and
problems of your work, and how to compile and run your code
At most three students can form a group to do the homework together and submit the homework
together with an email. If you can do the homework by yourself (a one-person group), it surely
will be fine.
Submit the homework by sending an email (to must_learn_c@126.com ) with attached program
files (together with the file of the grammar rules). The details of how to submit your homework is
in the file how_to_submit_homeworks.pdf .
References
1. Kenneth C. Louden. Compiler Construction Principles and Practice. PWS Publishing Company,
1997. ISBN 0-534-93972-4 http://www.cs.sjsu.edu/~louden/cmptext/.
2. Andrew W. Appel. Modern Compiler Implementation in C: Basic Techniques. Cambridge
University Press, New York, NY, USA, 1997.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!