while (A != 0) { Product = Product + B; A = A - 1; }You can download an example of a machine that implements this algorithm from here. Download this model, load it into the simulator, and experiment with it. What is the cost of this machine? How many cycles are required to multiply two relatively large numbers (say 0x1234 by 0xABCD)?
while (A != 0) { if (A % 2 == 1) // If A is odd Product = Product + B; B = B << 1; // Multiply B by two A = A >> 1; // Divide A by two }Checking for oddness is easy in a binary representation. Just look at the low order bit. Doubling and halving numbers is easily accomplished by shifting left or right, respectively. What is the cost of this new machine? How many cycles are required to multiply two numbers?
The machine you'll simulate (called the HW3 machine) is a 32-bit machine with an extremely simple architecture. There are only eight instructions, no general purpose registers, and no explicit addressing of memory. Instead, data memory is organized as a stack of 32-bit integers, and all instructions refer to the top of the stack for operands. (There is also a separate memory to hold instructions, addressable only through the HW3 PC.) The HW3 CPU has an internal register, the SP, to keep track of the top of the stack. The user's program can affect the SP only indirectly, by adding to or removing from the stack.
The table below lists the set of instructions available (as well as some other info to be explained shortly). A user of the HW3 machine would write a program using those instructions. (Please note: the opcodes have changed from the original HW3 definition. Once you get started, you'll see why we did this.)
Opcode | Operation | Example | Meaning |
---|---|---|---|
0000 | Stop | 0x00000000 | Stop the program |
0001 | Show | 0x10000000 | Show the stack top |
0010 | Clear | 0x20000000 | Clear the stack |
1010 | Mult | 0xA0000000 | Multiply |
1011 | Div | 0xB0000000 | Divide |
1000 | Add | 0x80000000 | Add |
1001 | Sub | 0x90000000 | Subtract |
1111 | Push | 0xF0000003 | Push 3 onto the stack |
Stop indicates the end of a program. Clear indicates that the stack should be cleared, that is, set to empty. Show requests that the integer at the top of the stack be printed. Push adds a new integer to the top of the stack. Add, Sub, Mult, and Div replace the top two integers on the stack with the single integer result of the operation specified. For example, if the stack contains (from bottom to top) {1 2 3}, a Sub instruction would result in {1 -1}. Notice that only the Push instruction specifies an operand.
HW3 instructions are encoded as 32-bit values. The high order four bits (bits 28 through 31) specify the opcode, as shown in the table. In all instructions other than Push, the low order 28 bits are ignored. In the Push instruction, the low order 28 bits contain a signed integer.
Your machine will implement a fetch-increment-execute loop: it fetches the instruction specified by the PC, increments the PC to point to the next instruction, and then performs the operation given by the instruction just fetched.
Although this machine seems to be too simple to do anything useful, we could turn it into a fully general purpose machine by adding just a few instructions (such as Load/Store/Branch).
Designing the datapath and control is totally up to you, but here are some hints:
// Comment line <starting address in decimal> <word1 in hex> <word2 in hex> ... <wordN in hex>For example:
// My first program 0 F0000002 F0000003 F0000004 F0000005 A0000000 80000000 90000000 00000000Pushes four operands onto the stack and then does a MULT, ADD, SUB, and finally a STOP. Double clicking the memory component will allow you to load a file like the above into memory.
It is possible for the HW3 machine to encounter error conditions. You should deal with stack overflow (pushing more data onto the stack than it can hold) and underflow (performing more operations than there are elements on the stack to serve as operands for). Your machine should halt and place a value into a status register indicating what happened: 0 means normal termination, 1 means stack underflow, 2 means stack overflow.
The SMOK system provides you with some important metrics that you should keep in mind. The first is the cycle time of your machine and the second is the "cost", which is a measure of how many transistors your components would consume. The person(s) who get the lowest cycle time and cost will receive a special prize!
Please turn in the following: