This is an old revision of the document!


Homework 1 - CPU Simulation

Information

Deadline: 21st of March, 23:55
Points: 1 point out of the final grade
Link:
Late upload: not allowed

Purpose

The purpose of this homework is to get you familiarized with the following concepts:

  1. basic TypeScript development
  2. use of data structures in TypeScript
  3. use of command line parameters
  4. reading data from files
  5. basic string splitting and processing
  6. the way a simple CPU works

What do you have to do

The purpose of the homework is to simulate a CPU. This is a very simple CPU, it has one register, called R, and an infinite stack space, called STACK. The CPU performs all computation using signed integer numbers. It does not know how to handle floating point numbers.

Instruction Set

The instruction set is divided into three instructions categories:

  1. No Action - these instructions do not perform any action
  2. Memory - these instructions allow the CPU to interact with the stack (the memory)
  3. Math - these instructions allow the CPU to perform mathematical operations
  4. Flow - these instructions allow the CPU to perform if's and loops
  5. Pseudo Debug - these instructions are used for debugging
Instruction Parameters Description Example
No Action
nop any parameters Does nothing, it is just used for comments
 nop
 
Memory
push integer number Pushes the parameter to the stack
 ; stack is [ ]
 push 1 
 ; stack is [ 1 ]
 push 2
 ; stack is [ 1 2 ]
 
pop - Pops a number from the stack, in other words it deletes a number from the stack
 ; stack is [ 1 2 ]
 pop 
 ; stack is [ 1 ]
 
load - Pops a number from the stack and stores it into R (the CPU register)
 ; stack is [ 1 2 ]
 load 
 ; stack is [ 1 ]
 ; R is 2
 
store - Pushes the number from R (the CPU register) to the stack
 ; stack is [ 1 ]
 ; R is 2
 store 
 ; stack is [ 1 2 ]
 ; R is 2
 
Math
add - Pops two numbers from the stack, adds them and adds the result to the stack
 push 1 
 push 2
 ; stack is [ 1 2 ]
 add 
 ; stack is [ 3 ]
 
sub - Pops two numbers from the stack, subtracts them and adds the result to the stack
 push 1 
 push 2
 ; stack is [ 1 2 ]
 sub 
 ; stack is [ -1 ]
 
mul - Pops two numbers from the stack, multiplies them and adds the result to the stack
 push 1 
 push 2
 ; stack is [ 1 2 ]
 mul 
 ; stack is [ 2 ]
 
div - Pops two numbers from the stack, divides them using integer division and adds the result to the stack
 push 1 
 push 2
 ; stack is [ 1 2 ]
 div 
 ; stack is [ 0 ]
 
mod - Pops two numbers from the stack, computes the remainder of their divisionand adds the result to the stack
 push 5 
 push 3
 ; stack is [ 5 3 ]
 mod 
 ; stack is [ 2 ]
 
Flow
jump integer number / label Instead of executing the next instruction, the CPU will take the instruction: a) at the line number specified as parameter or b) at the label specified by the parameter
 ; example with line 
 push 5 ; line 1
 jump 5 ; line 2
 push 3 ; line 3 - skipped
 push 2 ; line 4 - skipped
 push 2 ; line 5 - jump here
 mul
 ; stack is [ 10 ] 
 ; example with label
 push 5 
 jump two 
 push 3 ; skipped
 push 2 ; skipped
 two: push 2 ; label two
 mul
 ; stack is [ 10 ]
 
jumpz integer number / label If the top value from the stack is zero, it works the same way as `jump`, otherwise it does nothing
 ; example ignore
 push 5  
 jumpz two ; ignored, stack top is 5
 push 3 
 push 3 
 two: push 2 
 mul
 ; stack is [ 5 3 6 ] 
 ; example jump
 push 0  
 jumpz two ; jumps, stack top is 0
 push 3 ; skipped
 push 3 ; skipped
 two: push 2 
 mul
 ; stack is [ 0 ]
 
jumpnz integer number / label If the top value from the stack is not zero, it works the same way as `jump`, otherwise it does nothing
 ; example ignore
 push 5  
 jumpz two ; jumps, stack top is not 0
 push 3 ; skipped
 push 3 ; skipped
 two: push 2 
 mul
 ; stack is [ 10 ] 
 ; example jump
 push 0  
 jumpz two ; ignored, stack top is 0
 push 3 
 push 3 
 two: push 2 
 mul
 ; stack is [ 0 3 6 ]
 
Pseudo Debug
print - Prints the number from the top of the stack
 ; stack is [ 1 2 ]
 print ; prints 2
 
stack - Prints the stack
 ; stack is [ 1 2 ]
 stack ; prints [ 1 2 ]
 

An example of calculating 3 to the power of 2 is:

node main.js 3 2 pwr
# this will write to the screen
9
  1. Make a new NodeJS project
  2. Make a package.json file containing your name as author, any version you want and the name of the main file (ex: main.js, homework.js, calculator.js …)
  3. Write the calculator having the main file the one written in package.json

Operations

Help

The help operation (this means running your program with the parameter help or without any parameter, will print the string 'Calculator, Author: <your name here, the same as the one in package.json>'.

For printing the logo, use the cowsay library.

An example:

node main.js
# this will print
 _________________________
< Calculator, Author: ALF >
 -------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  help
  +
  -
  mul
  div
  mod
  sq
  abs
  pwr
  sort
  rev
  max
  min
  uniq
  cos
  sin

Errors

The calculator has two possible errors:

  1. the command was not found
  2. the number of parameters is incorrect

If you run

node main.js help
# this will print (exactly)
ERROR: this command does not exist, use help to see available commands
 
node main.js 10 mul
# this will print (exactly)
ERROR: multiply command uses at least 2 parameters

Bonus

For an additional 0.25p, implement the calculator to work for complex numbers for addition, subtraction and multiply. The input format will be:

#add a1+b1i with a2+b2i
node main.js a1 b1 a2 b2 complex +
 
#subtract a1+b1i with a2+b2i
node main.js a1 b1 a2 b2 complex -
 
#multiply a1+b1i with a2+b2i
node main.js a1 b1 a2 b2 complex mul

Bonus will be awarded only if all other tests pass.

Rules

  1. You may not use RegEx or any other library that would use regular expressions
  2. Files must have “use strict”
  3. You files need to pass jshint (with node: true, esnext: true)
  4. Source code needs to be indented (-0.1p)
  5. You need to write a file named Readme and explain how you wrote the homework (-0.1p)

Copying

The homework is individual. Any attempt of copying will result in 0p for the homework. Automated anti copying system will be used.

Questions

If you have any questions related to the homework, please ask them by posting an issue on the github repository with the title format [calculator] <your question title>. You will need a github account for that.

DO NO POST ANY CODE. This is considered copying and will result in a 0p homework for you.

If you want to receive an email when issues are posted or when there are new messages, got to the github repository and click Watch.

Testing

The homework will be tested automatically using a set of public and private tests.

Public Tests

You can download the public tests from the GitHub repository.

To run the tests, download the contents of the repository in the folder with the homework. Enter the Devoirs/calculator/verify folder and run ./run_all.sh .. .

Copy the homework contents into the Devoirs/caclulator folder.

cd Devoirs/calculator/verify
rm -rf node_modules
./run_all.sh ..

You will need bash for that. You can use either Linux or Windows Linux Subsystem.

To install nodejs in Linux or Windows Linux Subsystem, do the following:

wget https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz
tar xvfJ node-v10.15.1-linux-x64.tar.xz
cd node-v10.15.1-linux-x64
sudo cp -R -v * /usr

Private Tests

When uploading the homework, we might have some private tests that it needs to pass. vmchecker will run them.

You may always upload the homework as many times you want until the deadline. This will run all the tests for you and display the result.

Upload the homework

The homework needs to be uploaded to vmchecker. Login with your moodle user name, select the Automates et Langages Formelles (FILS) course and upload the homework archive.

Readme

The readme file has the following format:

Your full name
Group

An explanation how you wrote your homework, what did you use, what are the main ideas.

Homework Archive

To upload your homework, please follow the following:

  1. Create a zip (not rar, ace, 7zip or anything else) archive containing:
    • your main javascript file (the same as specified in package.json)
    • your javascript files (*.js)
    • the package.json file
    • the Readme file
  2. sign in with vmchecker
  3. select the Automates et Langages Formelles (FILS) course
  4. select 1. Calculator
  5. upload the archive

The archive needs to contain the files in its root, not in a folder. DO NOT archive a folder with the files, archive DIRECTLY the files.

DO NOT include node_modules.

When the archive is uploaded, vmchecker will run:

unzip archive.zip homework
cd homework
npm install
echo '{ "node":true, "esnext":true }' > .jshintrc
jshint *.js
alf/teme/tema_en_1.1615030376.txt.gz · Last modified: 2021/03/06 13:32 by alexandru.radovici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0