Object:: The MIPS Architecture
Object:: The MIPS Architecture
Object:: The MIPS Architecture
OBJECT:
To introduce MIPS Assembly Language Programming using MARS Software (MIPS simulator).
THEORY:
One advantage of learning assembly language programming is that it directly exposes many of
the types of memory (heap, static data, text, stack and registers) used in a program, and forces
the programmer to deal with them.
Types of Storage:
To a programmer, storage in MIPS is divided into two main categories. The first category,
memory that exists in the Central Processing Unit (CPU) itself, is called register memory or more
commonly simply registers. Register memory is very limitedand contained in what is often called
a register file on the CPU. This type of memory will be called registers.
The second type of memory is what most novice programmers think of as memory andis often
just called memory. Memory for the purposes is where a programmer puts instructions and
data. So from a programmer's point of view, anything stored on a computer is stored in memory.
The following diagram shows a simple design for a 3-Address Load/Store computer, which is
applicable to a MIPS computer.
Figure 5.2: 3-address store/load computer architecture
All CPU architectures contain 3 main units. The first is the ALU, which performs all calculations such
as addition, multiplication, subtraction, division, bit-shifts, logical operations, etc. Except for
instructions which interface to units not on the CPU, such asmemory access or interactions with the
user of disks, all operations use the ALU.
Registers are a limited amount of memory which exists on the CPU. No data can be operated on in
the CPU that is not stored in a register. Data from memory, the user, or disk drives must first be
loaded into a register before the CPU can use it. In the MIPS CPU, there are only 32 registers, each
of which can be used to store a single 32 bit values. Because the number of these registers is so
limited, it is vital that the programmer use them effectively.
In order to use data from memory, the address and data to be read/written is placed onthe system
bus using a load/store command and transferred to/from the memory to theCPU. The data and
address are normally placed on the system bus using a Load Word,lw, or Store Word, sw, operation.
The data is then read/written from/to memory to/froma register. To use more than 32 data values
in a program, the values must exist in memory, and must be loaded to a register to use.
There is a second way to read/write data to/from a register. If the data to be accessed ison an
external device, such as a user terminal or disk drive, the syscall operator is used.The syscall operator
allows the CPU to talk to an I/O controller to retrieve/write information to the user, disk drive, etc.
CE-207 Computer Organization & Architecture SSUET/QR/114
The final part of a CPU is the Control Unit (CU). A CU controls the mechanical settingson the computer
so that it can execute the commands. The CU is the focus of a class which is often taught with
assembly language, the class being Computer Architecture. This class will not cover the CU in
anything but passing detail.
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 namesreflecting their conventional
use:
3
CE-207 Computer Organization & Architecture SSUET/QR/114
# Title: Filename:
Date:
# Author:
##################
Description: Data segment #####################
.data
...
...
... # main program entry
4
CE-207 Computer Organization & Architecture SSUET/QR/114
Assembly language programs are not compiled, they are assembled. So a program doesnot consist
of statements and blocks of statements as in a HLL, but a number of instructions telling the
computing machine how to execute. Assembly language sourcecode lines follow this format:
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 instructions. The label main is equivalentto the address of the first instruction in
program1.
Directives: Directives are instructions to the assembler, specifying an action to be taken during the
assembly process. One important use of directives is declaring or reserving memory variables. In
addition, directives are used to break up the program into sections. Following are some
commonly used directives of MIPS AssemblyLanguage:
Top-level Directives:
.text: indicates that following items are stored in the user text segment, typically instructions.
.data: indicates that following data items are stored in the data segment.
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
.globl sym: declare that symbol sym is global and can be referenced from other files.
5
CE-207 Computer Organization & Architecture SSUET/QR/114
Constants: Numeric constants in MIPS Assembly Language use the same syntax asC, C++, and
Java.
Pseudo-instructions:
Pseudo-instructions do not correspond to real MIPS instructions. Instead, the assembler, would
translate pseudo-instructions to real instructions (one on more instructions). Pseudo-instructions
not only make it easier to program, it can also add clarity to the program, by making the intention
of the programmer more clear. Here's a list of usefulpseudo-instructions.
MIPS Instructions:
• Data Transfer
• Arithmetic and Logical
• Control
• Floating Point
6
CE-207 Computer Organization & Architecture SSUET/QR/114
Load and store instructions are the only instructions that can access memory. Their purpose is to
move data between a memory location and a CPU register. They are not interchangeable with the
move instruction. Load and store instructions take one registeroperand and one memory address or
immediate operand.
Instructions Description
la Rdest, var Load Address. Loads the address of var into Rdest.
li Rdest, imm Load Immediate. Loadsthe immediate value imm intoRdest.
Input and output in MIPS is performed by system calls, which are calls to operatingsystem kernel
subprograms. To initiate a system call, we load the system call function code into $v0. If the
function requires arguments, they go into theargument registers, starting with $a0. The system
call function codes are as follows:
7
CE-207 Computer Organization & Architecture SSUET/QR/114
Code in
Service Argument(s) Result(s)
$v0
$a0 = number to be
Print integer 1
printed
$a0 = address of stringin
Print String 4
memory
Number returned in
Read Integer 5
$v0.
$a0=address of input
buffer in memory.
Read String 8 $a1 = length of buffer
(n)
Exit 10
Print Char 11 $a0 =character to print
Introduction to MARS:
MARS, the Mips Assembly and Runtime Simulator, will assemble and simulate the execution of MIPS
assembly language programs. It can be used either from a commandline or through its integrated
development environment (IDE).
MARS is the easiest to use, and provides the best tools for explaining how MIPS assembly and the
MIPS CUP work. MARS is an executable jar file, so you must have the Java Runtime Environment
(JRE) installed to run MARS. A link to Java is on the MARS download page, so you should install Java
if it is not on your computer. When it is started, a page which is similar to the following should come
up.
8
CE-207 Computer Organization & Architecture SSUET/QR/114
Menus and Toolbar: Most menu items have equivalent toolbar icons. If the function
of a toolbar icon is not obvious, just hover the mouse over it and a tooltip will soon
appear. Nearly all menu items also have keyboard shortcuts. Any menu item not
appropriate in a given situation is disabled.
Editor: MARS includes two integrated text editors. The default editor, new in Release
4.0, features syntax-aware color highlighting of most MIPS language elements and
popup instruction guides. The original, generic, text editor withoutthese features is still
available and can be selected in the Editor Settings dialog.It supports a single font
which can be modified in the Editor Settings dialog. The bottom border of either editor
includes the cursor line and column positionand there is a checkbox to display line
numbers. They are displayed outside theediting area. If you use an external editor,
MARS provides a convenience settingthat will automatically assemble a file as soon as
it is opened. See the Settings menu.
Message Areas: There are two tabbed message areas at the bottom of the screen.The
Run I/O tab is used at runtime for displaying console output and entering console
input as program execution progresses. You have the option of enteringconsole input
into a pop-up dialog then echoes to the message area. The MARSMessages tab is used
for other messages such as assembly or runtime errors andinformational messages.
You can click on assembly error messages to select thecorresponding line of code in
the editor.
MIPS Registers: MIPS registers are displayed at all times, even when you areediting
and not running a program. While writing your program, this serves as a useful
reference for register names and their conventional uses (hover mouse over the
register name to see tool tips). There are three register tabs: the RegisterFile (integer
registers $0 through $31 plus LO, HI and the Program Counter), selected Coprocessor
0 registers (exceptions and interrupts), and Coprocessor 1floating point registers.
Assembly: Select Assemble from the Run menu or the corresponding toolbar icon to
assemble the file currently in the Edit tab. Prior to Release 3.1, only onefile could be
assembled and run at a time. Releases 3.1 and later provide a primitive Project
capability. To use it, go to the Settings menu and check Assemble operation applies to
all files in current directory. Subsequently, the assembler will assemble the current file
as the "main" program and alsoassemble all other assembly files (*.asm; *.s) in the
same directory. The resultsare linked and if all these operations were successful the
program can be executed. Labels that are declared global with the ".globl" directive
may be referenced in any of the other files in the project. There is also a setting that
permits automatic loading and assembly of a selected exception handler file. MARS
uses the MIPS32 starting address for exception handlers: 0x80000180.
Execution: Once a MIPS program successfully assembles, the registers are initialized
and three windows in the Execute tab are filled: Text Segment, DataSegment, and
Program Labels. The major execution-time features are describedbelow.
9
CE-207 Computer Organization & Architecture SSUET/QR/114
Labels Window: Display of the Labels window (symbol table) is controlled through the
Settings menu. When displayed, you can click on any label or its associated address to
center and highlight the contents of that address in the TextSegment window or Data
Segment window as appropriate.
The assembler and simulator are invoked from the IDE when you select the Assemble,Go, or Step
operations from the Run menu or their corresponding toolbar icons or keyboard shortcuts. MARS
messages are displayed on the MARS Messages tab of the message area at the bottom of the
screen. Runtime console input and output is handledin the Run I/O tab.
STEP#1: Load mars simulator, copy this code to the editor and save file with .asmextension.
10
CE-207 Computer Organization & Architecture SSUET/QR/114
STEP# 3
Execute program by pressing F5. Type any
integer number for inpu
11
CE-207 Computer Organization & Architecture SSUET/QR/114
LAB TASKS:
TASK#1:
Write an assembly program that Read and Print your Roll No.
SOLUTION:
Write an assembly program that Read and Print last alphabet of your name.
SOLUTION:
SOLUTION: