CC2302 COAL Lab # 01
CC2302 COAL Lab # 01
CC2302 COAL Lab # 01
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).
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.
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.
.data
# Data declaration section
St1: .asciiz " this is my first program in MIPS assembly!\n"
.text
# 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