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
The homework tests will have the following format:
All tests will have debug instructions.
Grade | Tests |
---|---|
50% | stack and mathematical instructions |
20% | jump instructions |
10% | labels |
10% | register instructions |
10% | errors |
The homework has a lot of features that have to be implemented, we suggest you start like this:
no file
error\n
(\r\n
if using windows, make sure top change it before you commit to git) string using the split functionunknown instruction
errorpush
and pop
instructions using an array of numbers for the stackFor an additional 2p, implement a memory for the CPU. The simulator will receive another parameter in the command line that represents the number of memory locations available.
Implement the following memory instructions:
Instruction | Parameter | Details |
---|---|---|
write | - | Pops two numbers from the stack and writes the first one to the memory location defined by the second one |
read | - | Pops a number from the stack and reads a number from the memory location defined bu the popped number and pushes it to the stack |
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 quiestions repository with the title format [simulator] <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 questions repository and click Watch.