Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

MIPS_primery_zadach / dandamudi05gtr guide risc processors programmers engineers

.pdf
Скачиваний:
78
Добавлен:
11.05.2015
Размер:
1.39 Mб
Скачать

Chapter 9 SPIM Simulator and Debugger

155

Figure 9.4 Debug commands available under the Simulator menu.

Single Step: This is the single-step command. The simulator executes one instruction and pauses execution. You can also use the F10 key for single-stepping.

Multiple Step...: This is a generalization of single-stepping. In this command, you can specify the number of instructions each step should execute. When you select this command, SPIM opens a dialog window to get the number of instructions information.

Breakpoints...: This command is useful to set up breakpoints. It opens the Breakpoint dialog box shown in Figure 9.5. You can add/delete breakpoints through this dialog box. As shown in this figure, it also lists the active breakpoints. When the execution reaches a breakpoint, execution pauses and pops a query dialog box (Figure 9.6) to continue execution. Normally, you enter the address of the instruction to specify a breakpoint. However, if the instruction has a global label, you can enter this label instead of its address.

Set Value...: This command can be used to set the value of a register or a memory location. It pops a window to enter the register/memory address and the value as shown in Figure 9.7. In this example, we are setting the value of the $a2 register to 7FFFF000H.

Display Symbol Table: This command displays the simulator symbol table in the message window.

Settings...: This opens the Settings dialog box shown in Figure 9.2. We have already discussed the simulator settings in detail.

When single-stepping your program, the instructions you see do not exactly correspond to your source code for two reasons: the system might have introduced some code (e.g., the startup code mentioned before), or because the pseudoinstructions are translated into

156

Guide to RISC Processors

Figure 9.5 Breakpoints dialog box.

Figure 9.6 Breakpoint query window.

Figure 9.7 Set value dialog box.

Chapter 9 SPIM Simulator and Debugger

157

processor instructions. For some pseudoinstructions, there is a single processor instruction. However, other pseudoinstructions may get translated into more than one processor instruction.

Summary

We have introduced the MIPS simulator SPIM. SPIM is a convenient tool to experience RISC assembly language programming. SPIM is available for a variety of platforms. It includes a simple debugger to facilitate single-stepping and setting breakpoints. In the last section, we have given an overview of its debugging facilities. However, we have not presented all the details about SPIM. Some of the missing details are given in the next chapter. Specifically, the next chapter describes the SPIM system calls and SPIM assembler directives. These details complement our discussion in this chapter.

10

Assembly Language

Overview

This chapter introduces the basics of the MIPS assembly language. Assembly language statements can either instruct the processor to perform a task, or direct the assembler during the assembly process. The latter statements are called assembler directives. We start this chapter with a discussion of the format and types of assembly language statements.

Assemblers provide several directives to reserve storage space for variables. SPIM also provides several I/O system calls to facilitate input and output of basic data types including integers, strings, and floating-point numbers. These details are presented in this chapter. The instruction set can be divided into several groups of instructions (arithmetic, logical, shift, and so on). We describe the data movement, load, and store instructions in detail. To help us write useful assembly language programs, we give an overview of some of the MIPS instructions belonging to the other groups. Later chapters discuss these instruction groups more fully.

Toward the end of the chapter, we provide several assembly language program examples. In each chapter, we introduce a simple program as our first program. Then we give several examples in the “Illustrative Examples” section. Starting with this chapter, we follow this format. The chapter concludes with a summary.

Introduction

In the previous chapters we have discussed the MIPS architecture and SPIM simulator details. In the remainder of the book, we concentrate on the assembly language program structure and related details to write and execute the MIPS assembly language programs.

Assembly language programs consist of processor instructions, instructions supported only by the assembler, and assembler directives. All three statements follow the same format. We start this chapter with a description of the assembly language statement format.

159

160

Guide to RISC Processors

The remainder of the book gives details about the MIPS instruction set as implemented by the SPIM simulator. However, to write meaningful assembly language programs, we give an overview of the instructions set. In this chapter, we present complete details on the load and store instructions. We only give a brief overview of the remaining instructions and defer a detailed discussion of these instructions to later chapters.

Assembly Language Statements

Assembly language programs are created out of three different classes of statements. Statements in the first class tell the processor what to do. These statements are called executable instructions, or instructions for short. Each executable instruction consists of an operation code (opcode for short). Executable instructions cause the assembler to generate machine language instructions.

The second class of statements consists of pseudoinstructions. These are not directly supported by the processor. The assembler supports the pseudoinstructions and generates one or more processor instructions to implement them. We clearly identify the pseudoinstructions and, where appropriate, show how they are implemented using the processor instructions.

The last class of statements provides information to the assembler on various aspects of the assembly process. These instructions are called assembler directives. Assembler directives are nonexecutable and do not generate any machine language instructions.

Assembly language statements are entered one per line in the source file. All three classes of the assembly language statements use the same format:

[label:] mnemonic [operands] [#comment]

The fields in the square brackets are optional in some statements. As a result of this format, it is common practice to align the fields to aid readability of assembly language programs. The assembler does not care about spaces between the fields.

Label Labels are used to associate them with the address of a memory location. Labels typically appear in text and data segments. In both segments, a label is terminated by a colon. In the text segment, labels are used to identify an instruction as shown here:

repeat: add

$t0,$t0,1 #increment $t0 by 1

In this example, the label repeat identifies the add instruction. In the data segment, labels are used for variable names as shown below:

number: .word

10

#initializes number to 10

This example declares the variable number and initializes it to 10. Labels, for obvious reasons, cannot be reserved words such as instruction mnemonics like add.

Mnemonic This is a required field and identifies the purpose of the statement. In most statements, it represents the opcode of the instruction such as add and sub. In certain

Chapter 10 Assembly Language Overview

161

statements, this field is not required. Examples include lines consisting of a comment, or a label, or a label and a comment.

Operands Operands specify the data to be manipulated by the statement. The number of operands required depends on the specific statement. Most statements have three operands as shown here:

add $t0,$t1,$t2

However, some instructions and pseudoinstructions have only one or two operands. Here are some examples:

li $v0,5 move $a0,$t2

la $t3,number mflo $t2

Don’t worry about the semantics of these instructions. We give details about these instructions in the remainder of the book.

Comment This is an optional field and serves the same purpose as that in a high-level language. Comments play a more important role in assembly language, as it is a low-level language. Assembler ignores all comments. Comments begin with a sharp sign (#) and extend until the end of the line. Because the readability of assembly language programs is poor, comments should be generously used to improve readability.

SPIM System Calls

SPIM provides I/O support through the system call (syscall) instruction. Eight of these calls facilitate input and output of the four basic data types: string, integer, float, and double. A notable service missing from this list is the character input and output. For character I/O, we have to use the string system calls.

To invoke a service, the system call service code should be placed in the $v0 register. Any required arguments are placed in the $a0 and $a1 registers (use $f12 for floatingpoint values). Any value returned by a system call is placed in $v0 ($f0 for floating-point values).

All ten system calls are summarized in Table 10.1. The first three calls are selfexplanatory. The print_string system call takes a pointer to a NULL-terminated string and prints the string. The read_int, read_float and read_double system calls read input up to and including newline. Characters following the number are ignored. The read_string call takes the pointer to a buffer in $a0 and the buffer size n in $a1. The buffer size should be expressed in bytes. It reads at most n − 1 characters into the buffer and terminates the string by the NULL character. The read_string call has the same semantics as the fgets function in the C library.

162

 

 

 

Guide to RISC Processors

 

 

Table 10.1 SPIM assembler system calls

 

 

 

 

 

 

 

 

 

 

 

Service

 

System call code

 

Arguments

Result

 

 

(in $v0)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

print_int

 

1

$a0

= integer

 

 

 

print_float

 

2

$f12 = float

 

 

 

print_double

 

3

$f12 = double

 

 

 

print_string

 

4

$a0

= string address

 

 

 

read_int

 

5

 

 

integer in $v0

 

read_float

 

6

 

 

float in $f0

 

read_double

 

7

 

 

double in $f0

 

read_string

 

8

$a0

= buffer address

 

 

 

 

 

 

$a1

= buffer size

 

 

 

sbrk

 

9

 

 

address in $v0

 

exit

 

10

 

 

 

 

 

 

 

 

 

 

 

 

The sbrk call returns a pointer to a block of memory containing n additional bytes. The final system call exit stops execution of a program.

SPIM Assembler Directives

SPIM supports a subset of the assembler directives provided by the MIPS assembler. This section presents some of the most common SPIM directives. The SPIM reference manual provides a complete list of directives supported by the simulator. All assembler directives begin with a period.

Segment declaration Two segments of an assembly program—code and data—can be declared by using .TEXT and .DATA directives. The statement

.TEXT <address>

directs the assembler to map the statements following it to the user text segment. The argument address is optional; if present, the statements are stored beginning at address. SPIM allows only instructions or words (using .WORD) in the text segment.

The data directive has a similar format as .TEXT except that the statements following it must refer to data items.

Chapter 10 Assembly Language Overview

163

String directives SPIM provides two directives to allocate storage for strings: .ASCII and .ASCIIZ. The .ASCII directive can be used to allocate space for a string that is not terminated by the NULL character. The statement

.ASCII "string"

allocates a number of bytes equal to the number of characters in string. For example,

.ASCII "Toy Story"

allocates nine bytes of contiguous storage and initializes it to “Toy Story”.

Strings are normally NULL-terminated as in C. For example, to display a string using print_string service, the string must be NULL-terminated. Using .ASCIIZ instead of ASCII stores the specified string in the NULL-terminated format. The .ASCII directive is useful to break a long string into multiple string statements as shown in the following example.

.ASCII

"Toy Story is a good computer-animated movie. \n"

.ASCII

"This reviewer recommends it to all kids \n"

.ASCIIZ

"and their parents."

An associated assembler directive

.SPACE n

can be used to allocate n bytes of uninitialized space in the current segment.

Data directives SPIM provides four directives to store both integers and floating-point numbers. The assembler directive

.HALF

h1,h2, . . .,hn

stores the n 16-bit numbers in successive memory halfwords. For 32-bit numbers, use the

.WORD directive. Although we refer to these 16and 32-bit values as numbers, they can be any 16and 32-bit quantities.

Floating-point values can be stored as single-precision or double-precision numbers. To store n single-precision floating point numbers, use

.FLOAT

f1,f2, . . .,fn

To store double precision numbers, use the .DOUBLE directive instead.

Miscellaneous directives We discuss two directives that deal with data alignment and global symbol declaration. The data directives .HALF, .WORD, .FLOAT, and .DOUBLE automatically align the data. We can explicitly control data alignment using the .ALIGN directive. The statement

164

Guide to RISC Processors

 

 

# Title of the program

Filename

#

 

# Objective:

 

#Input:

#Output:

#Register usage

################# Data segment #####################

.data

. . .

data go here

. . .

################# Code segment #####################

.text

.globl main main:

. . .

code goes here

. . .

li $v0,10 # exit syscall

Figure 10.1 MIPS assembly language program template

.ALIGN n

aligns the next datum on a 2 n byte boundary. Use

.ALIGN 0

to turn off the automatic alignment feature of the data directives until the next .DATA directive.

Before closing this section, we discuss one last directive: .GLOBL. It declares a symbol as global so that it can be referenced from other files. We normally use this directive to declare main as a global symbol so that it can be referenced by SPIM’s trap file (see page 153 for details on the trap file). The program template given next shows how the global directive is used.

Chapter 10 Assembly Language Overview

165

MIPS Program Template

Our MIPS programs follow the template shown in Figure 10.1. It consists of two main parts: a data segment and a code segment. The data segment is indicated by the .data directive. This part typically contains messages to be used in the user interface and the allocation of storage for variables, both initialized and uninitialized.

The code segment is identified by the .text directive. Program execution starts from the statement identified by the main label in the code segment. We end program execution by using the exit system call.

Here is an example code fragment that prompts the user for a name and reads the name.

#################### Data segment ########################

.data

prompt:

.ASCIIZ "Enter your name: " in_name:

.space 31

#################### Code segment ########################

.text

 

 

. . .

 

la

$a0,prompt

# prompt user

li

$v0,4

 

syscall

 

la

$a0,in_name

# read name

li

$a1,31

 

li

$v0,8

 

syscall

. . .

The data segment declares a prompt message prompt and space for the input string in_name. Because we reserved 31 bytes for in_name, we can read at most 30 characters from the input into this string.

In the code segment, the first system call displays the prompt message using the print_string system call. As expected by this system call, we place the address of prompt in $a0 and load 4 into $v0. The second system call uses the read_string system call to read the name from the user into the in_name buffer. By loading 31 into $a1, we limit the input string length to 30 characters.

Data Movement Instructions

MIPS provides several data movement instructions to move data between registers. Here we discuss the move instruction that can be used to move data between the general regis-

Соседние файлы в папке MIPS_primery_zadach