PowerPoint 演示文稿
CO101
Principle of Computer
Organization
Lab 02: MIPS Assembly
Language Programming
Liang Yanyan
澳門科技大學
Macau of University of Science and Technology
Overview
• Assembly programing
• Programmer view of a MIPS32 machine
• Preliminaries of assembly programing
• Using SPIM
• System service in SPIM
• Lab assignment
2
Abstraction of Computer
3
Question:
1. Where’s cache?
2. Why to know programmers’ view?
Registers
• 32 general-purpose registers
• register preceded by $ in assembly language instruction
• two formats for addressing:
• using register number e.g. $0 through $31
• using equivalent names e.g. $t1, $sp
• special registers Lo and Hi used to store result of
multiplication and division
• not directly addressable; contents accessed with special
instruction mfhi (“move from Hi”) and mo (“move from
Lo”)
• stack grows from high memory to low memory
4
Register Names and Descriptions
5
Data Types and Literals
• Data types:
• Instructions are all 32 bits
• byte(8 bits), halfword (2 bytes), word (4 bytes)
• a character requires 1 byte of storage
• an integer requires 1 word (4 bytes) of storage
• Literals:
• numbers entered as is. e.g. 4
• characters enclosed in single quotes. e.g. ‘b’
• strings enclosed in double quotes. e.g. “A string”
6
Program Structure I
• Just plain text le with data declarations, program code
(name of le should end in suffix .s to be used with SPIM
simulator)
• Data declaration section followed by program code
section
7
Data Declarations
1. placed in section of program identified with assembler directive .data.
2. declares variable names used in program; storage allocated in main
memory (RAM)
Program Structure II
Comments
anything following # on a line
8
Code
1. placed in section of text identified with assembler directive .text
2. contains program code (instructions)
3. starting point for code e.g. excution given label main:,
4. ending point of main code should use exit system call
The structure of an assembly program looks like this:
Program Structure III
Program outline
# Comment giving name of program and description
# Template.s
# Bare-bones outline of MIPS assembly language program
.data # variable declarations follow this line
# …
.text # instructions follow this line
main: # indicates start of code
# …
# End of program, leave a blank line afterwards
# to make SPIM happy
9
An Example Program I
10
Pseudo instruction I
• Some instructions in this example are pseudo
instructions which will be translated to MIPS instructions
by the assembler. Here’s a list of useful pseudo-
instructions.
• mov $t0, $t1: Copy contents of register t1 to register t0.
• li $s0, immed: Load immediate into to register s0. The way this is
translated depends on whether immed is 16 bits or 32 bits.
• la $s0, addr: Load address into to register s0.
• lw $t0, addr: Load a word at address into register t0
• Similar pseudo-instructions exist for sw, etc
• Translating some pseudoinstructions
• mov $t0, $s0 —› addi $t0, $s0, 0
• li $rs, small —› addi $rs, $zero, small
• li $rs, big —› lui $rs, upper(big) ori $rs, $rs, lower(big)
• la $rs, big —› lui $rs, upper(big) ori $rs, $rs, lower(big)
11
Pseudo instruction II
1. where small means a quantity that can be represented
using 16 bits, and big means a 32 bit quantity. upper(big)
is the upper 16 bits of a 32 bit quantity. lower(big) is the
lower 16 bits of the 32 bit quantity.
2. upper(big) and lower(big) are not real instructions. If you
were to do the translation, you’d have to break it up
yourself to figure out those quantities.
12
More Information
• For more information about MIPS instructions
and assembly programing you can refer to:
• 1. Lecture slides and textbook.
• 2. Google
13
What is SPIM
• SPIM is a MIPS32 simulator.
• Spim is a self-contained simulator that runs MIPS32
programs.
• It reads and executes assembly language programs
written for this processor.
• Spim also provides a simple debugger and minimal
set of operating system services.
• Spim does not execute binary (compiled) programs.
• Dowload it here:
• http://sourceforge.net/projects/spimsimulator/files/
14
SPIM Overview
15
Operations
• Load a source le: File —› Reinitialize and Load File
• Run the code: F5 or Press the green triangle button
• Single stepping: F10
• Breakpoint: in Text panel, right click on an address to set
a breakpoint there.
16
System calls in SPIM
• SPIM provides a small set of operating system-like
services through the system call ( syscall ) instruction.
17
System calls in SPIM
• To request a service, a program loads the system call code into
register $v0 and arguments into registers $a0 – $a3 (or $f12 for
floating-point values). System calls that return values put their
results in register $v0 (or $f0 for floating-point results). Like this
example:
18
Using system call
.data
str: .asciiz “the answer = ”
.text
li $v0, 4 # system call code for print_str
la $a0, str # address of string to print
syscall # print the string
li $v0, 1 # system call code for print_int
li $a0, 5 # integer to print
Run An Example Program
• Download the le from course FTP server and run it on
your computer.
19
Lab Assignment
• Finish these two assignments and submit your code (.s
file) to yyliang.fit.must@gmail.com before Oct. 31
(midnight).
• 1. Write an assembly program that outputs all the digits in your
student ID (e.g. sid 1409853aii101234 should output
1409853101234).
• 2. Write an assembly program that outputs the odd digits in your
student ID (e.g. sid 1409853aii101234 should output 1953113).
The SID is required to be declared as an array of word in the
data segment.
20
mailto:yyliang.fit.must@gmail.com
CO101�Principle of Computer Organization
Overview
Abstraction of Computer
Registers
Register Names and Descriptions
Data Types and Literals
Program Structure I
Program Structure II
Program Structure III
An Example Program I
Pseudo instruction I
Pseudo instruction II
More Information
What is SPIM
SPIM Overview
Operations
System calls in SPIM
System calls in SPIM
Run An Example Program
Lab Assignment