You are on page 1of 24

SPIM

COMPUTER ARCHITECTURE ASSIGNMENT 2

TEAM MEMBERS:
1. TRNG THNH DIN 12520071 2. NG MINH TR 12520980 3. TRN PH HUY 12520178 4. L NGUYN HI PHONG 12520315 5. L XCH LONG 12520238

SPIM
INTRODUCTION PCSPIM QTSPIM

INFORMATION SECURITY CLASS

11/30/13

COMPUTER ARCHITECTURE

SPIM

(CTRL + CLICK to go the page)

I.

Introduction: ..2
1.About Spim:..2

II.

PCSpim:6 1.Getting Started with PCSpim: .6 2.DeBugging:...9 3.Save File:12

III.

QtSpim: .....13 1.How to use: ......13 2.Tab Data: .....16

3.Execute the program: ....17 4.Save File: ..18

5.Debugging: .....19
IV. END.22

INFORMATION SECURITY CLASS

SPIM

I.

Introduction:
1. About Spim:
Encoding instructions as binary numbers is natural and efficient for computers. Humans, however, have a great deal of difficulty understanding and manupulating these numbers. People read and write symbols (words) much better than long sequences of digits. Humans can write and rad symbols, and computers can execute the equivalent binary numbers. This appendix describes the process by which a human-readable program is translated into a form that a computer can execute, provides a fews hints about writing assembly programs, and explains how to run these programs on SPIM, a simulator that executes MIPS programs. Assembly language is the symbolic representation of a computers binary encoding-machine language ( Binary representation used for communication within a computer system). Assembly language is more readable than machine language because it uses symbols instead of bits. The symbols in assembly language name commonly occurring bit patterns, such as opcodes and register specifiers, so people can read and remember them. In addition, assembly language permits programmers to use labels to identify and name particular memory words that hold instructions or data.

Figure. 1.1.1 A tool called an assembler translates assembly language into binary instructions. Assemblers provide a friendlier representation than a computers 0s and 1s that simplifies writing and reading programs. An assembler reads a single assembly language source file and produces an object file containing machine instructions and bookkeeping information that helps combine several object files into a program. Figure illustrates how a program is built. Most programs consist of several files-also called modules-that are written, compiled, and assembled independently. A program may also use prewritten routines supplied in a program library. A module typically contains references to subroutines and data defined in other modules and in libraries. The code in a module cannot be executed when it contains unresolved references to labels in other object files or libraries. Another tool, called a linker, combines a collection of object and library files into an executable file, which a computer can run. INFORMATION SECURITY CLASS 2

SPIM

Figure shows assembly language that labels memory addresses with mnemonic names. Most programmers prefer to read and write this form. Names that begin with a period, for example .data and .globl, are assembler directives that tall the assembler how to translate a program but do not produce machine instructions. Names followed by a colon, such as str or main, are labels that name the net memory location. This program is as readable as most assembly language programs.

Figure 1.1.2
The commands that start with periods are assembler directives. .text indicates that succeeding lines contain instructions .data indicates that thy contain data .align n indicates that the items on the succeeding lines should be aligned on a 2^n byte boundary. Hence .align 2 means the next item should be on a word boundary .globl main declares that main is a global symbol that should be visible to code stored in other files. Finally .asciiz stores a null-terminated string in memory.

Spim is copyrighted by James Larus and distributed under a BSD license. Copyright 1990-2010, James R. Larus. All rights reserved.

2. Basic:
Comments: The # character represents a comment line. Anything types after the # is considered a comment. Assembler Directives:

INFORMATION SECURITY CLASS

SPIM

An assembler directive is a message to the assembler that tells the assembler something it needs to know in order to carry out the assembly process. Directives are required for data declarations and to define the start and end of procedures. Assembler directives start with a . .Ex: .data, .text. Data Declarations: The data must be declared in the .data section. Variable names must start with a letter. The general format: <variableName>: .<dataType> <initialValue> The primary assembler directives for data declaration:

Declaration .byte .half .word .ascii .asciiz .float

8-bit variable 16-bit variable 32-bit variable ASCII string NULL terminated ASCII string 32 bit IEEE floating point number .double 64 bit IEEE floating point number .space <n> <n> bytes of uninitialized memory
Table 1.2.1

Integer Data Declarations: Integer values are defined with the .word,.half or .byte directives EX: wVar1: .word 450000 hVar2: .half 5000 bVar3: .byte 10 String Data Declarations: Strings are defined with .ascii or .asciiz directives. The C/C++ style new line, \n, and tab, \t, are supported within in strings. To define a string with multiple lines, the NULL termination would only be required on final line. Ex: message: .ascii Hello World\n .ascii Line 2: So, long and thanks .ascii for all the fish.\n .ascii Game Over.\n Floating-Point Data Declarations: Floating-point values are defined with .float or .double directives. INFORMATION SECURITY CLASS 4

SPIM

Ex: Pi: .float 3.14159 Pi2: .double 6.28318 Constants: Constant names must start with a letter. Constant definitions are created with an = sign. Ex: TRUE = 1 Program Code: The code must be preceded by the .text directive. The .globl <name> and .ent <name> directives are required to define the name of the initial or main procedure. The procedure should be terminated with the .end <name> directive. Labels: Labels are code location. Characters in a label are case-sentitive. The first use of a label is the main program starting point, which must be named main. Rules: o Must start with letter. o May be followed by letters, numbers, or a _. o Must be terminated with a :. o May only be defined one. Ex: main: Loop: Program Template: Basic template for QtSpim:

# # # .data # # #

Name and assignment number --------------------------------------------------Data declarations go in this section.

program specific data declarations --------------------------------------------------Program code goes in this section.

.text .globl .ent main: # # # # -----

main main

your program code goes here. ----Done, terminate program.


INFORMATION SECURITY CLASS 5

SPIM

li $v0, syscall .end main Name .data Parameters Addr

10 # all done!

.text

Addr

.kdata .ktext .extern .globl

Addr Addr Sym size Sym

Description The following items are to be assembled into the data segment. By default, degin at the next available address in the data segment. If the optional argument addr is present, then begin at addr. The following items are to be assembled into the text segment. If the optional argument addr is present, then begin at addr. In SPIM, the only items that can be assembled into the text segment are instructions and words (via the .word directive). The kernel data segment. Like the data segment, but used by the Operating System. The kernel text segment. Like the text segment, but used by the Operating System. Declare as global the label sym, and declare that it is size bytes in length (this information can be used by the assembler). Declare as global the label sym. Table 1.2.2

SPIM syscalls: Makes a system call. Service Code Print_int 1 Print_float 2 Print_double 3 Print_string 4 Read_int 5 Read_float 6 Read_double 7 Read_string 8 Sbrk Exit 9 10

Arguments $a0 $f12 $f12 $a0 None None None $a0 (address), $a1 (length) $a0 (length) None Table 1.2.3

Result None None None None $v0 $f0 $f0 None $v0 None

II.

PCSpim:
1. Getting Started with PCSpim:
Downloading this program:

http://spimsimulator.svn.sourceforge.net/viewvc/spimsimulator/
When PCSpim is installed, it adds an entry to your start menu called PCSpim. Click on it and PCSpim is installed, it adds an entry to your start menu called PCSpim. Click on it and PCSpim will start running. When PCSpim starts, it pops up a large window on your screen. The window is divided into four panes: INFORMATION SECURITY CLASS 6

SPIM

Figure 2.1.1 The top pane shows the values of all registers in the MIPS CPU and FPU. This display is updated whenever your program stops running. The next pane displays instructions from both your program and the system code that is loaded automatically when PCSpim starts running. Each instruction is displayed on a line that looks like.

Figure 2.1.2
The first number on the line, in square brackets, is the hexadecimal memory address of the instruction. The second number is the instructions numberical encoding, again displayed as a hexadecimal number. The third item is the instructions mnemonic description. Everything following the semicolon is the actual line from your assembly file that produced the instruction. The number 89 is the line number in that file. Sometimes nothing is on the line after the semicolon. This means that the instruction was produced by SPIM as part of translating a pseudo-instruction into more than one actual MIPS instruction.

The third pane displays the data loaded into your programs memory and the data on the programs stack. The bottom pane is where PCSpim writes messages. This is where error messages appear. Lets see how to load and run a program. You can either go to the File menu at the top left of PCSpims window and click on Spen, or you can click on the open icon right below. Either way, PCSpim opens up a INFORMATION SECURITY CLASS 7

SPIM

familiar dialog box, which lets you find the file that you want to load. After youve selected the file, click on the Open button and PCSpim will load the file. If you change your mind, click on the Cancel button and nothing will happen. When you click on open, PCSpim gets rid of the prompt window, then loads your program and redraws the screen to display its instructions and data. Now move the mouse to put the cursor over the scrollbar to the tight of the second pane and move the bar down so you can find the instructions from your program.

Figure 2.1.3 To run your program, you can either go to the Simulator menu and click on Go or just click on the Go icon. Either way, PCSpim pops up a dialog box with two entries and two buttons.

Figure 2.1.4 INFORMATION SECURITY CLASS 8

SPIM

If you want to stop your program, go to the Simulator menu and click on Break. This cause PCSpim to pop up a dialog box with two buttons. Befor clicking on either button, you can look at registers and memory to find out what your program was doing when you stopped it. When you understand what happened, you can either continue the program by clicking on Yes or stop your program by clicking on No. 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.

2. DeBugging
Suppose your program does not do what you expect. What can you do? SPIM has two features that help debug your program. The first, and perhaps the most useful, is single-stepping, which allows you to run your program an instruction at a time. Open the Simulartor menu and click on Single Step. Everytime you do this, PCSpim will execute one instruction and update its display, so that you can see what the instruction changed in the registers or memory.

Figure 2.2.1 INFORMATION SECURITY CLASS 9

SPIM

What do you do if your program runs for a long time before the bug arises? You could single-step until you get to the bug, but that can take a long time, and it is easy to get so bored and inattentive that you step past the problem. A better alternative is to use a breakpoint, which tells PCSpim to stop your program immediately before it executes a particular instruction. In the Simulator menu, there is an entry called Breakpoints. Click on it, and the PCSpim program pops up a dialog box with an empty box and a list. Type into this box the address of the instruction at which PCSpim should stop. Or, if the instruction has a global label, you can just type the name of the label. Labeled breakpoints are a particularly convenient way to stop at the first instruction of a procedure. To actually set the breakpoint, click the Add button. The breakpoint will then appear in the list of active breakpoints. You can get as many breakpoints as you want. To remove a breakpoints, select it in the list, and click on Remove. After you have set your breakpoints, Close the dialog and run your program.

Figure 2.2.2

INFORMATION SECURITY CLASS

10

SPIM

Figure 2.2.3 When SPIM is about to execute the breakpointed instruction, PCSpim pops up a dialog with the instruction address and two buttons. The Yes button continues running your program, and No stops your program. Before you click on either button, you can examine the display to see the values in registers or memory or you can add or remove breakpoints.

Figure 2.2.4 Single-stepping and setting breakpoints will probably help you find a bug in your program quickly. How do you fix it? Go back to the editor that you used to create your program and change it. To run the program again, you need a fresh copy of PCSpim, which you get in one of two ways. Either you can wait INFORMATION SECURITY CLASS 11

SPIM

from PCSpim by clicking on the wait entry in the File menu, or you can just reload your program. When you reload your program, PCSpim will ask if you want to reinitialize the simulator. The answer is yes if you have changed your program and want to try it again.

Figure 2.2.5

3. Save File:
To Save file log, click the Saving Icon in menu, a box will pops up in the figure:

INFORMATION SECURITY CLASS

12

SPIM

Figure 2.3.1

III.

QtSpim:
1. How to use:
Downloading: http://sourceforge.net/projects/spimsimulator/files/ There are multiple versions for different operation systems. For Windows, this is typically, performed with the standard Start Menu -> Programs -> QtSpim operation. For MAC OS, enter LaunchPad and click on QtSpim. For Linux, find the QtSpim icon (location is OS distribution dependent) and click on QtSpim. Main Screen: The initial QtSpim screen will appear as shown below. There will be some minor difference based on the specific Operating System being used:

INFORMATION SECURITY CLASS

13

SPIM

Figure 3.1.1 Load Program: to load the example program (and all programs), you can select standard File -> Reinitialize and Load File option from the menu bar. However, it is typically easier to select the Reinitialize and Load File Icon from the main screen (second file icon on right side). Note, the Load File option can be used on the initial, load, but subsequent file loads will need to use the Reinitialize and Load File to ensure the appropriate reinitialization occurs.

Figure 3.1.2 Once selected, a standard open file dialog box will be displayed. Find and select helloworld.asm file (or whatever you named it) created in section.

INFORMATION SECURITY CLASS

14

SPIM

Figure 3.1.3 When the file load is completed with no errors, the program is ready to run, but has not yet been executed. The screen will appear something like the following image.

INFORMATION SECURITY CLASS

15

SPIM

Figure 3.1.4 The code is placed in Text Window. The first column of hex values (in the []s) is the address of the line of code. The next hex value is the OpCode or hex value of the 1s and 0s that the CPU understands to be that instruction. MIPS includes psuedo-instructions. That is an instruction that the CPU does not execute, but the programmer is allowed to use. The assembler, QtSpim here, accepts the instruction and inserts the real or bare instruction as appropriate.

2. Tab Data:
The data segment contains the data declared by your program (if any). To view the data segment, click on the Data Icon. The data window will appear similar to the following:

INFORMATION SECURITY CLASS

16

SPIM

Figure 3.2.1 As before, the addresses are shown on the left side (with the []s). The values at that address are shown in hex (middle) and in ASCII (right side). Depending on the specific type of data declarations, it may be easier to view the hex representation ( like the array of numbers from the example code). Note, right clicking in the Data Window will display a menu allowing the user to change the default hex representation to decimal representation.

3. Execute the program:


To execute the entire program (uninterrupted), you can select the standard Simulator -> Run/Continue option from the menu bar. However, it is typically easier to select the Run/Continue Icon from the main screen or to type the F5 key.

INFORMATION SECURITY CLASS

17

SPIM

Figure 3.3.1 Once typed, the program will be execution. If a program performs input and/or output, it will be directed to the Console window. For example, the same program (from Appendix B) will display the following in the Console window when executed.

FIgure 3.3.2

4. Save File:
QtSpim can create a log file documenting of the program results. To create a log file, you can select the standard File -> Save Log File option from the menu bar. However, it is typically easier to select the Save Log File Icon from the main screen.

Figure 3.4.1 When selected, the Save Windows to Log File dialog box will be displayed as shown below on the left.

INFORMATION SECURITY CLASS

18

SPIM

Figure 3.4.2 In general, the Text Segments and Console options should be selected as shown on the left. Based on the current version, selecting all will cause the simulator to crash. Additionally, there is no default file name or location (for the log file). As such, a file name must be entered before it can be saved. This can done by manually entering the name in the Save to file box or by selecting the box (on the lower tight side).

5. Debugging:
Often, looking at program source code will not help to find errors. The first step in debugging is to ensure that the file assembles correctly (or reads in the specific case of QtSpim). However, even if the file assembles, it still may not work correctly. In this case, the program must be debugged. In a broad sense debugging is comparing the expected program results to actual program results. This requires a solid understanding of what the program is supposed to do and the specific order in which it down it -> that is understanding the algorithm being used to solve the program. The algorithm should be noted in the program comments and can be used as a checklist for the debugging process. One potentially useful way to check the program status is to view the register contents. The current register contents are show in registers window (left side) as shown in the image below.

Figure 3.5.1 The overall debugging process can be simplified by using the QtSpim controlled execution functions. These functions include single stepping through the program and using one or more breakpoints. A breakpoint a programmer selected location in the program where execution will be paused. When the program is paused the current program status can be checked by viewing the register contents and/or

INFORMATION SECURITY CLASS

19

SPIM

the data segment. Typically, a breakpoint will be set, the program executed, and from there single stepping through the program watching execution and checking results. To set a breakpoint, select an appropriate location. This should be chosen with a specific expectation in mind. For example, if a program does to produce the correct average for a list of numbers, a typical debugging strategy would be to see of the sum is correct. As such, a breakpoint could be set after the loop and before the average calculation. As an example, to set a breakpoint after the loop in the sample program (from Appendix B), the first instruction after the loop can be found in the Text Window. This will require looking at the pseudoinstructions (on the right side of the Text Window). The first instruction after the loop in the example program is highlighted in orange in the image below. Note, the orange highlighting was added in this document for reference and will not be displayed in QtSpim during normal execution.

Figure 3.5.2 When an appropriate instruction is determined, move the cursor to the instruction address an right-click. The right-click will display the breakpoint menu as shown in the image below.

INFORMATION SECURITY CLASS

20

SPIM

Figure 3.5.3 To set a breakpoint, select the Set Breakpoint option. If a breakpoint has already been set, it can be cleared by selecting the Clear Breakpoint option. Once the breakpoint has been set, it will be highlighted with a small red icon such as an N as shown in the following image. Note, different operating systems may use a different icon.

INFORMATION SECURITY CLASS

21

SPIM

Figure 3.5.4 Select the Run/Continue option which will execute the program up the selected breakpoint. When program execution reaches the breakpoint, it will be paused and a Breakpoint dialog box display as shown in the below image.

Figure 3.5.5 The program execution can be halted by selecting the Abort box. The breakpoint can be ignored, thus continuing to the next breakpoint or program termination, whichever comes first. However, typically the Single Step box will be selected enter the single step mode. The following image shows the result of selecting Single Step.

Figure 3.5.6 IV. END Documentation: APPENDIX Assemblers, Linkers, and the SPIM simulator James R. Larus Microsoft Research Microsoft Fear of serious injury cannot anone justify suppression of free speech and assembly Louis Brandeis whitney v. California, 1927 MIPS Assembly Language Programming using QtSpim Ed Jorgensen Version 1.1 July 2013 INFORMATION SECURITY CLASS 22

SPIM

SPIM S20: A MIPS R2000 Simulator James R. Larus University of Wisconsin Madison Getting Started with PCSpim. Overview of the Microsoft Windows version of spim

INFORMATION SECURITY CLASS

23

You might also like