CS232, Spring 2008

Wombat Programming assignment W3-1

due: Friday, March 7

Add a Stack and push and pop machine instructions to the Wombat2 and call the result the Wombat3. and then write and test a Wombat3 assembly-language program called "W3-1.a" that re-does Exercise W2-3 without any self-modifying code.

Here are the steps involved in creating the Stack and push and pop machine instructions:

  1. Start up CPU Sim and load the Wombat2 machine.
  2. Create a new separate RAM, called the "Stack".  The part of the Stack containing useful data should start at address 0 and grow toward higher-addressed locations, as data is pushed onto it.  To add a new RAM in CPU Sim, choose "Hardware modules..." from the Modify menu.
  3. Using the "Machine instructions..." menu item from the Modify menu, add push and pop machine instructions, with opcodes B and C (hexadecimal), respectively, that push the value in one of the A registers (specified by an operand to the push or pop instruction) onto the Stack or pop the top value off the Stack and put it in one of the A registers.  For example, an assembly language instruction would look like "push A1", which would push the value in register A[1] onto the top of the stack (the value pushed on the stack should also remain in A[1] since data movement is always done by copying). Similarly a " pop A1" assembly language instruction would pop the top value off the stack and put it in register A[1]. 
  4. Both new instructions should have field lengths of 5, 3, and 8.  The first 5-bit field forms the opcode, the next three-bit field contains the index of the A register to or from which the data is popped or pushed, and the last 8-bit field is unused. Therefore, in the format field for the push and pop instructions, type in "op reg un8".
  5. To keep track of the top location on the stack, use the A[7] register as a "stack pointer" register. From now on, the A[7] register should be used exclusively as a stack pointer and so should be used only to store the address of the top cell of the stack. More precisely, the stack pointer should always point to the top 16-bit slot on the Stack (the most recently pushed data). You can initialize the stack pointer to -2, if you wish, to indicate an empty stack. But it is okay for the stack pointer to be 0 initially, in which case bytes 0 and 1 of the stack will be unused and the first real data value will be added at address 2 & 3.
  6. To implement the new push and pop machine instructions, you will need to create some new microinstructions to move the data around and to increment and decrement the stack pointer.  You can also use many of the existing microinstructions.  I needed to create only 4 new microinstructions:  2 MemoryAccess microinstructions for accessing the Stack and 2 Increment microinstructions for modifying the value in the stack pointer.  The MemoryAccess microinstructions should use the stack pointer for the address register and the mdr for the data register.
  7. To implement the push instruction, include in its execute sequence some microinstructions that increment the stack pointer and then copy the value from the appropriate A register to the Stack location whose address is in the stack pointer.  Remember that the stack pointer should always point to the top 16-bit value on the Stack.  Do similar, but opposite, things to pop values off the Stack.
  8. CPU Sim has been set up to highlight the current instruction in the Main RAM of the Wombat2 when you step through the code in debug mode.  Similarly, you may wish to have CPU Sim highlight the top of the Stack of the Wombat3 as you step through the execution of your code in debug mode.  To set up this highlighting, choose "Options|Highlighting..." from the Execute menu.  Click the "New" button and then choose the stack pointer register (A[7]) and the Stack.  Also check the "dynamic" check box, which means that the highlighting will be updated continuously and not just at the beginning of each machine cycle.  Then click the "OK" button to close the highlighting dialog box.
  9. Save the changes you made to the Wombat3.

What to Hand In

As usual, hand in a hard copy of the Wombat3 assembly language program "W3-1.a".  (You don't need to hand in a hard copy of your "Wombat3.cpu" file.)  Also, put electronic copies of your "Wombat3.cpu" and "W3-1.a" files into a folder named "W3-1.your-last-name"  (for example, I would name my folder "W3-1.Skrien"), zip up your folder, and email it to me.  PLEASE name your folder and your programs correctly!