## LSU EE 3755 -- Spring 2008 -- Computer
Organization
#
## MIPS Notes 3 -- Object Files and
System Calls
## Contents
#
# Structure of SPIM Assembly Language
File
# SPIM and Real System
Calls
## References
#
# :PH:
Patterson & Hennessy, "Computer Organization &
Design"
# :Mv1: MIPS Technologies, "MIPS32 Architecture for
Programmers Vol I: Intro"
# :Mv2: MIPS Technologies, "MIPS32 Architecture for
Programmers Vol II: Instr"
################################################################################
## Structure of SPIM Assembly Language
File
# Simplified form of a real assembly language
program.
## What
an Assembler Does
#
# Creates an object file consisting of a number of
sections
# Some sections are:
#
# :Def:
Section Header
# A
directory to the other sections.
# Used
by programs that read the object file.
#
# :Def:
Text Segment
# The
program. (Encoded instructions, we know about these.)
# Stuff
that gets loaded into memory before the program is
run.
#
# :Def:
Data Segment
#
Encoded data. (Haven't seen this yet. Not much to
it.)
# Stuff
that gets loaded into memory before the program is
run.
#
# :Def:
Symbol Table
# A list
of symbols used in the assembler file.
# These
are used for linking object files together.
# The symbol table contains the remaining labels that
are not defined such
# as external references.
#
# :Def:
Relocation Information
# Places
in object file that refer to as yet undetermined memory
locations.
# The
relocation information identifies instructions and data words
that
# depend
on absolute addresses when the program is loaded into
memory.
#
# The object file header describes the size and
position of the other pieces of
# the object file.
##
Relevant Sections for this Class
#
# Text
# Data
##
Assembly Program Elements
#
# Instructions
#
These should be familiar
#
# Line Labels(address)
#
These should be familiar
#
# Assembler Directives
#
Tell the assembler how to interpret what
follows.
##
Assembler Directives
#
# These are a subset of directives recognized by real
assemblers.
#
.text
# Tells the
assembler that what follows is program code.
# The
assembler encodes instructions and places them in the text
segment.
#
.data
# Tells the
assembler that what follows is data.
# The
encoded data is placed in the data segment.
#
.globl FOOBAR
# Tells
assembler that other files may refer to symbol FOOBAR(Line
Label).
# In real
assemblers, this is needed so files can be linked
together.
# In SPIM
use .globl to SPIM can "see" __start, the entry point.
# In PCSpim
use .globl to PCSpim can "see"
main, the entry point.
.byte 1, 2, 3, ...
.half 1, 2, 3, ...
.word 1, 2, 3, ...
.float 1.1, 2.2, 3.3, ...
.double 1.1, 2.2, 3.3, ...
# Tell the
assembler to include the specified data.
The
# specified
data is converted into the indicated format:
# .word,
32-bit integers; .byte, 8-bit
integers.
#
.ascii "Hello class. I am an
ascii string. I'm not terminated."
.asciiz "Hello class. I am an ascii string, null-terminated as in
C."
# Tell the
assembler to include the specified data.
Each
# character
takes up one byte of memory. For
.asciiz a zero
# is written
after the last character (which is the way it's done
# in
c).
# :Example:
#
# A short assembly language program illustrating:
.data, .text, .asciiz, .globl
.data
happy_msg:
.asciiz "Hello, class!!"
msg2:
.asciiz "abc"
numbers: # 0x10010000 # the address of the data "byte
1"
.byte 1, 2, 3, 4
#
0x1004, 0x1006, 0x1008, 0x100a
halfs:
.half 1, 2, 3, 4
#
0x100c, 0x1010, 0x1014
.word 1, 2, 3
.float 1.1, 2.2, 3.3
.double 1.1, 2.2, 3.3
.text
.globl __start
__start:
la
$a0, happy_msg
addi
$v0, $0, 4
syscall #
printing string starting at
$a0(this reg. holds address of string).
addi
$v0, $0, 10
syscall #
syscall for exit(stop).
####
## the memory contents for data will be like
this:
## assume the address of "numbers" is 0x1001 0000
## at the simulator actual address will be 0x1001 0013
#
because we saved happy_msg starting at address
#
0x10001 0000 followed by msg2.
# data
0x1001 0000
1 ## byte 1 ##
#
0x1001 0001
2 ## byte 2
##
# 0x1001
0002 3 ## byte 3 ##
#
0x1001 0003
4 ## byte 4
##
#
0x1001 0004
1 ##
half
#
0x1001 0005
0
1
##
#
0x1001 0006
2 ## half
#
0x1001 0007
0
2
##
#
0x1001 0008
3 ##
half
#
0x1001 0009
0
3
##
#
0x1001 000a
4 ## half
#
0x1001 000b
0
4
##
#
0x1001 000c
1 ##
word
#
0x1001 000d
0
#
0x1001 000e
0
#
0x1001 000f 0 1
##
#
0x1001 0010
2 ## word 0000
0010
#
0x1001 0011
0
0000 0000
#
0x1001 0012
0
0000 0000
#
0x1001 0013
0
2
## 0000
0000
#
0x1001 0014
3 ##
word
#
0x1001 0015
0
#
0x1001 0016
0
#
0x1001 0017
0 3
##
#
0x1001 0018
cd ## float 1.1
#
0x1001 0019
cc
#
0x1001 001a
8c
#
0x1001 001b
3f
##
#
0x1001 001c
## float 2.2
#
0x1001 001d
##
.....
################################################################################
## SPIM and Real System
Calls
# :Def: System Call
#
# (1) A
routine provided by the operating system to perform
some
# action that user-level programs are not allowed to
do.
System
# calls typically perform I/O, allocate memory,
etc.
#
# (2)
The calling of such a routine.
## Unix
System Calls
#
# Details not important for this
class.
#
# Name
Number
Description
#
SYS_exit
1 Exit from
program.
#
SYS_fork
2 Duplicate this
(running) program.
#
SYS_read
3 Read (for example,
from a file).
#
SYS_write
4 Write (for example, to a
file).
#
SYS_open
5 Open a file,
etc.
#
SYS_close
6 Close an open
file.
#
SYS_wait
7 Wait for another
program to finish.
#
SYS_creat
8 Create a new
file.
#
SYS_link
9 Create an additional
name for a file.
#
SYS_unlink 10 Delete one of a file's
names.
## How
System Calls Made
#
# Depends on ISA.
#
# MIPS: syscall, and other
instructions.
#
# IA-32: int (software
interrupt)
#
# Common Details
#
#
The system call instruction specifies a number,
that
#
number identifies what the operating system is
supposed
#
to do.
##
Making SPIM System Calls
#
# System calls listed in PH Figure A.17, plus one
described below.
#
# Put system call code in register
$v0.
# Put parameters (if any) in registers $a0, $a1,
...
# Execute syscall
instruction.
# :Syntax: syscall
#
Passes control to exception handler.
#
In SPIM, exception handler provides service indicated by
$v0,
#
then returns to next instruction.
## SPIM
System Calls
#
# The complete list in PH Figure
A.17.
#
# Name Sys Call
Code
Description
# print_string 4
Print string starting at $a0.
# sbrk
9
Allocate(reserve) as many memory locations
#
as the contents of $a0.
#
Result will be starting
address in $v0.
# exit
10
Exit program. (Return control to user.)
# printf
11
Formatted print. (LSU SPIM only.)
# :Example:
#
# Use of exit system call. Make this call when your program
is
# finished.
#
# This is the correct way to exit from a
SPIM
# program.
SPIM will also exit from a program if it reaches
an
# uninitialized part of
memory.
addi
$v0, $0, 10 # Load
system call code
syscall
## Exit.
add
$t1, $t2, $t3 # Never
reached.
# :Example:
#
# Use of a system call to print a message.
.data
hello:
.asciiz "Hello, world!\n"
.text
.globl __start
__start:
la
$a0, hello
addi
$v0, $0, 4
syscall
addi
$v0, $0, 10
syscall
##
printf System Call
#
# System Call Code: 11
# $a0:
Address of format string.
# $a1, $a2, : Parameters.
#
# Print register contents formatted according to
format string.
# Format string same as C's printf with following
extension:
# After
the % can put a register number between slashes. Data
# will
be taken from that register.
#
# this program will add from 0 to
9.
#
.data
fmt:
.asciiz "Index: %/t1/d, sum: %/t0/d.\n"
.text
.globl __start
__start:
addi
$t0, $0, 0
addi
$t1, $0, 0
la
$a0, fmt
j
START
addi
$v0, $0, 11 # formatted print ..for
contents of $t1 and $t0.
LOOP:
add
$t0, $t0, $t1
syscall
# formatted print ..for contents of $t1 and
$t0.
addi
$t1, $t1, 1
START:
slti
$t2, $t1, 10
bne
$t2, $0, LOOP
nop
addi
$v0, $0, 10
syscall