Docsity

Assemblers, Linkers and the SPIM Simulator - Computer Engineering | ECE 2030, Assignments of Electrical and Electronics Engineering

Partial preview of the text

Download Assemblers, Linkers and the SPIM Simulator - Computer Engineering | ECE 2030 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

A

Fear of serious injury cannot alone

justify suppression of free speech

and assembly.

Louis Brandeis Whitney v. California , 1927

Assemblers,

Linkers,

and the SPIM

Simulator

James R. Larus

Microsoft Research

Microsoft

A P P E N D I X

A.1 Introduction A- A.2 Assemblers A- A.3 Linkers A- A.4 Loading A- A.5 Memory Usage A- A.6 Procedure Call Convention A- A.7 Exceptions and Interrupts A- A.8 Input and Output A- A.9 SPIM A- A.10 MIPS R2000 Assembly Language A- A.11 Concluding Remarks A- A.12 Exercises A-

Encoding instructions as binary numbers is natural and efficient for computers. Humans, however, have a great deal of difficulty understanding and manipulating these numbers. People read and write symbols (words) much better than long sequences of digits. Chapter 2 showed that we need not choose between numbers and words because computer instructions can be represented in many ways. Humans can write and read 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 few hints about writing assembly programs, and explains how to run these programs on SPIM, a simulator that executes MIPS programs. UNIX, Windows, and Mac OS X versions of the SPIM simulator are available on the CD_. Assembly language_ is the symbolic representation of a computer’s binary encoding— machine language. Assembly language is more readable than machine language because it uses symbols instead of bits. The symbols in assembly lan- guage name commonly occurring bit patterns, such as opcodes and register speci- fiers, so people can read and remember them. In addition, assembly language

A.1 Introduction A.

machine language Binary rep- resentation used for communi- cation within a computer system.

A.1 Introduction A-

routine is much easier to read because operations and operands are written with symbols, rather than with bit patterns. However, this assembly language is still difficult to follow because memory locations are named by their address, rather than by a symbolic label. Figure A.1.4 shows assembly language that labels memory addresses with mne- monic 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 tell 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 next memory location. This program is as readable as most assembly language programs (except for a glaring lack of comments), but it is still difficult to follow because many simple operations are required to accomplish simple tasks and because assembly language’s lack of control flow constructs provides few hints about the program’s operation. By contrast, the C routine in Figure A.1.5 is both shorter and clearer since vari- ables have mnemonic names and the loop is explicit rather than constructed with branches. In fact, the C routine is the only one that we wrote. The other forms of the program were produced by a C compiler and assembler. In general, assembly language plays two roles (see Figure A.1.6). The first role is the output language of compilers. A compiler translates a program written in a

00100111101111011111111111100000 10101111101111110000000000010100 10101111101001000000000000100000 10101111101001010000000000100100 10101111101000000000000000011000 10101111101000000000000000011100 10001111101011100000000000011100 10001111101110000000000000011000 00000001110011100000000000011001 00100101110010000000000000000001 00101001000000010000000001100101 10101111101010000000000000011100 00000000000000000111100000010010 00000011000011111100100000100001 00010100001000001111111111110111 10101111101110010000000000011000 00111100000001000001000000000000 10001111101001010000000000011000 00001100000100000000000011101100 00100100100001000000010000110000 10001111101111110000000000010100 00100111101111010000000000100000 00000011111000000000000000001000 00000000000000000001000000100001

FIGURE A.1.2 MIPS machine language code for a routine to compute and print the sum of the squares of integers between 0 and 100.

assembler directive An opera- tion that tells the assembler how to translate a program but does not produce machine instruc- tions; always begins with a period.

A-6 Appendix A Assemblers, Linkers, and the SPIM Simulator

high-level language (such as C or Pascal) into an equivalent program in machine or assembly language. The high-level language is called the source language , and the compiler’s output is its target language. Assembly language’s other role is as a language in which to write programs. This role used to be the dominant one. Today, however, because of larger main memories and better compilers, most programmers write in a high-level language and rarely, if ever, see the instructions that a computer executes. Nevertheless, assembly language is still important to write programs in which speed or size are critical or to exploit hardware features that have no analogues in high-level lan- guages. Although this appendix focuses on MIPS assembly language, assembly pro- gramming on most other machines is very similar. The additional instructions and address modes in CISC machines, such as the VAX, can make assembly pro- grams shorter but do not change the process of assembling a program or provide assembly language with the advantages of high-level languages such as type- checking and structured control flow.

addiu $29, $29, - sw $31, 20($29) sw $4, 32($29) sw $5, 36($29) sw $0, 24($29) sw $0, 28($29) lw $14, 28($29) lw $24, 24($29) multu $14, $ addiu $8, $14, 1 slti $1, $8, 101 sw $8, 28($29) mflo $ addu $25, $24, $ bne $1, $0, - sw $25, 24($29) lui $4, 4096 lw $5, 24($29) jal 1048812 addiu $4, $4, 1072 lw $31, 20($29) addiu $29, $29, 32 jr $ move $2, $

FIGURE A.1.3 The same routine written in assembly language. However, the code for the rou- tine does not label registers or memory locations nor include comments.

source language The high- level language in which a pro- gram is originally written.

A-8 Appendix A Assemblers, Linkers, and the SPIM Simulator

tainty about the time cost of operations, programmers may find it difficult to ensure that a high-level language program responds within a definite time inter- val—say, 1 millisecond after a sensor detects that a tire is skidding. An assembly language programmer, on the other hand, has tight control over which instruc- tions execute. In addition, in embedded applications, reducing a program’s size, so that it fits in fewer memory chips, reduces the cost of the embedded computer. A hybrid approach, in which most of a program is written in a high-level lan- guage and time-critical sections are written in assembly language, builds on the strengths of both languages. Programs typically spend most of their time execut- ing a small fraction of the program’s source code. This observation is just the principle of locality that underlies caches (see Section 7.2 in Chapter 7). Program profiling measures where a program spends its time and can find the time-critical parts of a program. In many cases, this portion of the program can be made faster with better data structures or algorithms. Sometimes, however, sig- nificant performance improvements only come from recoding a critical portion of a program in assembly language.

#include

int main (int argc, char *argv[]) { int i; int sum = 0;

for (i = 0; i <= 100; i = i + 1) sum = sum + i * i; printf ("The sum from 0 .. 100 is %d\n", sum); }

FIGURE A.1.5 The routine written in the C programming language.

FIGURE A.1.6 Assembly language either is written by a programmer or is the output of a compiler.

Program (^) Compiler Assembler (^) Linker Computer

High-level language program

Assembly language program

A.1 Introduction A-

This improvement is not necessarily an indication that the high-level language’s compiler has failed. Compilers typically are better than programmers at producing uniformly high-quality machine code across an entire program. Pro- grammers, however, understand a program’s algorithms and behavior at a deeper level than a compiler and can expend considerable effort and ingenuity improving small sections of the program. In particular, programmers often consider several procedures simultaneously while writing their code. Compilers typically compile each procedure in isolation and must follow strict conventions governing the use of registers at procedure boundaries. By retaining commonly used values in regis- ters, even across procedure boundaries, programmers can make a program run faster. Another major advantage of assembly language is the ability to exploit special- ized instructions, for example, string copy or pattern-matching instructions. Compilers, in most cases, cannot determine that a program loop can be replaced by a single instruction. However, the programmer who wrote the loop can replace it easily with a single instruction. Currently, a programmer’s advantage over a compiler has become difficult to maintain as compilation techniques improve and machines’ pipelines increase in complexity (Chapter 6). The final reason to use assembly language is that no high-level language is available on a particular computer. Many older or specialized computers do not have a compiler, so a programmer’s only alternative is assembly language.

Drawbacks of Assembly Language

Assembly language has many disadvantages that strongly argue against its wide- spread use. Perhaps its major disadvantage is that programs written in assembly language are inherently machine-specific and must be totally rewritten to run on another computer architecture. The rapid evolution of computers discussed in Chapter 1 means that architectures become obsolete. An assembly language pro- gram remains tightly bound to its original architecture, even after the computer is eclipsed by new, faster, and more cost-effective machines. Another disadvantage is that assembly language programs are longer than the equivalent programs written in a high-level language. For example, the C program in Figure A.1.5 is 11 lines long, while the assembly program in Figure A.1.4 is 31 lines long. In more complex programs, the ratio of assembly to high-level lan- guage (its expansion factor ) can be much larger than the factor of three in this example. Unfortunately, empirical studies have shown that programmers write roughly the same number of lines of code per day in assembly as in high-level lan- guages. This means that programmers are roughly x times more productive in a high-level language, where x is the assembly language expansion factor.

A.2 Assemblers A-

be referenced from files other than the one in which it is defined. A label is local if the object can be used only within the file in which it is defined. In most assem- blers, labels are local by default and must be explicitly declared global. Subrou- tines and global variables require external labels since they are referenced from many files in a program. Local labels hide names that should not be visible to other modules—for example, static functions in C, which can only be called by other functions in the same file. In addition, compiler-generated names—for example, a name for the instruction at the beginning of a loop—are local so the compiler need not produce unique names in every file.

Since the assembler processes each file in a program individually and in isola- tion, it only knows the addresses of local labels. The assembler depends on another tool, the linker, to combine a collection of object files and libraries into an executable file by resolving external labels. The assembler assists the linker by pro- viding lists of labels and unresolved references. However, even local labels present an interesting challenge to an assembler. Unlike names in most high-level languages, assembly labels may be used before they are defined. In the example, in Figure A.1.4, the label str is used by the la instruction before it is defined. The possibility of a forward reference , like this one, forces an assembler to translate a program in two steps: first find all labels and then produce instructions. In the example, when the assembler sees the la instruction, it does not know where the word labeled str is located or even whether str labels an instruction or datum.

Local and Global Labels

Consider the program in Figure A.1.4 on page A-7. The subroutine has an external (global) label main. It also contains two local labels—loop and str—that are only visible with this assembly language file. Finally, the routine also contains an unresolved reference to an external label printf, which is the library routine that prints values. Which labels in Figure A.1. could be referenced from another file?

Only global labels are visible outside of a file, so the only label that could be referenced from another file is main.

EXAMPLE

ANSWER

forward reference A label that is used before it is defined.

A-12 Appendix A Assemblers, Linkers, and the SPIM Simulator

An assembler’s first pass reads each line of an assembly file and breaks it into its component pieces. These pieces, which are called lexemes , are individual words, numbers, and punctuation characters. For example, the line

ble $t0, 100, loop

contains six lexemes: the opcode ble, the register specifier $t0, a comma, the number 100 , a comma, and the symbol loop. If a line begins with a label, the assembler records in its symbol table the name of the label and the address of the memory word that the instruction occupies. The assembler then calculates how many words of memory the instruction on the current line will occupy. By keeping track of the instructions’ sizes, the assembler can determine where the next instruction goes. To compute the size of a variable- length instruction, like those on the VAX, an assembler has to examine it in detail. Fixed-length instructions, like those on MIPS, on the other hand, require only a cursory examination. The assembler performs a similar calculation to compute the space required for data statements. When the assembler reaches the end of an assembly file, the symbol table records the location of each label defined in the file. The assembler uses the information in the symbol table during a second pass over the file, which actually produces machine code. The assembler again exam- ines each line in the file. If the line contains an instruction, the assembler com- bines the binary representations of its opcode and operands (register specifiers or memory address) into a legal instruction. The process is similar to the one used in Section 2.4 in Chapter 2. Instructions and data words that reference an external symbol defined in another file cannot be completely assembled (they are unre- solved) since the symbol’s address is not in the symbol table. An assembler does not complain about unresolved references since the corresponding label is likely to be defined in another file

Assembly language is a programming language. Its principal difference from high-level languages such as BASIC, Java, and C is that assembly lan- guage provides only a few, simple types of data and control flow. Assembly language programs do not specify the type of value held in a variable. Instead, a programmer must apply the appropriate operations (e.g., integer or floating-point addition) to a value. In addition, in assembly language, programs must implement all control flow with go to s. Both factors make assembly language programming for any machine—MIPS or 80x86— more difficult and error-prone than writing in a high-level language.

symbol table A table that matches names of labels to the addresses of the memory words that instructions occupy.

The BIG Picture

A-14 Appendix A Assemblers, Linkers, and the SPIM Simulator

gram. This relocation information is necessary because the assembler does not know which memory locations a procedure or piece of data will occupy after it is linked with the rest of the program. Procedures and data from a file are stored in a contiguous piece of memory, but the assembler does not know where this mem- ory will be located. The assembler also passes some symbol table entries to the linker. In particular, the assembler must record which external symbols are defined in a file and what unresolved references occur in a file.

Elaboration: For convenience, assemblers assume each file starts at the same address (for example, location 0) with the expectation that the linker will relocate the code and data when they are assigned locations in memory. The assembler produces relocation information , which contains an entry describing each instruction or data word in the file that references an absolute address. On MIPS, only the subroutine call, load, and store instructions reference absolute addresses. Instructions that use PC-relative addressing, such as branches, need not be relocated.

Additional Facilities

Assemblers provide a variety of convenience features that help make assembler programs short and easier to write, but do not fundamentally change assembly language. For example, data layout directives allow a programmer to describe data in a more concise and natural manner than its binary representation. In Figure A.1.4, the directive

.asciiz “The sum from 0 .. 100 is %d\n”

stores characters from the string in memory. Contrast this line with the alternative of writing each character as its ASCII value (Figure 2.21 in Chapter 2 describes the ASCII encoding for characters):

.byte 84, 104, 101, 32, 115, 117, 109, 32 .byte 102, 114, 111, 109, 32, 48, 32, 46 .byte 46, 32, 49, 48, 48, 32, 105, 115 .byte 32, 37, 100, 10, 0

The .asciiz directive is easier to read because it represents characters as letters, not binary numbers. An assembler can translate characters to their binary repre- sentation much faster and more accurately than a human. Data layout directives

FIGURE A.2.1 Object file. A UNIX assembler produces an object file with six distinct sections.

Object file header

Text segment

Data segment

Relocation information

Symbol table

Debugging information

A.2 Assemblers A-

specify data in a human-readable form that the assembler translates to binary. Other layout directives are described in Section A.10 on page A-45.

Macros are a pattern-matching and replacement facility that provide a simple mechanism to name a frequently used sequence of instructions. Instead of repeat- edly typing the same instructions every time they are used, a programmer invokes the macro and the assembler replaces the macro call with the corresponding sequence of instructions. Macros, like subroutines, permit a programmer to create and name a new abstraction for a common operation. Unlike subroutines, how- ever, macros do not cause a subroutine call and return when the program runs since a macro call is replaced by the macro’s body when the program is assembled. After this replacement, the resulting assembly is indistinguishable from the equiv- alent program written without macros.

String Directive

Define the sequence of bytes produced by this directive:

.asciiz “The quick brown fox jumps over the lazy dog”

.byte 84, 104, 101, 32, 113, 117, 105, 99 .byte 107, 32, 98, 114, 111, 119, 110, 32 .byte 102, 111, 120, 32, 106, 117, 109, 112 .byte 115, 32, 111, 118, 101, 114, 32, 116 .byte 104, 101, 32, 108, 97, 122, 121, 32 .byte 100, 111, 103, 0

Macros

As an example, suppose that a programmer needs to print many numbers. The library routine printf accepts a format string and one or more values to print as its arguments. A programmer could print the integer in register $ with the following instructions:

.data int_str: .asciiz“%d” .text la $a0, int_str # Load string address

into first arg

EXAMPLE

ANSWER

EXAMPLE

A.2 Assemblers A-

Elaboration: Assemblers conditionally assemble pieces of code, which permits a programmer to include or exclude groups of instructions when a program is assembled. This feature is particularly useful when several versions of a program differ by a small amount. Rather than keep these programs in separate files—which greatly complicates fixing bugs in the common code—programmers typically merge the versions into a sin- gle file. Code particular to one version is conditionally assembled, so it can be excluded when other versions of the program are assembled. If macros and conditional assembly are useful, why do assemblers for UNIX systems rarely, if ever, provide them? One reason is that most programmers on these systems write programs in higher-level languages like C. Most of the assembly code is produced by compilers, which find it more convenient to repeat code rather than define macros. Another reason is that other tools on UNIX—such as cpp, the C preprocessor, or m4, a general macro processor—can provide macros and conditional assembly for assembly language programs.

la $a0, int_str mov $a1, $a jal printf

This example illustrates a drawback of macros. A programmer who uses this macro must be aware that print_int uses register $a0 and so cannot correctly print the value in that register.

ANSWER

Some assemblers also implement pseudoinstructions , which are instructions pro- vided by an assembler but not implemented in hardware. Chapter 2 contains many examples of how the MIPS assembler synthesizes pseudoinstructions and address- ing modes from the spartan MIPS hardware instruction set. For example, Section 2.6 in Chapter 2 describes how the assembler synthesizes the blt instruc- tion from two other instructions: slt and bne. By extending the instruction set, the MIPS assembler makes assembly language programming easier without compli- cating the hardware. Many pseudoinstructions could also be simulated with macros, but the MIPS assembler can generate better code for these instructions because it can use a dedicated register ($at) and is able to optimize the generated code.

Hardware Software Interface

A-18 Appendix A Assemblers, Linkers, and the SPIM Simulator

Separate compilation permits a program to be split into pieces that are stored in different files. Each file contains a logically related collection of subroutines and data structures that form a module in a larger program. A file can be compiled and assembled independently of other files, so changes to one module do not require recompiling the entire program. As we discussed above, separate compilation necessitates the additional step of linking to combine object files from separate modules and fix their unresolved references. The tool that merges these files is the linker (see Figure A.3.1). It performs three tasks: ■ Searches the program libraries to find library routines used by the program ■ Determines the memory locations that code from each module will occupy and relocates its instructions by adjusting absolute references ■ Resolves references among files A linker’s first task is to ensure that a program contains no undefined labels. The linker matches the external symbols and unresolved references from a pro- gram’s files. An external symbol in one file resolves a reference from another file if both refer to a label with the same name. Unmatched references mean a symbol was used, but not defined anywhere in the program. Unresolved references at this stage in the linking process do not necessarily mean a programmer made a mistake. The program could have referenced a library routine whose code was not in the object files passed to the linker. After matching symbols in the program, the linker searches the system’s program librar- ies to find predefined subroutines and data structures that the program references. The basic libraries contain routines that read and write data, allocate and deallo- cate memory, and perform numeric operations. Other libraries contain routines to access a database or manipulate terminal windows. A program that references an unresolved symbol that is not in any library is erroneous and cannot be linked. When the program uses a library routine, the linker extracts the routine’s code from the library and incorporates it into the program text segment. This new rou- tine, in turn, may depend on other library routines, so the linker continues to fetch other library routines until no external references are unresolved or a rou- tine cannot be found. If all external references are resolved, the linker next determines the memory locations that each module will occupy. Since the files were assembled in isolation,

A.3 Linkers A.

separate compilation Split- ting a program across many files, each of which can be com- piled without knowledge of what is in the other files.

A-20 Appendix A Assemblers, Linkers, and the SPIM Simulator

ating system kernel brings a program into memory and starts it running. To start a program, the operating system performs the following steps:

  1. Reads the executable file’s header to determine the size of the text and data segments.
  2. Creates a new address space for the program. This address space is large enough to hold the text and data segments, along with a stack segment (see Section A.5).
  3. Copies instructions and data from the executable file into the new address space.
  4. Copies arguments passed to the program onto the stack.
  5. Initializes the machine registers. In general, most registers are cleared, but the stack pointer must be assigned the address of the first free stack location (see Section A.5).
  6. Jumps to a start-up routine that copies the program’s arguments from the stack to registers and calls the program’s main routine. If the main routine returns, the start-up routine terminates the program with the exit system call.

The next few sections elaborate the description of the MIPS architecture pre- sented earlier in the book. Earlier chapters focused primarily on hardware and its relationship with low-level software. These sections focus primarily on how assembly language programmers use MIPS hardware. These sections describe a set of conventions followed on many MIPS systems. For the most part, the hard- ware does not impose these conventions. Instead, they represent an agreement among programmers to follow the same set of rules so that software written by different people can work together and make effective use of MIPS hardware. Systems based on MIPS processors typically divide memory into three parts (see Figure A.5.1). The first part, near the bottom of the address space (starting at address 400000 (^) hex ), is the text segment , which holds the program’s instructions. The second part, above the text segment, is the data segment , which is further divided into two parts. Static data (starting at address 10000000 (^) hex ) contains objects whose size is known to the compiler and whose lifetime—the interval dur- ing which a program can access them—is the program’s entire execution. For example, in C, global variables are statically allocated since they can be referenced

A.5 (^) Memory Usage A.

static data The portion of memory that contains data whose size is known to the com- piler and whose lifetime is the program’s entire execution.

A.5 Memory Usage A-

FIGURE A.5.1 Layout of memory.

Dynamic data Static data

Reserved

Stack segment

Data segment

Text segment

7fff fffchex

(^10000000) hex

(^400000) hex

Because the data segment begins far above the program at address 10000000 (^) hex , load and store instructions cannot directly reference data objects with their 16-bit offset fields (see Section 2.4 in Chapter 2). For example, to load the word in the data segment at address 10010020 (^) hex into register $v0 requires two instructions:

lui $s0, 0x1001 # 0x1001 means 1001 base 16 lw $v0, 0x0020($s0) # 0x10010000 + 0x0020 = 0x

(The 0x before a number means that it is a hexadecimal value. For example, 0x8000 is 8000 (^) hex or 32,768ten .) To avoid repeating the lui instruction at every load and store, MIPS systems typically dedicate a register ($gp) as a global pointer to the static data segment. This register contains address 10008000 (^) hex, so load and store instructions can use their signed 16-bit offset fields to access the first 64 KB of the static data segment. With this global pointer, we can rewrite the example as a single instruction:

lw $v0, 0x8020($gp)

Of course, a global pointer register makes addressing locations 10000000 (^) hex – (^10010000) hex faster than other heap locations. The MIPS compiler usually stores global variables in this area because these variables have fixed locations and fit bet- ter than other global data, such as arrays.

Hardware Software Interface