CS4121代做、代写C++语言编程
CS4121 Cminus Expression Evaluation Compiler Project

Due Date: Tuesday, June. 3, 2025 at11:59 am

Purpose

The purpose of this project is to gain experience in giving meaning to a programming language by generating

Mips assembly for a subset of Cminus. Specifically, you will be generating assembly for integer I/O operations,

integer arithmetic and logical expressions and assignment statements.

Project Summary

In this project, you will add actions to a parser provided by me. You must add code to do the following:

1. Assign space for global and local integer variables declared in a Cminus program.

2. Generate assembly pseudo-ops for string constants used in a Cminus program.

3. Generate assembly to print string constants.

4. Generate assembly to print integers.

5. Generate assembly to read integers.

6. Generate assembly to compute integer expressions.

7. Generate assembly to compute logic expressions.

8. Generate assembly to assign values to integer variables.

Prologue and Epilogue Code

Since you are converting a Cminus program to Mips assembly, you must begin each assembly file with any

data declarations and a declaration of where themainprogram begins. This is done with the following code:

.data

.newl: .asciiz "\n"

.text

.globl main

main: nop

This code declares a data section with a string.newlthat is just the newline character, followed by a text

section (instructions) containing a declaration of the main routine. Each Mips assembly file should begin

with this sequence. Any space that you allocate for strings or floating-point constants in the static data area

may be allocated with directives after the .data directive and before the .text directive.

Assigning Variable Space

Memory for global variables declared in a Cminus program will be address as an offset off of$gp. The

register$gppoints to the middle of a 64K region in the static data area. You may address this area as a

positive or negative offset off of$gp. I will guarantee that you will need no more than 64K of space in the

static data area for any input program for any of the projects in this class.

Memory for local variables is allocated on the stack. Each integer requires four bytes of space. Local space

is allocated by adjusting the stack pointer the requisite number of bytes. Since stacks grow in the negative

direction in memory, space is allocated by subtracting from the stack pointer. The Cminus declarations

1

int i,j,k;

require 12 bytes of space. That space is allocated on the stack with the instructions

sw $fp, ($sp)

move $fp, $sp

sub $sp, $sp, 12

which should be placedimmediatelyfollowing the prologue code. The first instruction stores the old frame

pointer. The second sets the new frame pointer,$fp, and the third instruction allocates the space for the 3

variables.

String Constants

A Cminus program may use string constants in write statements. These constants are declared in the data

section using the.asciizpseudo-op. For the Cminus statement,

write( Hello );

The following declaration must be added to the data section of the assembly file:

.string0: .asciiz "Hello"

The label.string0is implementation dependent. You may name your string constants however you wish.

Printing Strings

Printing strings requires using a system call. The system call service for printing strings is 4. Since a

character string is stored in memory, you must pass the address of the string to the system call in register

$a0. As an example, the code to implement thewritestatement in the previous section would be:

la $a0, .string0

li $v0, 4

syscall

Note that you will need to additionally print the newline character when printing any data.

Printing Integers

Printing integers is similar to printing strings except that the actual integer is passed to the system call

rather than an address and the system call service is 1. As an example, to implement the statement:

write(7);

the following Mips assembly would need to be generated:

li $a0, 7

li $v0, 1

syscall

Reading Integers

To read an integer, the system call service is 5. The read value is returned in register$v0. Thus, to read an

integer, the following instructions are needed:

li $v0, 5

syscall

2

Accessing Variables

You may access local variables by loading them from an offset of the frame pointer ($fp). As an example,

assuming that the variableais assigned the second 4 bytes of local space. The following code might be

generated to accessa:

sub $s0, $fp, 4

lw $s1, 0($s0)

Loading a global variable is similar except that we use$gpinstead of$fp. Ifais a global variable that is

store 8 bytes (in the positive direction) off of$gp, the following code might be generated to access it:

add $s0, $gp, 8

lw $s1, 0($s0)

Integer Arithmetic Expressions

In Mips assembly, all operations are done on registers. The best way to generate code is to store all interme-

diate values in Mips registers. Using the registers$s0, ..., $s7,$t0, ..., $t9should be sufficient. You

should not need any other temporary registers. For an operation, the operands should all be put into regis-

ters, a result register should be allocated, the operations should be performed and then the input registers

should be released to be reused later. As an example, the statement

write(a+b);

might result in the code (ifais the first declared variable andbis the second)

lw $s1, 0($fp)

sub $s0, $fp, 4

lw $s2, 0($s0)

add $s3, $s1, $s2

move $a0, $s3

li $v0, 1

syscall

Logic Expressions

Logic expressions are similar to arithmetic expressions. For the mips, the value forfalseis 0 and the value

fortrueis 1.

Storing Integer Variables

To store a value in a variable, first compute the address and then store the value into that location. For

example, the statement

a = 5;

could be implemented with

li $s0, 5

sub $s1, $fp, 4

sw $s0, 0($s1)

3

Exiting the Program

Theexitstatement in Cminus can be implemented in Mips assembly as follows:

li $v0, 10

syscall

These instructions call the system routine that exits a program. Everymainroutine in a Cminus program

will end in anexitstatement.

Requirements

Write all of your code in C or C++ . It will be tested on a CS machine and MUST work there. You will

receive no special consideration for programs which work elsewhere, but not on a CS machine.

Input.The fileCminusProject2.tgzcontains the parser need to begin this project. You will need to

modifiy the actions in the project fileparser/CminusParser.yto do this project. Currently, the actions

just emit the rules that are reduced. Sample input for this project is provided in the project directoryinput.

To run your compiler, use the command

cmc .cm

To execute the resulting assembly file, use the Mars simulator(http://courses.missouristate.edu/KenVollmar/mars/).

Submission.Your code should be well-documented. You will submit all of your files, by tarring up your

project directory using the command

tar -czf CminusProject2.tgz CminusProject2

Submit the fileCminusProject2.tgzvia the CS4121 Canvas page. Make sure you do a make clean of your

directory before executing the tar command. This will remove all of the .o files and make your tar file much

smaller.

Data Structures and DocumentationI have provided several C data structures for those who will

be programming in C. There are doubly linked list, symbol table and string manipulation routines in the

workspace directoryCminusProject2/util. The HTML Doxygen documentation for the provided code

is inCminusProject1/Documentation/html/index.html. You may ask me any questions regarding these

routines. You will not likely need any of these structures now, but you may want to familiarize yourself with

them. For those coding in C++, you may use STL.

codegen directoryI have created the codegen directory where the codegen.*, reg.* and other files are

store. You will add fuctions to generate instructions for different productions in parser and these functions

are in codegen.c. In addition, the register allocation management untily implimenation files are reg.c and

reg.h, which makes you use registers simple. Before generatging the compiler, you need issue make command

under the codegen direcetory.

Makefile StructureThe Makefiles for the project are set up to automatically generate make dependences.

In a particular directory (e.g.,parser), you may add new files for compilation by adding the source file name

to theSRCSvariable declaration on the first line of that directory sMakefile. For example, to add the file

newfile.cto be compiled in theparserdirectory, change the first line ofparser/Makefilefrom

SRCS = CminusScanner.c CminusParser.c

4

to

SRCS = CminusScanner.c CminusParser.c newfile.c

Nothing else needs to be done. Do not add source files to the root directoryCminusProject2as the make

files assume there are no source files in that directory.

If you would like to add your own subdirectory (e.g.,newdir) toCminusProject2, then change the line

DIRS = parser util

inCminusProject1/Makefileto

DIRS = parser util newdir

and the line

LIBS = parser/libparser-g.a util/libutil-g.a

inCminusProject1/Makefileto

LIBS = parser/libparser-g.a util/libutil-g.a newdir/libnewdir-g.a

Then, copyutil/Makefiletonewdir/Makefile. Finally, change theSRCSdeclaration innewdir/Makefile

to contain only the source files in that directory and change the line

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图