这个作业是编写一个模拟“ Brookshear Machine”的C程序
159.102 Technical Programming 2 Assignment 3
You have been asked to write a C program that simulates a ‘Brookshear Machine’.
The program reads machine code from a text file “a3.txt”.
Each line in the file contains 4 hexadecimal digits.
The first two digits on the first line make up an 8-bit byte that is to be stored at location 0 in the memory of the Brookshear Machine. The second two digits make up an 8-bit byte that is to be stored at location 1 in memory.
The next line contains two bytes that are stored at locations 2 and 3.
This is repeated until you get to the end of file.
Each line will be valid , and the data will fit into 256 bytes.
The Brookshear Program that these instructions form may not be particularly interesting, but it will be valid.
Your program steps though the machine code, one instruction at a time. Before each instruction, your program prints out a line of text that describes the current contents of the Program Counter, the instruction to be executed and the registers.
Each time an instruction is executed your program alters the Program Counter, Registers and Memory accordingly.
The format of the output line is:
PC INST – [R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 RA RB RC RD RE RF]
Where PC is the program counter, INST is the current instruction and R0 through to RF are the value of the sixteen registers. Each field is output as a 2 or 4 digit hexadecimal value (use the X format specifier to get capital letters) with a leading 0 if necessary.
Output continues (with no user intervention) until the Halt instruction is executed, at which time your program also stops.
A Halt instruction will occur in all test programs.
Example: if a3.txt contains:
Then the output is:
00 B404 – [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
04 2412 – [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
06 5345 – [00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00]
08 350C – [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]
0A C000 – [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]
Use unsigned char‘s for your PC, registers and memory.
When reading the a3.txt file, be careful with alignment problems in the scanf-like functions. First read each line into an unsigned int variable, then extract out the two bytes using bit-wise operators and store them in your memory array.
Your program must open the file a3.txt for reading by using the following arguments to the fopen function call
f = fopen(“a3.txt”,”r”); //you may change ‘f’ if you prefer!
Make sure that your program compiles and links using gcc. Submit your assign3.c electronically.
Marks will be awarded on how many test programs your program handles correctly.
There should be no other output apart from that stated above.
Test your program with different machine instructions before submission.
Due date: 5.00pm Tues 28th April, worth 4%.
Brookshear machine instructions
1RXY Load register R with the value at memory address XY
2RXY Load register R with the value XY
3RXY Store the value in register R at memory address XY
40RS Copy/move the value in register R to register S
5RST Add the values in registers R and S and put the answer in register T
7RST Bit-wise OR the values in registers R and S and put the answer in register T
8RST Bit-wise AND the values in registers R and S and put the answer in register T
9RST Bit-wise XOR the values in registers R and S and put the answer in register T
AR0X Rotate the contents of register R X times to the right
BRXY Jump to the instruction located at memory address XY if the value in register R is equal to the value in register 0 (i.e. change the PC to XY)