Building an instruction set | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Your task will be to define the fetch, decode and execute parts of a virtual processor. The task below will guide you through the steps you will need to follow in order to create your virtual processor. The processor will be 8 bit. In order to do the following you will need an extra register called the status register (not be confused with the interrupt status register). The status register will store the result of logical comparisons. It again will work on the idea of flags. Its structure will be -
The instructions you will need to encode are - ADD <Reg num> or literal - Will add a number to a specified register SUB <Reg num> or literal - Will subtract a number to a specified register MOV <Reg source>, <Reg Dest> - Moves a value from one register to another JMP <Reg with address> - Unconditional jump to an address specified by a register CMP <R1>. <R2> - compares two registers and stores the result in the status register JE <Reg with address> - Jumps if the equals flag is set in the register JLT <Reg with address> - Jumps if the less than flag is set JGT <Reg with address> - Jumps if the greater than flag is set LOAD <Reg Number>, Value – Loads a literal value into a register STORE <Reg Number>, <Reg address> - Stores a value into memory for later use LOAD <Reg Number>, <Reg Address> - Loads a value from memory
Step 1 decide on the registers You will have 4 general purpose registers to work with. The other registers you will have access to have already been defined in the course. You must ensure that all of the registers you will require are defined. You must list down the name of each of the registers and describe why they are needed. You should also give them a acronym such as R1 etc Step 2 Decide on operation codes or opcodes This is where you will create a table of operation codes. The instructions will be set out as follows
Some instructions might have more data and will require a additional byte in order to work. These will be any operation which uses multiple operands. Each instruction must be encoded. You are to produce a table which maps each instruction to a 4-bit opcode. Step 3 Considering memory and instructions It is important to remember that you will need to store some values or data along with your instructions. We will need to know where the data starts and also its structure. To make life a bit easier we will assume that instructions and data are kept in two separate blocks. Instructions will start at address 0x0 and data will be stored at address 0x7D0. Addresses are normally defined in hexadecimal.
The above diagram shows the situation. When you write some code you may need to store values into memory. As such you will need to consider which address each value is stored. Step 4 Convert a sample program into machine code ADD R1 ADD R2 You need to write down the machine code for this. The best way to do this is to do it in the form of a table as shown below.
Step 5 dealing with literals and multiple operands Some instructions will go across two bytes, that is be 16-bits. This is because 8 bits is not enough room to store all of the data required. This is fine and will be interpreted correctly by the processor. For example the instructions - ADD 5 LOAD R1, 10 ADD R1 Let's assume that the opcode for ADD is 0000 and the opcode for LOAD is 0011. We would get the following in our table
As you can see we are using 5 bytes to store only 3 instructions. The data parts are not considered to be instructions as such should not be numbered. The first shaded bar is the binary number for 5. The second is the binary number for 10. Now try and write machine code for the following LOAD R1, 20 LOAD R2, 30 ADD R2 SUB R1 Step 5 Jump! In order to jump we need to know the instruction number we are going to jump to. In assembly code we use what are known as labels. This makes things much easier. However for our virtual processor we are not going to allow labels. This is so we can get a better understanding of the jump command. Convert the following programs- LOAD ACC, 0 LOAD R1,[0x9] LOAD R2, 200 JMP R1 ADD R2 ADD 10 What is the value stored in the ACC at the end? Step 6 Conditional jump Now we can look at a conditional jump. When you run the CMP command it will compare two registers together. Look at the following code LOAD R1, 10 LOAD R2, 20 LOAD R3, [0xA] CMP R1, R2 JGT R3 ADD R2 ADD R1 The status register is set out as follows. As 10 is less than 20 we set the less than flag which is shown in shaded.
The next line is a conditional jump, JGT. This means that it only jumps if the value if bit 3 is set in the status register. As 10 is less than 20 and bit 2 would have been set as the result of the CMP instruction there will be no jump. As such the code will add both R2 and R1 to the ACC. As such the final value in the ACC will be 30 not 10. Convert the code above into machine code. Step 7 creating iteration LOAD R1, 0 LOAD R2, 1 LOAD R3, 10 MOV R1, ACC ADD 1 MOV ACC, R1 MOV R2, ACC ADD R2 MOV ACC, R2 CMP R1, R3 JLT [0x7] The above is a for loop. See if you can work out what happens. Also see if you can identify the initialisation, what happens each loop and where the conditional break is. Write down the result of the above code and write a pseudo code equivalent. Step 8 Some more programs. Write code to do the following-
Finally for the adventurous - Write bubble sort on the array of 100. |