## LSU EE 3755 -- Spring 2002 -- Computer Organization
#
## Note Set 11 -- Object Files and System Calls
# Time-stamp: <29 September 2002, 10:41:48 CDT, koppel@drop>
## 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.
#
# :Def: Relocation Information
# Places in object file that refer to as yet undetermined memory locations.
#
#
## Relevant Sections for this Class
#
# Text
# Data
## Assembly Program Elements
#
# Instructions
# These should be familiar
#
# Line Labels
# 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.
# In real assemblers, this is needed so files can be linked together.
# In SPIM use globl to SPIM can "see" __start, 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
.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
addi $v0, $0, 10
syscall
####
################################################################################
## 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 invocation 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 $a0 characters of memory. 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, officially sanctioned, 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
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, ... Positional parameters. (Parameters can be non-positional.)
#
# 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.
#
.data
fmt:
.asciiz "Index: %/t1/d, sum: %/t0/3d.\n"
.text
.globl __start
__start:
addi $t0, $0, 0
addi $t1, $0, 0
la $a0, fmt
j START
addi $v0, $0, 11
LOOP:
add $t0, $t0, $t1
syscall
addi $t1, $t1, 1
START:
slti $t2, $t1, 10
bne $t2, $0, LOOP
nop
addi $v0, $0, 10
syscall