You are on page 1of 13

University of Management and Technology

Department of Artificial Intelligence


CC2302L Computer Organization and Assembly Language Lab
Spring 2024
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 01: Introduction to MIPS Assembly Language


1. Learning Objectives
 Identify and explain the core components of the MIPS architecture
 Understanding of key concepts in MIPS assembly programming
2. Introduction
MIPS (Microprocessor without Interlocked Pipelined Stages) is a RISC (Reduced Instruction Set
Computing) instruction set architecture (ISA) commonly used in embedded systems and some
processors. Assembly language for MIPS provides a low-level way to program these processors,
giving you more control over the hardware compared to high-level languages (HLLs) like C++.
1.1. Basic Components
1.1.1. Instructions
These are the basic operations the processor can perform, such as arithmetic, data movement,
and control flow. MIPS instructions are typically short and encoded in 32 bits (for MIPS32).

1.1.2. Registers
These are small, high-speed memory locations within the CPU used to hold temporary data
during program execution. MIPS has a set of predefined registers for different purposes.
1.1.3. Memory
This is the main storage area for data and instructions. Assembly language allows you to access
and manipulate memory locations.

• Reserved - This is memory which is reserved for the MIPS platform. Memory at these
addresses is not useable by a program.
• Program text - (Addresses 0x0040 0000 - 0x1000 00000) This is where the machine code
representation of the program is stored. Each instruction is stored as a word (32 bits or 4 byte) in
this memory. All instructions fall on a word boundary, which is a multiple of 4 (0x0040 0000,
0x0040 0004, 0x0040 0080, 0x0040 00B0, etc).
• Static data - (Addresses 0x1001 0000 - 0x1004 0000) This is data which will come from the
data segment of the program. The size of the elements in this section are assigned when the
program is created (assembled and linked), and cannot change during the execution of the
program.
• Heap - (Addresses 0x1004 0000 - until stack data is reached, grows upward) Heap is dynamic
data which is allocated on an as-needed basis at run time (e.g. with a new operator in Java). How
this memory is allocated and reclaimed is language specific. Data in heap is always globally
available.
• Stack – (Addresses 0x7fff fe00 - until heap data is reached, grows downward) The program
stack is dynamic data allocated for subprograms via push and pop operations. All method local
variables are stored here. Because of the nature of the push and pop operations, the size of the
stack record to create must be known when the program is assembled.
1.1.4. Directives
These are special instructions that tell the assembler how to process the code, such as defining
data sections or indicating the program entry point.
a) Data Segment Directives
.data: This directive marks the beginning of the data segment, where you define initialized
variables. You can then use instructions like .word, .half, or .asciiz to specify the data
type and value of each variable. For example,
.data
message: .asciiz "Hello, world!" # String variable
count: .word 10 # Integer variable (word = 4 bytes)
.bss: This directive marks the beginning of the bss segment, where you define uninitialized
variables. These variables will be allocated memory but won't have any initial value.
.bss
array: .space 100 # Uninitialized array with 100 bytes
b) Text Segment Directives
.text: This directive marks the beginning of the text segment, where you write your program's
executable instructions. This is where the actual machine code for your program resides.
.text
main:
c) Other Directives
.comment text: This directive allows you to include comments within your assembly code
for better readability and documentation. Comments are ignored by the assembler.
.comment This is a comment
1.2. Key Concepts
1.2.1. Instruction Set
MIPS has a large set of instructions covering various operations. You'll learn about basic
arithmetic (add, subtract, multiply, etc.), logical operations (AND, OR, NOT), data movement
instructions (load, store), and control flow instructions (if-then-else, loops).

1.2.2. Assembly Process


Writing assembly code involves creating a text file with instructions and directives. An
assembler then translates this code into machine code (binary instructions) that the processor can
understand.
1.2.3. Addressing Modes
These specify how to determine the memory location for data operands in instructions. MIPS
supports various addressing modes like immediate (value included in the instruction), register
(using a register's value as address), and base displacement (adding a constant to a register's
value).
1.2.4. Subroutines (Functions)
Assembly language allows you to define and call subroutines, which are reusable blocks of code.
You'll learn about parameter passing, stack usage, and function return mechanisms.
1.3. Benefits of Learning MIPS Assembly
1.3.1. Understanding Low-Level Operations
You gain a deeper understanding of how computers work at the hardware level, which can be
valuable for various fields like computer architecture and embedded systems development.
1.3.2. Optimizing Code
In some cases, assembly language allows for more efficient code compared to HLLs, especially
for performance-critical tasks.
1.3.3. Reverse Engineering
Assembly knowledge can be helpful for understanding and analyzing existing machine code for
debugging or security purposes.

3. Introduction to MARS
MARS (MIPS Assembly Language Simulator) is a free, user-friendly software tool specifically
designed for developing and debugging MIPS assembly language programs. It provides a
comprehensive environment to write, assemble, run, and analyze your MIPS code. Here are key
features of MARS:
1.4. Editor
MARS provides a built-in editor where you can write your MIPS assembly code. It offers basic
text editing functionalities and syntax highlighting for better readability.
1.5. Assembler
The integrated assembler translates your assembly code into its equivalent machine code
(executable instructions) that the MIPS processor can understand. It detects syntax errors and
displays them within the editor for debugging.
1.6. Simulator
This is the heart of MARS. It allows you to step through your program line by line, observing
how registers and memory change with each instruction execution. You can control the
program's execution speed, set breakpoints to pause at specific points, and single-step through
instructions for detailed analysis.
1.7. Data Windows
MARS provides separate windows to view and modify the contents of registers and memory
locations. You can inspect register values and memory addresses during program execution to
understand data flow.
1.8. Disassembly Window
This window displays the machine code (binary instructions) generated from your assembly
code. You can correlate assembly instructions with their corresponding machine code for a
deeper understanding.
1.9. Other Utilities
MARS offers additional tools like data cache simulator and performance analysis features, which
can be helpful for advanced debugging and optimization.
4. First program in MARS IDE
1.10. Creating a New MIPS Program
 Begin by opening a new file. You can access this option through the "File" menu and then
selecting "New".
 The newly created file will have an extension of ".asm," which signifies it's an assembly
language file.
 Here's where you'll write your MIPS code.
1.11. Saving Your Program
 Once you've finished writing your code, it's time to save it. Click on "Save As" from the
"File" menu.
 Choose the desired location where you want to save your assembly file.
 Finally, click "Save" to confirm and store your program.
1.12. Assembling and Running the Program
 After entering your program, you'll need to assemble it before running. Locate the
"Assemble" button and click it. This translates your code into machine code that
the MIPS simulator can understand.
 With the assembly complete, click the "Run" button to execute your program.
 If everything works correctly, you should see the message "Hello World" displayed in the
designated output area (Run I/O box). Congratulations! You've successfully written,
assembled, and run your first MIPS program.
5. Program Explanation
This MIPS program is a very basic example that prints "Hello World" to the console
MIPS Code Comment
.text # Define the program instructions.
main: # Label to define the main program.
li $v0,4 # Load 4 into $v0 to indicate a print string.
la $a0, greeting # Load the address of the greeting into $a0.
syscall # Print greeting. The print is indicated by
# $v0 having a value of 4, and the string to
# print is stored at the address in $a0.
li $v0, 10 # Load a 10 (halt) into $v0.
syscall # The program ends.
.data # Define the program data.
greeting: .asciiz "Hello World" # The string to print.

Let's break it down line by line:


Comments
 Lines starting with # are comments and ignored by the assembler.
Program Sections
 .text: This section defines the actual instructions the program will execute.
 .data: This section defines any data used by the program, such as strings.
Program Code
 main: This label marks the beginning of the main function, which is the entry point of the
program.
 li $v0, 4: This instruction loads the value 4 into register $v0. In the MIPS
convention, register $v0 is used to specify the system call number. Here, 4 indicates a
system call for printing a string.
 la $a0, greeting: This instruction loads the address of the label greeting (which
stores the string) into register $a1. Register $a0 is typically used to hold the address of
the first argument for the system call.
 syscall: This instruction triggers a system call. The specific action is determined by the
value in $v0 (which is 4 for printing a string).
 li $v0, 10: This instruction loads the value 10 into register $v0. In MIPS, 10 signifies
the system call for exiting the program.
 syscall: This system call halts the program execution.
Program Data
 greeting: .asciiz "Hello World": This line defines a label greeting and uses
the .asciiz directive to declare a string literal "Hello World" with a null terminator (\0) at
the end. The null terminator is important for the system call to know how long the string
is.
6. List of MIPS Instructions
A complete list of all syscall be found by using the help menu option (or F1) in MARS. This will
bring up the following screen, which gives all the possible options in MARS.
7. Text Segment
The Text Segment in MARS displays crucial information related to the code within your MIPS
assembly program. Here's a breakdown of what each column represents:

1.13. Address (hexadecimal)


This column shows the memory address of each instruction in the Text Segment. Memory
addresses in MIPS are typically represented in hexadecimal format (base 16). The starting
address of the Text Segment often begins at a specific value depending on the MIPS ISA (e.g.,
0x00400000 for MIPS32). Understanding memory addresses can be helpful for debugging
purposes, especially when dealing with memory access instructions or data placement within the
program.
1.14. Code (binary)
This column displays the machine code representation of each assembly instruction. Machine
code is the binary format that the MIPS processor directly executes. Each instruction is typically
encoded in 32 bits (for MIPS32) and consists of fields for the opcode (identifies the operation),
register numbers (source and destination), and immediate values (constants included in the
instruction).
1.15. Basic
This column shows the most basic representation of the assembly instruction, often containing
the opcode and essential operands. It might omit some details present in the original assembly
source code for a more concise view. The "Basic" view can be helpful for quickly identifying the
core operation of each instruction.
1.16. Source
This column displays the original assembly language source code that you wrote for each
instruction. It includes any comments, labels, or additional details you might have included in
your code. The "Source" view is essential for understanding the programmer's intent and the
overall logic of the code within the Text Segment.

8. Data Segment
The data segment in MARS contains address and hexadecimal values stored on those addresses.
This segment offers to view various segments of memory such as .data, .extern, heap, current
$gp, current $sp, .text, .kdata and MMIO.

This segment also offers to view data as ASCII characters


9. Register in Data Window
You can inspect register values during program execution to understand data flow and register
values.

10. Step-by-Step Debugging in MARS


MARS provides a powerful feature for debugging your MIPS programs one step at a time. This
allows you to examine how your code executes line by line and identify any errors or unexpected
behavior. Here's how it works:
1.17. Run One Step at a Time
Click the button for "Run one step at a time”. This will execute a single instruction in
your program and then pause.
1.18. Monitor Program State
Text Segment: The currently executing instruction will be highlighted in yellow, making it easy
to see which line of code is being processed.
Data Segment: Any variables in the data segment that are being modified by the instruction will
be highlighted in green. This helps you track how data values change as your program
progresses.
Registers: Registers whose values are used or updated by the instruction might also be
highlighted or displayed differently, depending on MARS's implementation. This allows you to
monitor register changes and their impact on the program's execution.

MIPS FIRST PROGRAM


The following program displays the string "this is my first program in MIPS assembly" on
screen.

.data
# Data declaration section
St1: .asciiz " this is my first program in MIPS assembly!\n"

.text

main: # Start of code section

# Load the address of the message


# into the $a0 register. Then load 4 into

# the $v0 register to tell the processor


# that you want to print a string.
la $a0, St1
li $v0, 4
syscall
# Now do a graceful exit
li $v0, 10
syscall

# END OF PROGRAM

Lab Task
Task #1
 Open the MARS IDE

 Copy the above code in new file and save the file with extension .asm i.e.
First_program.asm
 Then go to the RUN tab.
 Assemble the .asm file using assemble command (F3).
 Run the exe file using Go command (F5).
 Observe the output

Task # 2:
Write an assembly language program using MIPS syntax.
1. Print your Name
2. Print your Registration ID
3. Print your section
4. Output in following format:

Name: xyz
ID: 1234
Section: B

You might also like