Mips Labs Using SPIM - Lab 1
|
In this lab, a small swapping program will be used to demonstrate the usage of the SPIM environment
Opening SPIM
SPIM - A Whirlwind tour
The Program
Analysis of the Program
Assembling OpCodes of Instructions
Loading the Program
Executing the Program
Questions
Conclusion
Download and save this Trap
Handler file on your XP workstation.
Start PCSpim on your XP workstation. Instructions on where to get it and how to run it can be found here.
|
It will probably show an error like "Trap file not found. Do you want to open the settings dialog?" Click Yes, and set the path of the
trap file to whatever location you saved the downloaded trap
handler file |
Back to Top
PCSpim has four windows -
- Top Window - Registers
- II Window - Code
- III Window - Data, Stack and Kernel Data
- Bottom Window - Blurb
When there is no file loaded, the following are the states found in the windows:
Registers
Zeroed out, except for the stack pointer, which points to an empty stack (see the third window)
Code.
Contains the kernel code and code to execute the code that is loaded.
Data, Stack and Kernel Data
Data and stack are zeroed out. Kernel Data contains data needed by the Kernel Code.
Blurb
The only relevant detail is "Loaded ... trap.handler"
Memory Organization
|
0x00400000
|
Code
|
|
0x10000000 - 0x10040000
|
Data
|
|
0x7fffeffc
|
Stack
|
|
0x80000080
|
Kernel Code
|
|
0x90000000
|
Kernel Data
|
Back to Top
Download the source file from here:
Lab1.spim
Back to Top
#Comments begin with a '#' and continue till
#the end of the line
#
#Lab 1: Register Swapping & SPIM orientation
#
#
# Assembler Directive to load the
# data segment, text segment
.data
.word 7
.word 3
.text
.globl main
main:
lui $s0, 0x1001
#load upper part of register s0(16) with 0x1001 s0 = 0x10010000
lw $s1, 0($s0)
#load s1 with the contents of memory address 0x10010000 = 7,
#since we loaded the data there.
lw $s2, 4($s0)
#load s2 with the contents of memory address 0x10010004 = 3,
#since we loaded the data there.
sw $s2, 0($s0)
#store contents of s2 into memory address 0x10010000
sw $s1, 4($s0)
#store contents of s1 into memory address 0x10010004
Back to Top
From the mnemonic for the instruction (e.g. lui $s0, 0x1001), we can assemble the actual machine code for the instructions. For
operations involving immediate operands, the format for the instruction is (See Appendix
A-59):
Format of the instruction lui $s0, 0x1001
| OpCode |
Zero |
Register |
Immediate Operand |
| 0xf (001111) |
00000 |
(s0 is Register 16)10000 |
0001 0000 0000 0001 |
| 6 bits |
5 bits |
5 bits |
16 bits |
Written together, it will look like 001111 00000 10000 0001 0000 0000 0001
To express this binary string in hexadecimal, we need to group them into groups of four each, thus:
0011 1100 0001 0000 0001 0000 0000 0001
This binary string, expressed in hexadecimal, is 0x3c101001, which is the
machine code for the instruction
An example of the machine code of a load instruction is:
lw $s0,0($s1)
Format of the instruction lw $s1, 0($s0)
| OpCode |
Register |
Register |
Immediate Operand |
| 0x23 (10 0011) |
($s0=$16) 10000 |
($s1 = $17) 10001 |
0000 0000 0000 0000 |
| 6 bits |
5 bits |
5 bits |
16 bits |
Note: note the way that the opcode is constructed. 0x23 does not have the usual representation of 0010 0011 because 8 bits are
not available. It is represented as 10 0011 in 6 bits.
The machine code, therefore, is 1000 1110 0001 0001 0000 0000 0000 0000 or, 0x8e11000.
Back to Top
Save the file
Lab1.spim
into some folder on your XP workstation.
Load the file Lab1.spim into pcspim
[File->Open->(whatever path you have saved into)\Lab1.spim]
Note the immediate changes:
- The code is loaded into addresses starting from 0x00400024 (or 0x00400020 for pcspim)
- The data 7 and 3 is loaded into addresses 0x10010000 and 0x10010004
- The machine code for the mnemonics in the program is as calculated
The stack is still empty as we have not started executing the code.
Back to Top
Once the program has been loaded, we will step through the execution of the program. To set a breakpoint, select
[Simulator->Breakpoints]. Set a breakpoint at the
memory address of the first line of our code, 0x00400024 (or 0x00400020 for pcspim). The display of the first line of the code shows "break $1"
Select [Simulator->Go].
When it reaches the breakpoint, it will ask us whether to continue. Press no. Note that the
stack has changed because code is executing. It grows upwards, towards lower addresses.
Step through the code using < F10 >. After the first step, observe Register 16 (s0) in the topmost window. It has become 0x1001000.
Press < F10 >. Register 17 (s1) now contains 7, the contents of 0x1001000.
Press < F10 >. Register 18 (s2) now contains 3, the contents of 0x1001004.
Press < F10 >. Observe the third window. Memory address 0x1001000 now contains 3.
Press < F10 >. Memory address 0x1001004 now contains 7.
The contents in the memory have been swapped.
Back to Top
The following questions should be answered and submitted to the TA, Matt, via email: mpatitz@iastate.edu
1. After the values have been swapped in memory, what value is in the memory location immediately following the new location of the 7 (0x10010008)?
2. What is the very first instruction listed in the instruction window after the program has been loaded? (Hint: it includes the comment "# argc")
3. What is the value in the register referenced by the very last instruction? (Hint: it is a "jr" instruction)
Back to Top
In this lab, we have seen:
- The spim environment
- How to load, and track the execution of a piece of code in SPIM
Back to Top
|