A Short Tutorial on Running MIPS Code on Pcspim
Total Page:16
File Type:pdf, Size:1020Kb
A short tutorial on running MIPS code on PCSpim
a. Running the sample MIPS code on PCSpim
Consider the following segment of MIPS code. It adds 4 to every element in the array a[i] stored in memory.
#for (i = 0; i < 10; i++) #{ # a[i] = 4 + a[i]; #}
.data #Directive specifying the data (Optional) str: .word 1,2,3,4,5,6,7,8,9,10 # 32 bit data stored in memory with the address of the first # location being stored in str label (Optional) .text #Directive specifying the start of the program (Required) .globl main # Label main is global (Required) main: la $s0,str # load address in str in $s0. (The first line of code starting # with label main (Required)) sub $s1,$s1,$s1 #Set s1 to zero loop: slti $t0,$s1,11 #if $s1 < 11 then $t0 = 1 beq $t0,$zero,exit #if $t0 = 0 then exit lw $t0,0($s0) #load a[i] add $t0,$t0,4 #a[i] = a[i]+4 sw $t0,0($s0) #store a[i] in memory addi $s0,$s0, 4 #goto next location addi $s1,$s1, 1 #increment I j loop #next iteration exit: jr $ra #Exit to SPIM after the end of the program (Required)
In the code, anything starting with a “.” is an assembler directive. Assembler directives are the instructions for the assembler and do not result in assembly code. For more information please see the PCSpim tutorial and the Appendix A8 on the CD.
. .data indicates that the data to be stored in the memory before the program starts running is specified in the next few lines. Here we are putting a list of 10, 32-bit data values from 1 to 10 in to the data memory. The size is specified by .word directive. The .data section is optional.
. .text directive is followed by the program text. This line is compulsory.
. .globl main the label main is global and can be referenced by different files. This line is compulsory.
. Write the code of the programs with the first line having the label main. This is because PCSpim uses this label to start executing the MIPS code.
. The program should always be terminated by a jr $ra instruction which causes the control to return to PCSpim. b. Steps required to run the above code on PCSpim
. Before running the program set the following the in the Simulator->Settings menu.
ON Save window positions - ON General register in hexadecimal - OFF Floating point register in hexadecimal - OFF Bare Machine (Required) ON Allow pseudo instructions (Required) ON Load exception file (Required) OFF Delayed Branches (Required) OFF Delayed Loads (Required) ON Mapped I/O (Required) OFF Quiet (Required)
. Open a notepad or text-pad and copy the above code to it.
. Change the .txt extension to .asm or .s extension.
. Start PCSpim.
. Go to File -> Open and then select the file having the assembly code PCSpim will not only load the file but also check the syntax and report errors. Now move the mouse to put the cursor over the scrollbar to the right of the second pane and move the bar down so you can find the instructions from your program.
. To run your program, you can either go to the Simulator menu or click on Go or just click on the Go icon. Either way, PCSpim pops up a dialog box with two entries and two buttons. This button tells PCSpim to run your program. Notice that when your program is running, PCSpim does not update the register display pane because the registers are continually changing. You can always tell whether PCSpim is running by looking at this pane or the title bar at the top of PCSpim’s window.
. If your program reads or writes from the terminal, PCSpim pops up another window called the console. All characters that your program writes appear on the console, and everything that you type as input to your program should be typed in this window.
. When you load the above code, the data that you entered will be shown as hex on the DATA pane. After running the code, one can see the value of those locations incremented by 4.
. Debugging
. To set breakpoints, go to the simulator window and click on set-breakpoints. Enter the address of the instruction where you want to set the breakpoint and then click on Add. Now the program will stop at the particular address and will ask whether or not to continue.
. Single stepping can be done after the execution stops at a breakpoint. F10 key is used to execute one instruction at a time.
. One can also execute more than one instruction by selecting Multiple step from the Simulation menu and the specifying the number of steps to take at a time.