Introduction To Mips Assembly Language: Objective Theory
Introduction To Mips Assembly Language: Objective Theory
Introduction To Mips Assembly Language: Objective Theory
OBJECTIVE
Introduction to MIPS Assembly language.
THEORY
The MIPS Architecture
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:
# 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
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
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.
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