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 ]
 

Errors

The file that the CPU simulator executes might have errors. When the simulator encounters an error, it ignores that instruction and continues the execution with the following line.

The general error format is:

ERROR (instruction_name): error text

The errors have to be printed exactly like shown here

Error Meaning Example
unknown instruction the simulator found an instruction that is does not know
multi ; ERROR (multi): unknown instruction
stack underflow the simulator tries to execute an instruction that pops some values from the stack, but the stack does not have enough values
push 1
 mul ; ERROR (mul): stack underflow
unable to open file `filename` (`node error message`) the simulator tries to open a file that it cannot open
$ node index not_a_filename.asm
ERROR unable to open file filename.asm(ENOENT: no such 
file or directory, open 'filename.asm')
undefined label the simulator tries to jump to a label that is not defined
jump jumper ; ERROR (jump): undefined label jumper
invalid jump address the simulator tries to jump to an address (line number) that does not exist in the file
 jump 2 ; ERROR (mul): invalid jump address 2

Running the simulator

To run the simulator, a user will issue the command:

$ node index.js filename.asm

The simulator will read the instructions from the file given as a parameter and execute them.

If no parameter is given, the following message will be displayed:

$ node index.js
USAGE: node index.js filename.asm

The $ in the examples is the shell's prompt and is not part of the command

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.1615031826.txt.gz · Last modified: 2021/03/06 13:57 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