Design and Construction of a PC-Based Stack Machine Simulator for Undergraduate Computer Science & Engineering Courses
Total Page:16
File Type:pdf, Size:1020Kb
Design and Construction of a PC-Based Stack Machine Simulator for Undergraduate Computer Science & Engineering Courses Fitratullah Khan and Sohail Anwar Department of Computer Science The University of Texas at Brownsville Brownsville, Texas 78520 Abstract - A senior level compiler design course in an top of the stack. The instructions pop operands from the stack undergraduate computer science and engineering program and push results back on to the stack. usually deals with teaching the students the basics of compiler The stack machine designed by the authors consists of a construction. A thorough understanding of the grammar of a code segment, a stack segment, an Arithmetic Logic Unit formal language and a compiler designed for it can only be (ALU), and four address registers. The code segment has the truly obtained by writing a working compiler for the language. program to be executed. The stack segment holds the A semester long feat of constructing the lexical, syntactic, intermediate data and addresses generated during program semantic, and code generation phases of a compiler exposes execution. A portion of the stack is set aside to store global the students to the inner workings of the compiler. The final data as well. The four address registers are pointers into the phase of testing the integrity and effectiveness of the code and stack segments: constructed compiler is both important and rewarding for a ! Instruction Pointer (IP) points to the next student. Furthermore, since the impetus of such an instruction to be executed, undergraduate course is to deal with the issues of compiler ! Stack Pointer (SP) points to the valid item on top construction rather than intricacies of different machines, it is of the stack, instructive to generate the code for a simple stack machine, ! Local Scope Pointer (LSP) points to the local incorporating a hardware stack, rather than dealing with a data declared within a procedure and the register-based machine such as a microcomputer. However, parameters passed to the procedure, and the educational institutions have the latter as a well ! Parent Scope Pointer (PSP) points to the data established computing platform. Therefore, for testing a defined in the outer static scope. constructed compiler for a stack machine, it is feasible to make Each of the segments is 64KWords, where a word is 16 a microcomputer-based stack machine simulator. This paper bits. Hence, each segment is 64K deep and 16-bit wide, and presents the working design of a PC-based stack machine the addresses of the locations within a segment range from simulator developed by the authors for use in an undergraduate computer science and engineering course in compiler design. This paper begins by putting forward the architecture of the virtual stack machine for which the simulator is designed and constructed. The stack machine’s instruction set is explained and then followed by a discussion of its encoding scheme. The main data structures and the different stages of the simulator are elaborated upon, culminating in a description of the salient features of the simulator that make it an effective tool in a compiler construction course. This includes the simulator’s different analytical features such as measuring space-time complexity to analyze the effectiveness of a compiled code. The Architecture of the Virtual Stack Machine The stack machine simulator presented in this paper is a Virtual Stack Machine (VSM) running on a Personal Computer (PC). Typically, a VSM has a program memory and a data stack [1]. The program instructions reside in the program memory, and the data that the instructions operate on are available from the Figure 1 Architecture of the VSM 0000H (Hexadecimal) to FFFFH. Each register is 16-bit wide It is important to note that LSP points to the local scope to address any location within the 64K range of a segment. and can be pushed on the stack to create a dynamic link among Figure 1 shows the architecture of the VSM. Note that all the stack frames of the procedures called dynamically [5]. In address and data paths are 16-bit wide. The instruction pointer Figure 2, this chain of dynamic links is depicted by the saved points within the code segment. Similarly, the stack and scope old value of LSP on top of the return address. This is typically pointers only point to locations within the stack segment. The done by pushing LSP before setting LSP to point to the local globally defined data are also made to reside within the stack scope of the procedure. segment rather than having a segment of their own. This Note that the memory of the VSM is a word addressable design approach provides a set of simple data structures upon rather than a byte addressable memory. All operations and which to build the stack machine simulator. instructions are in units of words. In case of an Input/Output The stack grows from high memory to low memory as is (I/O) operation involving characters, the least significant byte the case in Intel X86 architecture [2]. The stack pointer points contains the character, whereas the most significant byte has to the valid item on top of the stack. Hence, a push operation zeros. Besides providing simplicity and uniformity, this first decrements the stack pointer by 1 and then the specified approach provides 64KWords of memory rather than 64KBytes operand is pushed on to the stack. The pop operation first pops with 16-bit pointers. the contents of the location pointed by the stack pointer and then increments the stack pointer by 1. Instruction Set of the Virtual Stack Machine The global data are stored in the high memory region in the stack segment starting from FFFFH. The normal stack Generally, an assembly language program for a stack machine starts where the global variables end. PSP points to the last is comprised of arithmetic, logical, input/output, control, stack location containing a global variable, thereby demarcating the altering, and assembler directive instructions. Table I two regions, global data and normal stack. summarizes the complete instruction set of the designed VSM. Figure 2 shows a snapshot of the stack illustrating the Note that the assembler directives are not part of the machine’s functions of SP, LSP, and PSP. The figure shows the possible instruction set. However, these directives are found in an values of the stack after a procedure, with two passed assembly language program written for the VSM. They have parameters and one local variable, is called. The stack frame been included in the table for completeness. or the activation record of the called procedure shown is It is important to note that the result of some binary operations, such as subtraction and division, are sensitive to the order of operands on the stack. The description of such instructions in Table I explicitly names the operands to state the expected order. For example, in case of a division operation the divisor is at the top (lower memory address) of the stack whereas the location below it (higher memory address) has the dividend [5]. Similarly, in case of a subtraction operation, the lower address has the subtrahend and the higher address the minuend. POPI and RTI are dereferencing type of operations in which an operand becomes the address of another operand. This is also called indirect addressing [6]. These operations are mostly required when dealing with arrays and variables passed by reference rather than passed by value [1]. Instruction Encoding Figure 2 A Snapshot of the Stack The instructions of the VSM are either two words wide or one word wide. The two-word instructions are those that require an actually a function of the execution of a set of assembly address, offset, or an immediate value. Table II shows the language instructions either generated by a compiler or directly machine code for each instruction. Note that the opcode of an coded by a programmer [4]. Therefore, it is up to the compiler instruction always appears as the Least Significant Word or the programmer to adhere to the intended use of different (LSW). For a two-word instruction, the Most Significant Word registers. For example, LSP can be used to point to global data (MSW) contains the address, offset, or an immediate value. and PSP to local scope instead, however, that is not the intended use of the registers. Table I Instruction Set of the Virtual Stack Machine Instruction Description (all operands and results are on stack unless otherwise specified) Directives: end Physical end of the program (not a machine instruction) exit Halt the execution of the program and exit (not a machine instruction) Arithmetic: add Pop the top two locations, add, and push the result div Pop the dividend and divisor, divide, and push the quotient and then the remainder mlt Pop the multiplicand and multiplier, multiply, and push the result neg Pop the top location, negate, and push the result sub Pop subtrahend and minuend, subtract, and push the result Logical: and Pop the top two locations, perform bitwise AND, and push the result not Pop the top location, perform bitwise NOT, and push the result or Pop the top two locations, perform bitwise OR, and push the result Input/Output: getc Get a character byte from the standard input, pad it with zeros on the left, and push it geti Get a 16-bit integer from the standard input and push it putc Pop the top location and print the least significant byte as a character on the standard output puti Pop the top location and print it as a 16-bit integer on the standard output Control Flow: call label