Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
77 views8 pages

Computer Systems Assignment 4 - Polish Evaluator

This document describes an assignment to write a program that evaluates expressions in Polish notation. The program will consist of several functions: - An eval function that evaluates a Polish expression and returns the result. - Functions for each operator (neg, add, sub, mul, div, mod) that evaluate that operation. - A strToInt function to convert string input to integers. The student is provided skeleton code and specifications for the required functions. They must implement the functions based on the given algorithms and comment blocks. The overall program will accept input from the user, call eval to evaluate it, and print the result.

Uploaded by

Kevin Wen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views8 pages

Computer Systems Assignment 4 - Polish Evaluator

This document describes an assignment to write a program that evaluates expressions in Polish notation. The program will consist of several functions: - An eval function that evaluates a Polish expression and returns the result. - Functions for each operator (neg, add, sub, mul, div, mod) that evaluate that operation. - A strToInt function to convert string input to integers. The student is provided skeleton code and specifications for the required functions. They must implement the functions based on the given algorithms and comment blocks. The overall program will accept input from the user, call eval to evaluate it, and print the result.

Uploaded by

Kevin Wen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Systems

Assignment 4 - Polish evaluator

May 20, 2011

1 Aim
The aim of this practical is to make you more familiar with calling sequences that use the stack both to pass
parameters and to allocate local variables. Calling sequences are very important, as is stack management. You
must understand this material if you intend to study third year computer courses such as Computer Architecture,
Compiler Construction, or Operating Systems.
You will also be making your program from multiple source files that can be linked together with some library
modules we provide. This will give you experience in seeing how the process of linking works.
To reinforce your understanding of arithmetic overflow, your program is required to detect and report situation
where overflow occurs.

2 Description of the exercise


The program you will write is a simple integer desk-calculator that accepts input in Polish notation (not reverse-
Polish notation).

2.1 Polish notation


Polish notation is named after the Polish mathematician Lukasiewicz, who first used it, almost as an afterthought,
in a small mathematics book that was published in the 1920s.
In normal mathematical notation (also known as infix notation), the operator of an expression (e.g. + − ×÷)
appear between the operands to which it applies, like this: 3 + 4, or 5 × 6.
In Polish notation (also known as prefix notation), an operator appears before its two operands. The previous
examples would thus be + 3 4 and × 5 6. (If you are familiar with older Hewlett-Packard calculators, you may
have seen reverse-Polish notation, where the operator comes last: 3 4 +.)
More complex expressions such as 3 + 4 × 5 can easily be handled: + 3 × 4 5. The × operator applies to 4 and
5, and the + operator to 3 and the result of the × operator.
In infix notation, we often need parentheses to make an expression clear, for example (4 × 5) + (6 ÷ 2). In Polish
notation, the intended meaning is always clear, so we never need parentheses. We simply write: + × 4 5 ÷ 6 2.

2.2 Operators you must handle


Your program must handle these operators:

operator meaning example answer


∼ neg ∼ 2 ans = −2
+ add +34 ans = 7
− sub −32 ans = 1
∗ mul ∗32 ans = 6
/ div /92 ans = 4
% mod / 11 4 ans = 3
Note:
• ∼ is the unary-minus (negate) operator;

1
• % is the modulo operator (The same as Java’s % operator.)

3 Overview of the task


This practical consists of writing several recursive procedures:

• A function named eval, that evaluates a polish expression;


• A number of simple functions, named neg, add, sub, mul, div, and mod, that evaluate the terms of a Polish
expression; and
• A strT oInt function to convert string input to an integer.

Each function will be written in DLX assembly code in a separate source file.
You will then link your functions with others we have provided to build a complete working program that accepts
input from the user, and prints the answer.
All subroutines must be relocatable, and must use .import and .export directives to permit linking. Use three
segment names: code, for instructions; constant, for any constant data; and stack, for the stack, which will hold
all variables. You will not need a data segment.

3.1 The main program


We have provided the main program, in a file named main. The main program reads in a line of text and stores it
in a LineBuf structure. It then calls eval, passing it the LineBuf. After eval returns, the program print the answer,
and repeats this sequence of steps indefinitely.

3.2 The LineBuf


The behaviour of the LineBuf is described by Java code in a file named LineBuf.java The lbRead subroutine
reads a line from the terminal into the LineBuf. lbSkipSPaces skips over spaces, and lbSkipChar skips over one
character. The current charater can be read by loading lb.nextCh from the LineBuf.
We have provided an implementation of the LineBuf subroutines, in a file named LineBuf.dls

3.3 Starting Files


The following files are provided for you:

init.dls A file containing stack initialisation code;


main.dls A file containing the main program;
Eval.java a file containing the algorithm for eval
evalSkeleton.dls A skeleton of the eval function;
negComments.dls a file containing the comments block for neg
addComments.dls a file containing the comments block for add
subComments.dls a file containing the comments block for sub
Mul.java a file containing the algorithm for multiplication
mulComments.dls a file containing the comments block for mul
divComments.dls a file containing the comments block for div
modComments.dls a file containing the comments block for mod
stringInt.dls A file contain string/integer conversion routines
stringIo.dls A file contain string input/output routines
unsDiv.dls A file containing an unsigned integer division subroutine;
io.dls A file containing the keyboard and display I/O routines;

4 What you need to do


You need to write the following subroutines:

2
4.1 Function eval
Write a function named eval that evaluates an expression in Polish notion and returns the result. A java version of
the eval algorithm is given in the file Eval.java
You eval function must conform to this comment block:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; eval Evaluate a Polish-notation expressions ;
; ;
; entry r14= SP ;
; 0(sp)= ->LineBuf ;
; jal eval ;
; ;
; exit r1= epression value ;
; r1= NIL if invalid expression ;
; ;
; uses r1,r2 ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If you need additional registers for your eval function, you must save and restore registers on the stack in the
prologue/epilogue. All temporary storage MUST be allocated on the stack.

5 Function neg
The neg function negates the value of the expression to its right. The algorithm for neg is given in the file Neg.java.
Your neg function must conform to this comment block:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; neg evaluate a NEG operation ;
; ;
; Entry r14= StackPointer ;
; 0(sp)= ->LineBuf ;
; jal neg ;
; ;
; Exit r1= sum ;
; ;
; Uses r1,r2 ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

5.1 Functions add, sub, mul, div, and mod


The functions add, sub, mul, div, and mod, perform (respectively) addition, subtraction, multiplication, division,
and modulodr. They each have a comment block similar to this one for add:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; add evaluate an ADD operation ;
; ;
; entry r14= SP ;
; 0(sp)= ->LineBuf ;
; jal add ;
; ;
; exit r1= sum ;
; r1= NIL if either operand was NIL, or result overflowed ;
; ;
; uses r1,r2 ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
You will need to work out the algorithms for these functions yourself.

3
6 Suggest plan of work
You can write your program in any order you like, but we suggest doing it in these stages:

6.1 Stage 1 — Build a Java solution


Before you write the program in DLX, it is a good idea to write the whole program in Java. That way you can see
how everything will go together, and how the program works. Time spent getting the algorithms right will be time
saved later during debugging.

6.2 Stage 2: Assemble, link, and locate


To complete the program, you will need to assemble each file separately, (to produce a relocatable object file), using
dasm. You must then link the pieces together, using dlnk (see appendix F of the CS notes), and finally relocate the
program prior to execution, using dloc (See appendix E of the notes).
You will need to create a suitable relocater address-file to tell dloc what to do. Once all this is done, you are
ready to use dsim to run the program.
Before you start to modify the program, you should make sure that you can do all the above things using the
dummy version of eval we have provided. The dummy eval simply returns the value 42 every time it is called.
If you have correctly performed the required steps, when you run the completed program it will print out a
prompt and wait for you type some input. As soon as you press return, the program will print: Ans= 42 on the
terminal, and then print a new prompt. This should happen every time you type input and press enter.
Once the program links and runs without error, you are ready to proceed. Do not proceed to stage 3 until your
program assembles and links and locates without error!

6.3 Stage 3 — Eval


You should now write the eval function. “Comment out” the calls in your eval to functions that don’t yet exist
(neg, add, sub, mul, div, and mod), so that you can run the program. Test that eval works correctly when given
an empty line of input) (just a <return> keypress.) The program should print: Unex[ected end of input, then
Ans= NIL, then a blnak line, and another prompt. Then try eval with a line containing spaces. It should print
exactly the same output for this cae.

6.4 stage 4 — Numeric input strT oInt


Now modify eval to call strT oInt (the function that converts a numeric string to an integer), and test your program
again.
You should now be able to enter numbers (such as 12), and the program will correctly type out the value you
have entered: Ans= 12. Test simple cases first (single-digit numbers), then more complex cases. Try typing in some
numbers with leading-spaces, and then a number.

6.5 Stage 5 — neg function


Implment the neg function, then modify evel to call it. Test your program with ∼ 1 <enter>. The program should
print: Ans= −1. Once that works correctly, try more complex expressions, such as: ∼∼ 12, which should print
Ans= 12, and ∼ ∼ 12. If all is well, the extra spaces will be ignored, and the result will still be: Ans= 12.

6.6 Stage 6: add function


Now write the add function, modify eval to call it, and test thoroughly.

6.7 Stage 7: sub function


The sub functions is very similar to add. You should find you can create it from the add function with just a small
amount of editing. Despite this, test it thoroughly!

4
6.8 Stage 8: mul function
Now it’s time to handle multiplication, and write the mul function. The basic algorithm for multiplication is:
//Algorithm assumes b>=0
private static int mul(int a, int b)
{
int p= 0;
while( b!=0 ){
if( (b&1) != 0 ){
p=p+a;
}
a= 2*a;
b= b>>1;
}
return p;
}
As before, write and thoroughly test your mul function.

6.9 Stage 9: div and mod functions


We have provided an unsigned divide function unsDiv, that you can use to implement both of these functions. The
div function works the same way as the java division operator (/).
The mod function works like Java’s (%) operator.
Once again, implement the functions, and test thoroughly.

6.10 Stage 10 — Bonus section


If you found all that straightforwrd, and seek a larger challenge, modify your code so that overflow is detected
whenever it occurs. Any function that detects overflow shoul return the value NIL (-2147483648).
You will find you have to think quite carefully to detect overflow in every situation.

7 Memory available for your program


The memory map of the computer system you are using is given in the diagram below. You will need this information
to make your program work correctly on the dsim simulator we have provided.
If you attempt to execute a load or store instruction that refers to an address in the regions marked non-existent
memory, dsim will stop, and report a non-existent memory error. The region marked read-only memory cannot be
used to store data, (for obvious reasons!).

5
Addresses Contents
FFFFFFFF
to non-existent memory
FFFFFF10
FFFFFF0C DspData register
FFFFFF08 DspCtrl register
FFFFFF04 KbdData register
FFFFFF00 KbdCtrl register
FFFFFEFF
to non-existent memory
01000800
010007FF
to Random-access memory
01000000
00FFFFFF
to non-existent memory
00004000
00003FFF
to Read-only memory
00002000
00001FFF
to non-existent memory
00000000

8 Commenting your code


Do not feel compelled to write a comment on every line! Sometimes doing simple things takes a few instructions,
and a single comment on the first instruction of the sequence is sufficient. Make sure your comments say what you
are doing, not how you are doing it (The instruction is how.). Do not explain the meaning of the instructions
themselves — a person reading your program is expected to know this already. Imagine a friend is going to read
your program. Try to include enough information so that your friend can understand what you are doing.

9 Detailed procedure for this Assignment


9.1 Step 1: Create the assignment directory
Open a terminal window on your machine, and cut-and-paste the following command (replace a1234567 with your
student number:

svn mkdir -m "Prac 4" https://version-control.adelaide.edu.au/svn/a1234567/11s1-cs-prac4

This command will create an empty directory named 11s1-cs-prac4 in your SVN repository.

9.2 Step 2: Check out a working copy of your assignment


In a directory on your system where you want the new directory to be locate, type this command:

svn checkout https://version-control.adelaide.edu.au/svn/a1234567/11s1-cs-prac4

A new directory, named 11s1-cs-prac4 will be created in your current directory. This directory looks empty, but
it actually contains a hidden file that SVN uses to manage your files.

9.3 Step 3: Download the starting files


There are a number of files that you will need to complete this assignment. Download them from the website into
the 11s1-cs-prac4 directory.

6
9.4 Step 4: Save the inital files
Change into (cd to) the directory 11s1-cs-prac4 and execute these commands:

svn add *
svn commit -m "inital commit"

SVN will display some messages as it adds the files to your repository.

9.5 Step 5: Edit your files


Edit the files as described earlier. As you work on your assignment, you should frequently commit the changes
you have made to your the repository. This protects you if your personal computer dies (the University has a very
reliable backup system that gurantess not to lose your files). It is also vital evidence if you are accused of plagiarism
— your repository will show that you are the original author, and someone else copied from you.)
To commit your files to the repository, change to the directory containing your files and type:

svn commit -m "message"

Where message is a a meaningful message that indicates what you just changed during the last editing session.
Committing changes is the only way your repository will get to know about the latest versions of your files.
When we mark your submission we will look at what has been committed to your repository and also, how often
and at what times you commited to your repository.

9.6 Step 6: Automated Marking


You can automatically mark your assignment by submitting it through the online web submission system.

10 Assessment
Marks will be awarded for clear and well formatted code, for intelligible and concise comments, and for getting
each stage of the program to work. When writing your program, try putting yourself in the position of another
programmer who has to understand what you have written and how it works. This should guide you in working
out how to format and comment your program.
We will use an automatic test procedure to verify that your program produces the correct results. You will lose
marks if your programs produce messages that are different from those shown in the sample output files. Provided
you don’t change the behaviour of the main program, you should find everything works correctly.

10.1 Marks
This assignment is worth 10% of the total mark for the Computer Systems course. Marks will be awarded via the
online web-submission system. If your program does not assemble correctly you will receive zero (0) marks. As you
have already seen, the test scripts are quite thorough — test your own program thoroughly!
If you do not pass all tests, the test script will offer hints to help you fix the problem.

10.2 Reasonable Attempt


For your submission to count as a reasonable attempt you must get eval, neg, and add working correctly, and
passing the relevant automatic tests.

10.3 Bonus marks


The bonus marks are only available to people who pass all the ordinary tests. Since testing for overflow is quite
tricky, there are two bonus marks available. The first mark will be awarded if your program passes a series of
simpler tests, and the second mark if you pass more extreme tests.

7
10.4 Handin Date
This assignment is due 9am on Monday 6th June. If your submission is late the maximum mark you can obtain
will be reduced by 25% per day (or part thereof) past the deadline.

David Knight
19-May-11

You might also like