---
# System prepended metadata

title: CSPT10 Computer Architecture III

---

# CSPT10 Computer Architecture III
## Implementing PUSH, POP
```
import sys

# a simple data-driven machine that reads instructions out of memory
# and executes them

PRINT_HI = 1
HALT = 2
PRINT_NUM = 3
SAVE = 4 # save a value to a register
PRINT_REGISTER = 5 # prints the value of a register
ADD = 6 # adds values from two registers x, y and stores it in register x
PUSH = 7
POP = 8

def loadMemory():
    memory = [
        PRINT_HI, # prints hi
        SAVE, # saves 65 into register 2
        65,
        2,
        PUSH, # push value (65) in reg 2 to the stack
        2,
        SAVE, # save 99 into reg 2
        99,
        2,
        PUSH, # push value (99) in reg 2 to the stack
        2,
        POP, # pop value (99) and store it in reg 0
        0,
        PRINT_REGISTER, # print value stored in register 0 (99)
        0,
        POP, # pop value (65) and store it in reg 0
        0,
        PRINT_REGISTER, # print value stored in register 0 (65)
        0,
        HALT # stop the program
    ]
    spaceForStack = 128 - len(memory) # assume we have 128 bytes of memory only
    memory += [0] * spaceForStack
    return memory

memory = loadMemory()
pc = 0 # program counter - points to the instruction we're currently executing
running = True
SP = 7
registers = [0] * 8
registers[SP] = len(memory) - 1

while running:
    commandToExecute = memory[pc]

    if commandToExecute == PRINT_HI:
        print("hi")
        pc += 1
    elif commandToExecute == HALT:
        running = False
    elif commandToExecute == PRINT_NUM:
        numToPrint = memory[pc + 1]
        print(numToPrint)
        pc += 2
    elif commandToExecute == PRINT_REGISTER:
        reg = memory[pc + 1]
        print(registers[reg])
        pc += 2
    elif commandToExecute == SAVE:
        numToSave = memory[pc + 1]
        registerToSaveItIn = memory[pc + 2]
        registers[registerToSaveItIn] = numToSave
        pc += 3
    elif commandToExecute == ADD:
        regA = memory[pc + 1]
        regB = memory[pc + 2]
        sumOfRegisters = registers[regA] + registers[regB]
        registers[regA] = sumOfRegisters
        pc += 3
    elif commandToExecute == PUSH:
        # takes in a register number and saves the value in it onto the stack
        # decrement the stack pointer
        registers[SP] -= 1
        # store the value in the register onto the top of the stack
        registerToGetValueIn = memory[pc + 1]
        valueInRegister = registers[registerToGetValueIn]
        memory[registers[SP]] = valueInRegister
        pc += 2
    elif commandToExecute == POP:
        # pops the value from the top of the stack and stores it in a given register
        # read the value from the top of the stack
        topmostValueInStack = memory[registers[SP]]
        # store value to the given register
        registerToStoreItIn = memory[pc + 1]
        registers[registerToStoreItIn] = topmostValueInStack
        # increment stack pointer
        registers[SP] += 1
        pc += 2
    else:
        print("ehh idk what to do")
        sys.exit(1)

```

## Guided Project Pt. II
### ls8.py
```
import sys
from cpu import *

def main(argv):
    """Main."""

    if len(argv) != 2:
        print(f"usage: {argv[0]} filename", file=sys.stderr)
        return 1

    cpu = CPU()

    cpu.load(argv[1])
    cpu.run()

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv))
```
### cpu.py
```
def load(self, filename):
        """Load a program into memory."""
        address = 0
        with open(filename) as fp:
            for line in fp:
                comment_split = line.split("#")
                num = comment_split[0].strip()
                if num == '':  # ignore blanks
                    continue
                val = int(num, 2)
                self.ram_write(val, address)
                address += 1
```