Highlights
Overview
Each student is asked to implement a simulated CPU for processing a simple machine language.
Objectives
Detailed Description
Introduction
Programs run on computers by having the hardware (or system software) execute basic operations called instructions. Many languages (such as Java) represent the program to be executed as byte codes which are very similar to machine instructions. In this assignment, you will build a simulator to process a simple machine language.
At their lowest level, computers operate by manipulating information stored in registers and memory. Registers and memory are like variables in C or Java; in fact inside the computer that is how variables get stored. In addition to data, memory also stores the instructions to execute. The basic operation of a computer is to read an instruction from memory, execute it and then move to the instruction stored in the next memory location. Typical instructions will read one or more values (called operands ) from memory and produce a result into another register or memory location. For example, an instruction might add the values stored in two registers, R3 and R4 and then store the result in the register R5. Other instructions might just move data from one place to another (between registers or between a register and a memory location). A final type of instruction, called a branch instruction , is used to change what instruction is executed next (to allow executing if and looping statements).
The Simulated CPU
The simulated computer has a 16-bit word size and a memory that contains 2 1 1 (2048) bytes of memory.
In addition to memory, the computer has 16 registers that can be used to hold 16-bit values, plus a program counter. The PC register is the "Program Counter" and always contains the address of the next instruction to execute. The PC cannot be read or written like a normal register, but can only be modified using special instructions: Branch
(B) and Branch-if-EQual (BEQ), any other attempt to modify it is an Invalid Instruction (including using it as the register value of Branch) R1-R15 are General Purpose Registers, and can be read or written.
Additionally, the simulated CPU includes a single "zero-flag" status-bit. This bit tracks the result of the last executed Comparison instruction ()CMP). If that comparison was between two equal values, the zero-flag should be set to 1, otherwise it should be 0.
For this project, you should assume that the program text (the machine code) is stored in the main memory of the computer starting at address 0x0000. Additionally, you may assume that there is a hard limit of 1024 possible instructions in our programs (since that would entire fill our 2048 bytes of available memory). Assume that each instruction takes up 2 bytes (16-bits) in the program memory. You can implement this memory any way you see fit, but you need to be able to execute instructions based on the PC and order the instructions were entered. For example, the following program is a simple loop that increments R1 infinitely:
# Put 0 into register 1
MOV R1 0x0
# put 1 into register 2
MOV R2 0x1
# R1 = R1 + R2
ADD R1 R1 R2
# PC = PC - 2
B 0xFFE
# STOP Execution, this is never reached 9
What to do
Create a C program called "uCPU" that acts as a simulator for a small machine language. Your program should accept upper or lowercase input from STDIN (the default input location in C). The input will consist of a series of instructions encoded as 16-bit (4-digit hexadecimal) numbers, terminated by an
Each 16-bit, 4-digit instruction has the following format:
The first hexadecimal digit, represents the instruction opcode (see section 4 below)
The remaining three hexadecimal digits represent the operands to the instruction. There are three possible formats for operands:
registers - are encoded as a single hexadecimal digit, representing an unsigned value from 0–15 indicating a register: R0–R15
8-bit constants - are encoded as two-digit hexadecimal numbers, representing a signed 8-bit value with a range of -128–127.
12-bit constants - are encoded as three-digit hexadecimal numbers, representing a signed 12-bit value with a range of -2048–2047.
You can assume that the maximum program length is 1024 instructions (2048 bytes).
EOF is represented on Unix systems as
Before exiting your program should dump all registers (including PC) and it’s memory to STDOUT (the default output location in C) in the following format:
[mem 0x0000] [mem 0x0001] ... [mem 0x000F]
[mem 0x0010] [mem 0x0011] ... [mem 0x001F]
[mem 0x07E0] [mem 0x07E1] ... [mem 0x07EF]
[mem 0x07F0] [mem 0x07F1] ... [mem 0x07FF]
Include with your project a test assembly language program "fib.s" that stores the first 49 Fibonacci numbers at memory location 0x0040. Do not compile your fib.s program using Project 1!!
This CMSC 216 - IT Computer Science has been solved by our PhD Experts at My Uni Paper.
© Copyright 2026 My Uni Papers – Student Hustle Made Hassle Free. All rights reserved.