Homework 1 - Simulator

Information

Deadline: 21st of March, 23:55
Points: 1 point out of the final grade
Link: Homework 1
Late upload: 1p / day (maximum 2 days)

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

Grading Tests

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

Implementation advice

The homework has a lot of features that have to be implemented, we suggest you start like this:

  1. Read the input file into s string and implement the no file error
  2. Split the read input by \n (\r\n if using windows, make sure top change it before you commit to git) string using the split function
  3. Split each line by ' ' and extract the instruction and its parameters
  4. Implement the unknown instruction error
  5. Implement the push and pop instructions using an array of numbers for the stack
  6. Implement the math instructions
  7. Implement all the instructions

Bonus

For 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

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 (except \r?\n for splitting the input lines of the source)
  2. Source code needs to be indented (-0.1p)
  3. You need to explain in details in the Readme.md how you wrote the homework (-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 quiestions repository with the title format [simulator] <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 questions repository and click Watch.

alf/teme/tema_en_1.txt · Last modified: 2021/03/17 16:21 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