LAB # 2
INTRODUCTION TO MIPS ASSEMBLY LANGUAGE
OBJECTIVE
Introduction to MIPS Assembly language.
THEORY
The MIPS Architecture
MIPS (originally an acronym for Microprocessor without Interlocked Pipeline Stages) is a
Reduced Instruction Set Computer (RISC). MIPS is a register based architecture, meaning the
CPU uses registers to perform operations on. Registers are memory just like RAM, except
registers are much smaller than RAM, and are much faster. In MIPS the CPU can only do
operations on registers, and special immediate values. MIPS processors have 32 registers, but
some of these are reserved. A fair number of registers however are available for your use.
MIPS: registers
The MIPS registers are arranged into a structure called a Register File. MIPS comes with 32
general purpose registers named $0. . . $31. Registers also have symbolic names reflecting their
conventional use:
Introduction to MIPS Assembly Language
Assembly Language Program Template
# Title: Filename:
# Author: Date:
# Description:
# Input:
# Output:
################# Data segment #####################
.data
...
...
################# Code segment #####################
.text
.global main
main:
... # main program entry
...
li $v0, 10 # Exit program
syscall
Assembly language instruction format
Assembly language source code lines follow this format:
[label:] [instruction/directive] [operands] [#comment]
where [label] is an optional symbolic name; [instruction/directive] is either the mnemonic for an
instruction or pseudo-instruction or directive; [operands] contains a combination of one, two, or
three constants, memory references, and register references, as required by the particular
instruction or directive; [#comment] is an optional comment.
Labels
Labels are nothing more than names used for referring to numbers and character strings or
memory locations within a program. Labels let you give names to memory variables, values, and
the locations of particular instructions.
The label main is equivalent to the address of the first instruction in program1.
li $v0, 5
Directives
Directives are required in every assembler program in order to define and control memory space
usage. Directives only provide the framework for an assembler program, though; you also need
lines in your source code that actually DO something, lines like
Beq $v0, $0, end
.DATA directive
Defines the data segment of a program containing data
The program's variables should be defined under this directive
Assembler will allocate and initialize the storage of variables
You should place your memory variables in this segment. For example,
.DATA
First: .space 100
Second: .word 1, 2, 3
Third: .byte 99, 2, 3
.TEXT directive
Defines the code segment of a program containing instructions
.GLOBL directive
Declares a symbol as global
Global symbols can be referenced from other files
We use this directive to declare main procedure of a program
.ASCII Directive
Allocates a sequence of bytes for an ASCII string
.ASCIIZ Directive
Same as .ASCII directive, but adds a NULL char at end of string
Strings are null-terminated, as in the C programming language
.SPACEn Directive
Allocates space of n uninitialized bytes in the data segment
Pseudo-instructions
Pseudo-instructions give MIPS architecture set of assembly language instructions than those
implemented by the hardware. For example, one of the frequent steps needed in programming
is to copy the value of one register into another register. This actually can be solved easily by
the instruction:
add $t0, $zero, $t1
However, it is more natural to use the pseudo-instruction
move $t0, $t1.
The assembler converts this pseudo-instruction into the machine language equivalent of the
prior instruction.
MIPS-INSTRUCTIONS
Instructions Description
la Rdest, var Load Address. Loads the address of var into Rdest.
li Rdest, imm Load Immediate. Loads the immediate value imm into
Rdest.
SYSTEM I/O (INPUT/OUTPUT)
Programs do input/output through system calls
MIPS provides a special syscall instruction
To obtain services from the operating system
Many services are provided in the MARS simulators
There are10 different services provided.
Using the syscall system services
Load the service number in register $v0
Load argument values, if any, in registers $a0, $a1, etc.
Issue the syscall instruction
Retrieve return values, if any, from result registers
Service Code in $v0 Argument(s) Result(s)
Print integer 1 $a0 = number to be printed
Print String 4 $a0 = address of string in
Memory
Read Integer 5 Number returned in $v0.
Read String 8 $a0= address of input buffer
In memory.
$a1 = length of buffer (n)
Exit 10
Print Char 11 $a0 = character to print
Read Char 12 $v0 = character read
LAB TASK
1. Write an assembly program that Print a string of characters
2. Write a program to prompt and read an integer from a user