The purpose of this homework is to explore CalOS, an operating system written entirely in Python 3, and build a very primitive batch-driven multi-programming environment in CalOS.

Current State of the OS

Download the files from here. You should have 6 files: main.py, cpu.py, calos.py, and ram.py; and mult.asm and fib.asm.

Take time to inspect these files. You should understand the relationship between the classes, and the basics of the assembly language.

Do this...

Create a file answers.txt and answer these questions about the code.

  1. How many bytes of RAM does the virtual machine have by default?
  2. What are the registers that the virtual machine has?
  3. What arguments does the instruction JLZ take and what does the instruction do?
  4. When does the CPU check if an interrupt has occurred?
  5. What does the Monitor instruction S do?
  6. What does the Monitor instruction ! do?
  7. Can assembly language files contain comments? How about blank lines?
  8. What function is called to write bytes into memory?
  9. Do assembly language programs assume they are loaded into memory at certain locations?

Here is a sample run of the operating system where I

Mac52078:~/vtn2$ python3 main.py
Monitor: enter ? to see options.
MON> l 20 mult.asm
Main found at 20
Tape loaded from 20 to 32
MON> d 10
Enter value (. to end) [10]> 5
Enter value (. to end) [11]> 3
Enter value (. to end) [12]> .
MON> !
State of the CPU is:
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
---------------------------------------------------------------------------
MON> x 20
Executing code at [20]: mov 0 12
CPU 0: pc 21, reg0 0, reg1 0, reg2 0
Executing code at [21]: mov *10 reg2
CPU 0: pc 22, reg0 0, reg1 0, reg2 5
Executing code at [22]: jez reg2 31
CPU 0: pc 23, reg0 0, reg1 0, reg2 5
Executing code at [23]: mov *11 reg1
CPU 0: pc 24, reg0 0, reg1 3, reg2 5
Executing code at [24]: jez reg1 31
CPU 0: pc 25, reg0 0, reg1 3, reg2 5
Executing code at [25]: mov reg2 reg0
CPU 0: pc 26, reg0 5, reg1 3, reg2 5
Executing code at [26]: sub 1 reg1
CPU 0: pc 27, reg0 5, reg1 2, reg2 5
Executing code at [27]: jez reg1 30
CPU 0: pc 28, reg0 5, reg1 2, reg2 5
Executing code at [28]: add reg0 reg2
CPU 0: pc 29, reg0 5, reg1 2, reg2 10
Executing code at [29]: jmp 26
CPU 0: pc 26, reg0 5, reg1 2, reg2 10
Executing code at [26]: sub 1 reg1
CPU 0: pc 27, reg0 5, reg1 1, reg2 10
Executing code at [27]: jez reg1 30
CPU 0: pc 28, reg0 5, reg1 1, reg2 10
Executing code at [28]: add reg0 reg2
CPU 0: pc 29, reg0 5, reg1 1, reg2 15
Executing code at [29]: jmp 26
CPU 0: pc 26, reg0 5, reg1 1, reg2 15
Executing code at [26]: sub 1 reg1
CPU 0: pc 27, reg0 5, reg1 0, reg2 15
Executing code at [27]: jez reg1 30
CPU 0: pc 30, reg0 5, reg1 0, reg2 15
Executing code at [30]: mov reg2 12
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
Executing code at [31]: end
State of the CPU is:
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
---------------------------------------------------------------------------
MON> s 10 13
[0010] 5
[0011] 3
[0012] 15
[0013] 0
State of the CPU is:
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
---------------------------------------------------------------------------
MON> l 100 fib.asm
Main found at 100
Tape loaded from 100 to 125
State of the CPU is:
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
---------------------------------------------------------------------------
MON> d 50
Enter value (. to end) [50]> 6
Enter value (. to end) [51]> .
State of the CPU is:
CPU 0: pc 31, reg0 5, reg1 0, reg2 15
---------------------------------------------------------------------------
MON> x 100
Executing code at [100]: mov 1 150
CPU 0: pc 101, reg0 0, reg1 0, reg2 0
Executing code at [101]: mov 1 151
CPU 0: pc 102, reg0 0, reg1 0, reg2 0
Executing code at [102]: mov 1 500
CPU 0: pc 103, reg0 0, reg1 0, reg2 0
<... snip ...>

MON> s 500 510
[0500] 1
[0501] 1
[0502] 2
[0503] 3
[0504] 5
[0505] 8
[0506] 0
[0507] 0
[0508] 0
[0509] 0
[0510] 0
State of the CPU is:
CPU 0: pc 124, reg0 0, reg1 5, reg2 0
---------------------------------------------------------------------------

Do this...

Implement batch-mode, which will allow the operator to run a series of programs after having loaded them into memory. This "array" of program locations is terminated with the value 0. E.g., if you put fib.asm in memory at location 100, and mult.asm in memory starting at location 20, and you put the following values in location 40:

100
20
0


Then when you run this command:

r 40

the command should run the program at location 100 first, then when it ends, run the program at location 20, and then when it ends, the command ends.

  1. Add an instruction r <addr> to the Monitor menu which will run the programs whose starting locations are in memory starting at location <addr>. Add a method _run_batch(self, addr) that is similar to _run_program(), but puts the CPU in batch mode, etc., as described in the next step.
  2. You will have to alter the CPU class so that it can run in batch mode. It should store its mode as a boolean, set to False in the constructor. The class should also store the address where the list of programs is stored in memory. There should be a "setter" defined to set this value.
  3. You will have to add code to the CPU class so that if the CPU is in batch mode, it repeatedly calls _run_program() on each program in the list. Make sure you set the program counter and clear the registers before trying to run each program.

Honors students:

Change the code to process the l (load from tape) instruction such that each time a program is loaded its starting address is placed next in the array of starting locations. The r instruction will not need to take an <addr> argument -- you'll have to hard-code this location in the code. You will also need to add an instruction to "clear" the array of program starting points.

Checking In

Submit all the code and supporting files for this homework to /home/cs/232/current/<yourid>/homework01/. (Don't forget to submit your answers.txt file.)

We will grade this exercise according to the following criteria: (40 pts total)