Sunteți pe pagina 1din 7

Lab # 01

Introduction to MIPS Assembly &


QtSpim

1. Introduction to MIPS Assembly


1.1. Assembly Language
Assembly language is closely related to machine language (sequence of bits). It
is in human readable form. ‘Assembler’ is a program which converts the assembly
language into the machine language. Every architecture has its unique assembly
language.

1.2. MIPS Instruction Set


MIPS is a reduced instruction set computer (RISC) instruction set architecture
(ISA) developed by MIPS Technologies (formerly MIPS Computer Systems).
The early MIPS architectures were 32-bit, with 64-bit versions added later.
MIPS supports several instructions all of which may not be mentioned here.
However, there are few instructions which form the basis for rest of the
instructions. MIPS is a Load/Store architecture, so most of the instructions are
register based. MIPS stands for Microprocessor without Interlocked Pipeline
Stages. Do not confuse it with Million Instructions Per Second (MIPS). The
number of MIPS (million instructions per second) is a general measure of
computing performance and, by implication, the amount of work a larger
computer can do.
R-Type Instructions Jump Instructions
add $t1, $t2, $t3 j label
sub $t1, $t2, $t3 jal label
and $t1, $t2, $t3 jr $ra
or $t1, $t2, $t3 ADD Immediate
slt $t1, $t2, $t3 addi $t1, $t2, offset
Memory Based Instructions Pseudo instructions
lw $t1, offset($t2) li $t1, Value (Any 32bit
sw $t1, offset($t3) integer value)
move $t1, $t2
mul $1, $t2, $t3

1.3. Registers
The following table explains the 32 registers of MIPS for integer operations.

1.4. Pseudo Instructions


QtSpim can also interpret what are called pseudo-instructions. These are
instructions that are written by the program writer to perform a specific
purpose not necessarily supported by the MIPS ISA. These instructions are
translated by QtSpim into native instructions. Certain other pseudo-
instructions will be translated into two native instructions; for example, "bgt",
or "Branch if Greater Than" which combines the functionality of a "Set if Less
Than" and a "branch" instruction. Keep in mind that when running a program
with pseudo-instructions the text segment panel will display the native
instruction version of the pseudo-instructions and pseudo-instructions such as
bgt will still count as two instructions. Pseudo instructions cannot be used to
make your program more efficient; they are simply for the convenience of the
programmer. Table below contains a brief list of pseudo instructions, their
meanings, and the native instructions they translate to.

2. MIPS Assembly Programming using QtSpim


2.1. QtSpim 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.
2.2. Completing the Program
These two instructions perform the calculation that we want, but they do
not form a complete program. Much like C, an assembly language program
must contain some additional information that tells the assembler where
the program begins and ends. The exact form of this information varies
from assembler to assembler (note that there may be more than one
assembler for a given architecture, and there are several for the MIPS
architecture)
2.2.1. Labels
Labels are used to tag the address of a particular instruction. If it is
required to jump back (for implementing loops) to a particular
instruction, or jump forward (for implementing if -else); these are
mentioned in the jump instructions.
Example Code:
add.s A program that computes the sum of 1 and 2,
# leaving the result in register $t0.
# Registers used:
# t0 - used to hold the result.
# t1 - used to hold the constant 1.

main:

li $t1, 1 # load 1 into $t1.


add $t0, $t1, 2 # $t0 = $t1 + 2.

li $v0, 10 # syscall code 10 is for exit.


syscall # make the syscall.

2.2.2. Syscalls
Since you will practice MIPS assembly programming on a MIPS
simulator which runs on different processor (Intel, most probably),
running an operating system (Windows); you can neither get input
yourself from the user nor display result on screen.
Syscall is used to get the job done in this matter. Before calling syscall,
calling code should be moved to register v0. The table given below
describes the arguments required and result registers.
Syscalls are very important in QtSpim. Syscalls are used to perform
I/O operations as well as end every program. When a syscall is made,
QtSpim will check the value of register $v0 and perform a specific
function based on the given value. For example, the value 1 will print
a integer from register $a0, and the value 5 will ask for input from the
user. All input and output will be written to the console, the other
window that opened when QtSpim was opened. When the value in $v0
is 10 and a syscall is made, the program will end. Every program run
in QtSpim should finish in this way.
Getting input from user
li $v0, 5 # Call code for input is 5
syscall # make the syscall.
move $t0, $v0 # Value is read in $v0 by syscall

Displaying value in $t4


li $a0, $t4 # Because argument should be in $a0
li $v0, 1 # Call code for displaying integer is 1
syscall # make the syscall.

Exit from execution at the end of code


li $v0, 10 # syscall code 10 is for exit.
syscall # make the syscall.
Example code:
# add1.s -- A program that computes and prints the sum
# of two numbers specified at runtime by the user.

# Registers used:

# $t0 - used to hold the first number.


# $t1 - used to hold the second number.
# $t2 - used to hold the sum of the $t1 and $t2.
# $v0 - syscall parameter and return value.
# $a0 - syscall parameter.

main:
## Get first number from user, put into $t0.
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t0, $v0 # move the number read into $t0.

## Get second number from user, put into $t1.


li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t1, $v0 # move the number read into $t1.
add $t2, $t0, $t1 # compute the sum.
## Print out $t2.
move $a0, $t2 # move the number to print into $a0.

li $v0, 1 # load syscall print_int into $v0.


syscall # make the syscall

li $v0, 10 # syscall code 10 is for exit.


syscall # make the syscall.

# end of add1.s.

S-ar putea să vă placă și