[go: up one dir, main page]

0% found this document useful (0 votes)
20 views15 pages

COAL Lab Notes 1

The document provides a comprehensive overview of MIPS registers, detailing their types, purposes, and usage in MIPS assembly programming. It categorizes registers into general-purpose, special-purpose, and coprocessor registers, explaining the specific roles of each type. Additionally, it introduces the MARS simulator for assembling and debugging MIPS programs, outlining its features and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

COAL Lab Notes 1

The document provides a comprehensive overview of MIPS registers, detailing their types, purposes, and usage in MIPS assembly programming. It categorizes registers into general-purpose, special-purpose, and coprocessor registers, explaining the specific roles of each type. Additionally, it introduces the MARS simulator for assembling and debugging MIPS programs, outlining its features and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

Introduction to MIPS Registers


MIPS processors have 32 general-purpose registers (GPRs), each with a specific function.
These registers store temporary values, function arguments, return values, and pointers for
efficient computation.

2. Categories of Registers in MIPS


MIPS registers are divided into the following categories:
Register Type Purpose Example Registers
Fixed-purpose registers like zero and
Special Registers $zero, $at
assembler registers
Return and Argument $v0 - $v1, $a0 -
Store function return values and arguments $a3
Registers
Store temporary data (not preserved after $t0 - $t9
Temporary Registers
function calls)
Store values that must be preserved across $s0 - $s7
Saved Registers
function calls
Used by the operating system (not for user $k0, $k1
Kernel Reserved Registers
programs)
Used for function calls and memory $gp, $sp, $fp,
Stack and Pointers $ra
management

3. Detailed Explanation of MIPS Registers


3.1 Special Registers
Register Purpose
$zero (0) Always holds the value 0 (cannot be modified)
$at (1) Reserved for the assembler (do not use manually)

3.2 Function Return and Argument Registers


Register Purpose
$v0 (2) Stores the first return value of a function
$v1 (3) Stores the second return value of a function
$a0 (4) - $a3 (7) Used to pass arguments to functions

3.3 Temporary Registers (``)


• These registers are not preserved after a function call.
• Suitable for short-term calculations.
Register Purpose
$t0 (8) - $t9 (25) Store temporary values (lost after function calls)

3.4 Saved Registers (``)


• These registers must be preserved across function calls.
• If a function modifies these, it must restore them before returning.
Register Purpose
$s0 (16) - $s7 (23) Store values that need to be preserved across function calls

3.5 Kernel Reserved Registers (``)


Register Purpose
$k0 (26), $k1 (27) Reserved for OS (not for user programs)

3.6 Stack and Pointers


Register Purpose
$gp (28) Global Pointer (points to global variables)
$sp (29) Stack Pointer (points to the top of the stack)
$fp (30) Frame Pointer (used in some function call conventions)
$ra (31) Return Address (stores return address when calling a function)

4. How to Use Registers in MIPS Assembly


Example: Using Temporary and Saved Registers
.text
main:
li $t0, 5 # Store 5 in $t0 (temporary register)
li $s0, 10 # Store 10 in $s0 (saved register)
add $t1, $t0, $s0 # $t1 = $t0 + $s0

li $v0, 10 # Exit system call


syscall
5. Summary of Register Usage
Category Registers Purpose
Special Registers $zero, $at Always 0, assembler reserved
Return Registers $v0, $v1 Function return values
Function Arguments $a0 - $a3 Function parameters
Temporary Registers $t0 - $t9 Short-term calculations (not preserved)
Saved Registers $s0 - $s7 Values that must be preserved
Kernel Registers $k0, $k1 Reserved for OS
Stack & Pointers $sp, $fp, $gp, $ra Stack, frame, global pointer, return address

This document provides a complete overview of MIPS registers, ensuring clarity in


programming and function usage.
Lecture on MIPS Assembly and MARS Simulator (Detailed Explanation)

1. Introduction to MARS Simulator


1.1 Objectives
The lab has two primary goals:
1. Get familiar with the MARS simulator

o Learn about its interface, components, and usage.

2. Learn how to assemble, run, and debug a MIPS program

o Understand how a MIPS program is written, converted into machine code, and executed
in MARS.

2. What is the MARS Simulator?


MARS (MIPS Assembler and Runtime Simulator) is an Integrated Development Environment
(IDE) for writing, assembling, debugging, and running MIPS assembly programs.
• Integrated Development Environment (IDE): A software tool that allows you to write, run, and
debug programs in one place.

• MIPS Assembly Language: A low-level programming language used to communicate directly


with MIPS processors.

• Written in Java: MARS is developed in Java, meaning it runs on any operating system with Java
installed.

MARS Components
The MARS environment is divided into two main windows:
1. Edit Window
o Used for creating and modifying MIPS programs.

o Similar to a text editor, but with features for assembling MIPS code.

2. Execute Window
o Used for running and debugging MIPS programs.

o Displays the program's execution process, register values, memory contents, and errors.

Switching Between Windows


You can switch between the Edit Window and Execute Window using the tabs at the top of
MARS.

3. Understanding the MARS Execute Window


3.1 Three Main Panes in the Execute Window
1. Text Segment
o Displays machine code (binary instructions) and their memory addresses.

o This is where the actual instructions of the program are stored.

2. Data Segment
o Shows memory locations that hold variables (data).

o Any variable declared in the .data section of a MIPS program appears here.

3. Labels Pane
o Displays addresses of labeled items, such as variables and jump endpoints.

o Labels are user-defined names assigned to memory locations or instructions.

3.2 Message Areas at the Bottom


1. Mars Messages Tab
o Shows messages related to assembly errors, runtime errors, and informational
messages.

o Clicking an error message highlights the corresponding line of code.

2. Run I/O Tab


o Used for console input/output during program execution.

o Any syscall output (such as printing text) appears here.

4. MARS Execute Window Features (Diagram Explanation)


Figure 1.1: The MARS Integrated Development Environment (IDE)

Explanation of Components:
1. Execute Window’s Tab: The tab that lets you switch between the Edit and Execute windows.

2. Assembly Code Display:


o Shows MIPS instructions along with addresses and machine code.

3. Data Segment View:

o Displays variable values stored in memory.

o You can directly edit values here.

4. Memory Navigation Controls:

o Allows switching between different memory segments (stack, heap, data).

5. Decimal and Hexadecimal Toggle:

o Allows switching between number representations (Decimal: Base 10, Hexadecimal:


Base 16).

6. Labels and Memory Addresses:

o Shows labels (user-defined names) and their corresponding addresses.

7. Register Values:

o Displays CPU register contents.

o You can edit register values here.

8. Breakpoints Checkbox:

o Used to set breakpoints for debugging.

o A breakpoint stops execution at a specific instruction.

9. Execution Speed Control:

o Adjusts execution speed for step-by-step debugging.

5. Assembling, Running, and Debugging a MIPS Program


5.1 Understanding the MIPS Register Window
• Located on the right side of the screen (visible even when editing).

• Used to track register values during execution.

5.2 Register Categories in MIPS


Register Type Usage Example Registers

General Stores integer values, function arguments, $t0 - $t9, $s0 - $s7, $a0 - $a3, $v0
Purpose return values - $v1
Register Type Usage Example Registers

Special Stores special values like return addresses,


$ra, $pc, $zero
Registers program counter

Coprocessor 0 Stores exception, interrupt, and status codes $12 (status), $13 (cause)

Coprocessor 1 Stores floating point numbers $f0 - $f31

5.3 How to Assemble a Program in MARS


1. Click on the "Assemble" Button (or select Run → Assemble).

2. If there are errors, they appear in the Mars Messages tab.

3. Once assembled successfully, memory and registers are initialized.

6. Running and Debugging in MARS


6.1 Execution Options in the Run Menu
Menu Item Icon Action

Run → Assemble Converts the program into machine code

Run → Go Executes the program until completion or a breakpoint

Run → Reset Resets the program and simulator

Run → Step Executes one instruction at a time

Run → Backstep Reverses the last executed instruction

6.2 Run Speed Slider


• Allows slowing down execution to observe each instruction.

• Does not affect single-step execution.

7. Lab Tasks
7.1 Task 1: Test a Simple MIPS Program
1. Type a sample MIPS program in the Edit Window.

2. Show/Hide line numbers (View → Show Line Numbers).


3. Assemble and Run the program.

4. Observe the output in the Run I/O tab.

7.2 Task 2: Explore MARS Simulator


• Download Fibonacci.asm from shorturl.at/aKysj.

• Identify the initialized data locations and values.

• Toggle decimal/hexadecimal formats.

• Run at 3 instructions per second.

• Step through the program and track memory changes.

• Set a breakpoint before printing the result.

• Run the program at full speed and observe stopping at the breakpoint.

Final Steps for Lecture Preparation


Do you want a PowerPoint presentation or lecture notes for this session?
Would you like me to prepare some extra MIPS coding exercises?

You said:
i download MARS. This file i received Mars45.jar how to install it?

ChatGPT said:

Great! Since you downloaded Mars45.jar, follow these steps to install and run MARS on your
system.

Installing and Running MARS (MIPS Assembler and Runtime Simulator)


Step 1: Check Java Installation
Since MARS is a Java-based application, you need Java Runtime Environment (JRE) or
Java Development Kit (JDK) installed on your computer.
Check if Java is Installed
Open Command Prompt (Windows) / Terminal (Mac/Linux) and type:
sh
CopyEdit
java -version
If Java is installed, you will see an output like:
scss
CopyEdit
java version "1.8.0_341"
Java(TM) SE Runtime Environment (build 1.8.0_341-b10)
If Java is not installed, download and install it from:
Java Download

Step 2: Running MARS


Once Java is installed, follow the steps based on your operating system:
For Windows
1. Move Mars45.jar to a folder where you want to keep it (e.g., C:\MARS).

2. Double-click on Mars45.jar.

o If MARS opens, you are ready to use it!

o If nothing happens, try the command-line method below.

✅ Command-Line Method (if double-click doesn’t work):


• Open Command Prompt (cmd) and navigate to the folder where Mars45.jar is located:
sh
CopyEdit
cd C:\MARS
• Run the following command:
sh
CopyEdit
java -jar Mars45.jar
For Linux / macOS
1. Move Mars45.jar to a directory, e.g., ~/MARS/

2. Open the Terminal and navigate to the directory:


sh
CopyEdit
cd ~/MARS
3. Run MARS with:
sh
CopyEdit
java -jar Mars45.jar

Step 3: Creating a Shortcut (Optional but Useful!)


If you don’t want to type commands every time, create a shortcut:
Windows Shortcut
1. Right-click Mars45.jar → Send to → Desktop (Create Shortcut).

2. Right-click the shortcut → Properties.

3. In Target, enter:
nginx
CopyEdit
java -jar "C:\MARS\Mars45.jar"
4. Click OK and rename the shortcut to "MARS Simulator".

Linux/macOS Alias
To make launching MARS easier, create an alias:
sh
CopyEdit
echo 'alias mars="java -jar ~/MARS/Mars45.jar"' >> ~/.bashrc
source ~/.bashrc
Now, you can type mars in the terminal to launch MARS.

Step 4: Testing MARS


1. Open MARS.

2. You should see the Edit Window where you can write MIPS code.

3. Go to Help → About MARS to confirm the version.

Now, you are ready to write, assemble, and debug MIPS programs in MARS!
Let me know if you need any help running it.

You said:
TYPES OF REGISTERS AND PURPOSE OF EACH TYPE REGISTER

ChatGPT said:

In MIPS assembly language, registers are small storage locations inside the CPU that store
temporary data during execution. MIPS has 32 general-purpose registers, along with
special-purpose registers. Let's go through each type in detail.
1. Types of Registers in MIPS
MIPS registers can be classified into the following categories:
Register Type Purpose Examples

General-Purpose Registers Store temporary data and $t0 - $t9, $s0 - $s7, $a0 - $a3,
(GPRs) function arguments $v0 - $v1

Hold specific control and status


Special-Purpose Registers $zero, $ra, $pc, HI, LO
values

Coprocessor 0 (System Control Handle exceptions, status, and $12 (status), $13 (cause), $14
Registers) interrupts (EPC)

Coprocessor 1 (Floating Point Perform floating-point


$f0 - $f31
Registers) arithmetic

Now, let’s discuss each type in detail.

2. General-Purpose Registers (GPRs)


These registers are used to store temporary values, function arguments, and results. There are
32 general-purpose registers ($0 to $31), but each has a specific purpose.

2.1 Temporary Registers ($t0 - $t9)

• Used for temporary calculations.

• The values stored in these registers are not preserved after a function call.

• Example:
assembly
CopyEdit
add $t0, $t1, $t2 # $t0 = $t1 + $t2
Register Purpose

$t0 - $t7 General-purpose temporary registers

$t8 - $t9 Extra temporary registers

2.2 Saved Registers ($s0 - $s7)

• Used to store values that need to be preserved across function calls.


• If a function modifies these registers, it must restore them before returning.

• Example:
assembly
CopyEdit
add $s0, $s1, $s2 # $s0 = $s1 + $s2
Register Purpose

$s0 - $s7 Saved registers (must be restored after function calls)

2.3 Function Argument Registers ($a0 - $a3)

• Used to pass function arguments to a subroutine.

• Only four arguments can be passed via registers. More arguments are passed on the stack.

• Example:
assembly
CopyEdit
li $a0, 5 # Load function argument (5)
jal myFunction # Jump to function
Register Purpose

$a0 - $a3 Function arguments

2.4 Return Value Registers ($v0 - $v1)

• Used to store the return values of functions.

• If a function returns one value, it is stored in $v0.

• If a function returns two values, the second one is stored in $v1.

• Example:
assembly
CopyEdit
li $v0, 10 # Return value = 10
Register Purpose

$v0 - $v1 Function return values


3. Special-Purpose Registers
These registers control execution flow, hold special values, or perform specific operations.

3.1 Zero Register ($zero)


• Always holds the value 0.

• Cannot be modified.

• Example:
assembly
CopyEdit
add $t0, $t1, $zero # $t0 = $t1 + 0
Register Purpose

$zero Always stores 0

3.2 Return Address Register ($ra)


• Stores the return address when a function is called.

• Used with the jal (Jump and Link) instruction.

• Example:
assembly
CopyEdit
jal myFunction # Call function (stores return address in $ra)
jr $ra # Return to caller
Register Purpose

$ra Return address for function calls

3.3 Program Counter ($pc)


• Stores the address of the current instruction being executed.

• Automatically increments to the next instruction.

• Used in branching and jumping.

Register Purpose

$pc Stores the memory address of the current instruction


3.4 HI and LO Registers
• Used for multiplication and division results.

• HI stores the remainder of a division.

• LO stores the quotient of a division.

• Example:
assembly
CopyEdit
mul $t0, $t1, $t2 # Multiply $t1 * $t2 (result stored in HI and LO)
mflo $t3 # Move LO to $t3 (quotient)
mfhi $t4 # Move HI to $t4 (remainder)
Register Purpose

HI Stores remainder of division

LO Stores quotient of division

4. Coprocessor 0 (System Control Registers)


• Used for exception handling, status tracking, and interrupts.

Register Purpose

$12 Status Register (stores CPU state)

$13 Cause Register (stores exception causes)

$14 EPC (Exception Program Counter, stores address of faulty instruction)

5. Coprocessor 1 (Floating Point Registers)


• Used for floating-point arithmetic.

• Registers: $f0 - $f31

• Example:
assembly
CopyEdit
l.s $f0, var1 # Load floating point value
add.s $f2, $f0, $f1 # Floating point addition
Register Purpose

$f0 - $f31 Floating-point values

Summary Table: MIPS Registers


Register Type Registers Purpose

General Purpose Registers (GPRs) $t0 - $t9 Temporary variables

$s0 - $s7 Saved variables

$a0 - $a3 Function arguments

$v0 - $v1 Function return values

Special Purpose Registers $zero Always 0

$ra Return address

$pc Program counter

HI, LO Multiplication and division results

Coprocessor 0 Registers $12 Status Register

$13 Cause Register

$14 EPC (Exception PC)

Coprocessor 1 Registers $f0 - $f31 Floating point values

You might also like