csci341 -...

26
Lecture 22, MIPS Programming: Directives, Linkers, Loaders, Memory CSCI341

Upload: others

Post on 18-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

Lecture 22, MIPS Programming:Directives, Linkers, Loaders, Memory

CSCI341

Page 2: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

REVIEW

• Assemblers understand special commands called “directives”

• Assemblers understand “macro” commands

• Assembly programs become object files

•Object files are structured in a specific way (six segments)

Page 3: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

REVIEW

Six segments.

object file header

text segment

data segment

relocation information

symbol table

debugging info

You should have a good understanding ofwhat these segments are for.

Page 4: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

ASSEMBLER DIRECTIVES

•.data

•.text

•.double

•.globl

Your new friend: .asciiz

(and her brother, .ascii)

Page 5: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

.ASCIIZ

“Store this string in consecutive bytes in memory,and null-terminate it.”

Page 6: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

.ASCII

“Store this string in consecutive bytes in memory,but don’t null-terminate it.”

Page 7: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

EXAMPLE

.asciiz “liberty”

.byte 108, 105, 98, 101, 114, 116, 121, 0

is like...

Page 8: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

.BYTE CRASHES YOUR .ASCIIZ PARTY!

.byte

Another directive you now know.

“Store n 8-bit values in successive bytes of memory.”

Page 9: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

.BYTE

.byte 1, 8, 6, 7, 5, 3, 0, 9

“Store n 8-bit values in successive bytes of memory.”

.byte b1, b2, b3, b4 ..., bn

addr 0 1 2 3 4 5 6 7

val 1 8 6 7 5 3 0 9

Page 10: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

EXAMPLE

.asciiz “liberty”

.byte 108, 105, 98, 101, 114, 116, 121, 0

is like...

Page 11: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

EXAMPLE

.asciiz directives are often labeled. Why?

.datawelcome: .asciiz “Welcome to the jungle!”

.text # assembly code here...

“Store the string Welcome to the jungle! and remember it’sbase address as welcome.”

Page 12: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

NULL TERMINATIONA “special” character whose ascii code is 0.

Page 13: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

WHY NULL-TERMINATION?

Common functions such as println or theOS syscall(8)will read your ascii data beginning

with the base address until the null character is “found.”

Page 14: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

.ASCIIZ AND SYSCALL

• Store a string in memory using .asciiz

• Print the string to the screen via syscall

• (we’ll return to this after break)

(printing a string on the console)

Page 15: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKERS

• Separate compilation of files (“modules”) means we’ve got to “link” them together before execution.

• (To resolve all unresolved labels and symbols)

Page 16: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKERS

• Search program libraries to find library function calls in the program

•Determine memory locations that each module’s instructions will occupy, and relocate those instructions by adjusting absolute references.

• (Remember, “the shelf may move,” so instructions referencing something on the shelf must change.)

• Resolve references among modules

Page 17: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKER MISSION #1

• Ensure the program has no undefined labels.

• Aside: remember all those compiler warnings about unused or “unmatched” variable references?

• Those are symbols that were defined but never used.

Page 18: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKER MISSION #2

• If a program uses a library function, “extract” the function’s code from the library and integrate it into the final executable.

• (Keep in mind library functions may call other library functions)

• Continue this process until all external references are resolved.

Page 19: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKER MISSION #3

•Determine the memory locations each “module” will occupy.

• Absolute address references must be relocated.

• Process is assisted by relocation information segment of each module’s object file.

Page 20: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LINKER COMPLETE?

• Produces executable file.

• Typically has same segment-format as object file, except:

• There are no longer unresolved references

•No relocation information is stored

Page 21: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LOADING

• Executable programs first reside on disk (secondary storage)

• To “execute” a program means to:

• Load the executable file into memory

• Set the program counter to the base address of what was just loaded

• Go!

Page 22: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LOADING (HELLO KERNEL!)

The “kernel” is the central componentof the operating system.

It’s the “bridge” between applications andthe hardware layer.

Handles system calls.

Page 23: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LOADING•OS reads the file’s header to determine size of text and data

segments

• Creates space in memory for the program (instructions, data and a “workspace” or “stack”)

• Copies instructions and data into memory

• Copies arguments passed to the program onto the stack

• Initializes the machine registers (sets the stack pointer)

• Jumps to a startup routine that calls the main routine

Page 24: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

LOADING

• Lastly, if main returns, the startup routine terminates the program via an exit system call.

Page 25: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

AFTER BREAK:

•Memory usage

• Stacks

• Procedure calling (B.6, 2.8)

Page 26: CSCI341 - mines.humanoriented.commines.humanoriented.com/classes/2011/spring/csci341/slides/22.pdf · Directives, Linkers, Loaders, Memory CSCI341. REVIEW • Assemblers understand

• Reading 20

•MIPS Assembly Language Programming

• Chapter 4 (online)

• Start reviewing for the midterm

HOMEWORK

yeah midterms!