report english version
TRANSCRIPT
-
8/13/2019 Report English Version
1/24
SPIM
SPIM INTRODUCTION PCSPIM QTSPIM
INFORMATION SECURITY CLASS 11/30/13 COMPUTER ARCHITECTURE
COMPUTER ARCHITECTURE ASSIGNMENT 2
TEAM MEMBERS:
1.TRNG THNH DIN125200712.NG MINH TR125209803.TRN PH HUY125201784.L NGUYN HI PHONG125203155.L XCH LONG12520238
-
8/13/2019 Report English Version
2/24
INFORMATION SECURITY CLASS 1
SPIM
(CTRL + CLICK to go the page)
I. Introduction:..21.About Spim:..2
II. PCSpim:61.Getting Started with PCSpim:.6
2.DeBugging:...9
3.Save File:12
III. QtSpim:.....131.How to use:......13
2.Tab Data:.....16
3.Execute the program:....17
4.Save File:..18
5.Debugging:.....19
IV. END.22
-
8/13/2019 Report English Version
3/24
INFORMATION SECURITY CLASS 2
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 simulato
that executes MIPS programs.
Assembly languageis the symbolic representation of a computers binary encoding-machine
language( Binary representation used for communication within a computer system).Assembly
languageis more readable than machine language because it uses symbols instead of bits. Thesymbols in assembly language name commonly occurring bit patterns, such as opcodesand 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 toolcalled an assemblertranslates 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 fileand produces an object file containing
machine instructions and bookkeeping information that helps combine several object files into aprogram. 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 pre-
written routines supplied in aprogram 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 alinker,
combines a collection of object and library files into an executable file, which a computer can run.
-
8/13/2019 Report English Version
4/24
INFORMATION SECURITY CLASS 3
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. .textindicates that succeeding lines contain
instructions .data indicates that thy contain data .align nindicates that the items on the succeeding lines should
be aligned on a 2^n byte boundary. Hence .align 2means the next item should be on a word boundary .globl
maindeclares 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.
Spimis copyrighted byJames Larusand 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:
-
8/13/2019 Report English Version
5/24
INFORMATION SECURITY CLASS 4
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:
: .
The primary assembler directives for data declaration:
Declaration
.byte 8-bit variable
.half 16-bit variable.word 32-bit variable
.ascii ASCII string
.asciiz NULL terminated ASCII string
.float 32 bit IEEE floating pointnumber
.double 64 bit IEEE floating pointnumber
.space bytes of uninitialized
memoryTable 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.
-
8/13/2019 Report English Version
6/24
INFORMATION SECURITY CLASS 5
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 and .ent directives are required to define the name of the initial
or main procedure. The procedure should be terminated with the .end 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:
# Name and assignment number
# ---------------------------------------------------# Data declarations go in this section.
.data# program specific data declarations
# ---------------------------------------------------# Program code goes in this section.
.text
.globl main
.ent mainmain:
# -----# your program code goes here.# -----# Done, terminate program.
-
8/13/2019 Report English Version
7/24
-
8/13/2019 Report English Version
8/24
-
8/13/2019 Report English Version
9/24
INFORMATION SECURITY CLASS 8
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 theinstructions 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
-
8/13/2019 Report English Version
10/24
-
8/13/2019 Report English Version
11/24
INFORMATION SECURITY CLASS 10
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 entrycalled 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
-
8/13/2019 Report English Version
12/24
INFORMATION SECURITY CLASS 11
SPIM
Figure 2.2.3
When SPIMis about to execute the breakpointedinstruction, PCSpimpops 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-steppingandsetting breakpointswill 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
-
8/13/2019 Report English Version
13/24
INFORMATION SECURITY CLASS 12
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 toreinitializethe 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 Iconin menu, a box will pops up in the figure:
-
8/13/2019 Report English Version
14/24
INFORMATION SECURITY CLASS 13
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 QtSpimicon(location is
OS distribution dependent) and click on QtSpim.
Main Screen: The initial QtSpimscreenwill appear as shown below. There will be some minor difference
based on the specific Operating System being used:
http://sourceforge.net/projects/spimsimulator/files/http://sourceforge.net/projects/spimsimulator/files/http://sourceforge.net/projects/spimsimulator/files/ -
8/13/2019 Report English Version
15/24
INFORMATION SECURITY CLASS 14
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.2Once selected, a standard open file dialog box will be displayed. Find and select helloworld.asm file (or
whatever you named it) created in section.
-
8/13/2019 Report English Version
16/24
INFORMATION SECURITY CLASS 15
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.
-
8/13/2019 Report English Version
17/24
INFORMATION SECURITY CLASS 16
SPIM
Figure 3.1.4
The codeis 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.
MIPSincludes 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:
-
8/13/2019 Report English Version
18/24
INFORMATION SECURITY CLASS 17
SPIM
Figure 3.2.1As 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.
-
8/13/2019 Report English Version
19/24
INFORMATION SECURITY CLASS 18
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.
-
8/13/2019 Report English Version
20/24
INFORMATION SECURITY CLASS 19
SPIM
Figure 3.4.2
In general, the Text Segmentsand Consoleoptions 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 beentered 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
-
8/13/2019 Report English Version
21/24
INFORMATION SECURITY CLASS 20
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 theloop 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 pseudo-
instructions (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.
-
8/13/2019 Report English Version
22/24
INFORMATION SECURITY CLASS 21
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.
-
8/13/2019 Report English Version
23/24
INFORMATION SECURITY CLASS 22
SPIM
Figure 3.5.4
Select the Run/Continueoption 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. ENDDocumentation:
APPENDIXAssemblers, Linkers, and the SPIM simulator
James R. LarusMicrosoft ResearchMicrosoft
Fear of serious injury cannot anone justify suppression of free speech and assembly
Louis Brandeiswhitney v. California, 1927
MIPS Assembly Language Programming using QtSpim
Ed JorgensenVersion 1.1July 2013
-
8/13/2019 Report English Version
24/24
INFORMATION SECURITY CLASS 23
SPIM
SPIM S20: A MIPS R2000 Simulator
James R. LarusUniversity of WisconsinMadison
Getting Started with PCSpim. Overview of the Microsoft Windows version of spim