Lab#4 Completed

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Page|1

LAB # 4
OBJECTIVE
To introduce MIPS Assembly Language Programming using MARS Software (MIPS simulator).

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 use.

MIPS and Its Storage:

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 limited and 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 and is 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.

Overview of a MIPS CPU

The following diagram shows a simple design for a 3-Address Load/Store computer, which is
applicable to a MIPS computer.

Muhammad Owais 2020F-


Page|2

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 as memory 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 on the
system bus using a load/store command and transferred to/from the memory to the CPU. 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/from a 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 is on an
external device, such as a user terminal or disk drive, the syscall operator is used. The syscall

Muhammad Owais 2020F-


Page|3

operator allows the CPU to talk to an I/O controller to retrieve/write information to the user, disk
drive, etc.
The final part of a CPU is the Control Unit (CU). A CU controls the mechanical settings on 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 names reflecting their
conventional use:

Figure 5.3 CPU Registers


Because the number of registers is very limited, they are carefully allocated and controlled.
Certain registers are to be used for certain purposes, and the rules governing the role of the
register should be followed. The preceding list is the 32 registers (numbered 0..31) that exist in a
MIPS CPU, and their purposes.

Types of MIPS memory:


MIPS implements a 32-bit flat memory model. This means as far as a programmer is concerned,
memory on a MIPS computer starts at address 0x00000000 and extends in sequential,
contiguous order to address 0xffffffff.
A 32 bit flat memory model says that a program can address (or find) 4 Gigabytes (4G) of data.
This does not mean that all of that memory is available to the programmer. Some of that
memory is used up by the operating system (called kernel data), some of it used by the I/O
subsystem, etc. But 4G of memory which is addressable. Figure 5.4 diagrams shows how the 4G
of memory is configured in a MIPS computer.

Muhammad Owais 2020F-


Page|4

Figure 5.4 MIPS Memory Configuration

Muhammad Owais 2020F-


Page|5

Introduction to MIPS Assembly Language

# Title: Filename: Date:


# Author:
# Description: # Input:
# Output:

################# Data segment #####################


.data
...
...
################# Code segment #####################
.text
.global main main:

... # main program entry


...
li $v0, 10 # Exit program

Figure 5.5 Assembly Language Program Template


Assembly language instruction format

Assembly language programs are not compiled, they are assembled. So a program does not
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 source code lines follow this
format:

[label:] [instruction/directive] [operands] [#comment]

where [label] is an optional symbolic name; [instruction/directive] is either the


mnemonicforaninstructionorpseudo-instructionoradirective;[operands]containsa
combinationofone,two,orthreeconstants,memoryreferences,andregisterreferences, 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 instructions. The label main is equivalent to 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 Assembly Language:

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.

Muhammad Owais 2020F-


Page|6

• 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.

• Global symbols can be referenced from other files


• We use this directive to declare main procedure of a program

Common Data Definitions:

.word: 32-bit integer


.half: 16-bit integer
.byte: 8-bit integer
.space: Uninitialized memory block
.ascii: ASCII string
.asciiz: Null-terminated ASCII string
Constants: Numeric constants in MIPS Assembly Language use the same syntax as C, C++,
and Java.

0x100 Hexadecimal integer


0100 Octal integer
100 Decimal integer 1.0
Decimal float or double

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 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, address: Load a word at address into register t0. Similar pseudoinstructions
exist for sw, etc.

Muhammad Owais 2020F-


Page|7

MIPS Instructions:

Following categories of Instruction set are available with:

• Data Transfer
• Arithmetic and Logical
• Control
• Floating Point

Data Transfer Instructions:

Integer Load and Store Instructions:

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 register operand and one memory
address or immediate operand.

Muhammad Owais 2020F-


Page|8

Instructions Description

la Rdest, var Load Address. Loads the address of var into Rdest.
li Rdest, imm Load Immediate. Loadsthe immediate value imm into Rdest.

Input/Output: System Calls:

Input and output in MIPS is performed by system calls, which are calls to operating system
kernel subprograms. To initiate a system call, we load the system call function code into
$v0. If the function requires arguments, they go into the argument registers, starting with
$a0. The system call function codes are as follows:
Code in
Service $v0 Argument(s) Result(s)

$a0 = number to be
Print integer 1 printed

$a0 = address of string in


Print String 4 memory
Number returned in
Read Integer 5 $v0.
$a0=address of input
buffer in memory. $a1
Read String 8 = length of buffer
(n)
Exit 10
Print Char 11 $a0 =character to print

Read Char 12 $v0 = character read

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 command line 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.

Muhammad Owais 2020F-


Page|9

Figure 5.1 MARS IDE


MARS Integrated Development Environment (IDE)
The IDE is invoked from a graphical interface by double-clicking the mars.jar icon that represents
this executable JAR file. The IDE provides basic editing, assembling and execution capabilities.
Hopefully it is intuitive to use. Here are comments on some features.

• 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 tool tip 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 without these 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 position and there is a checkbox to display line numbers.
They are displayed outside the editing area. If you use an external editor, MARS
provides a convenience setting that 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 entering console input into a
pop-up dialog then echoes to the message area. The MARS Messages tab is used for other
messages such as assembly or runtime errors and informational messages. You can click
on assembly error messages to select the corresponding line of code in the editor.
• MIPS Registers: MIPS registers are displayed at all times, even when you are editing
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 Register File (integer registers $0 through

Muhammad Owais 2020F-


P a g e | 10

$31 plus LO, HI and the Program Counter), selected Coprocessor 0 registers (exceptions
and interrupts), and Coprocessor 1 floating 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 one file 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 also assemble all other assembly files (*.asm; *.s) in the same
directory. The results are 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, Data Segment, and
Program Labels. The major execution-time features are described below.
• 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 Text Segment 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 handled in the Run I/O tab.

Program#1: Reading and Printing an Integer


################# Code segment #####################
.text
.globl main
main: # main program entry
li $v0, 5 # Read integer
syscall # $v0 = value read
move $a0, $v0 # $a0 = value to print
li $v0, 1 # Print integer
syscall

li $v0, 10 # Exit program


syscall

STEP#1: Load mars simulator, copy this code to the editor and save file with .asm extension.

Muhammad Owais 2020F-


P a g e | 11

STEP# 2: Assemble program by pressing F3.

Muhammad Owais 2020F-


P a g e | 12

STEP# 3
Execute program by pressing F5.
Type any integer number for input.

LAB TASKS
1. Write an assembly program that Read and Print your Roll No.
INPUT:

RESULT:

Muhammad Owais 2020F-


P a g e | 13

2. Write an assembly program that Read and Print Last Alphabet of your Name.
INPUT:

RESULT:

Muhammad Owais 2020F-


P a g e | 14

3. Write an assembly program that Read and Print Your Full Name.
INPUT:

RESULT:

Muhammad Owais 2020F-


P a g e | 15

Muhammad Owais 2020F-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy