This is an old revision of the document!
The purpose of this homework is to get you familiarized with the following concepts:
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.
The instruction set is divided into three instructions categories:
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 | |||
- | 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 ] |
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
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 |
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
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
The homework is individual. Any attempt of copying will result in 0p for the homework. Automated anti copying system will be used.
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.
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.
The homework will be tested automatically using a set of public and private 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.
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
When uploading the homework, we might have some private tests that it needs to pass. vmchecker will run them.
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.
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.
To upload your homework, please follow the following:
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