Mips and Mars

In order to study how the operating system interacts with the hardware, Mips assembly will be used.

To edit and execute Mips assembly programs we will use Mars (Mips Assembler and Runtime Simulator). Mars is available on the department Linux system.

Mars will run on any system (including Windows) as long as you have Java installed. If you prefer, you may download and install Mars on your private computer.

Subsections of Mips and Mars

Mips memory layout

To execute a MIPS program memory must be allocated. The MIPS computer can address 4 Gbyte of memory, from address 0x0000 0000 to 0xffff ffff. User memory is limited to locations below 0x7fff ffff. In the below figure the layout of the memory allocated to a MIPS program is shown.

The purpose of the various memory segments:

  • The user level code is stored in the text segment.
  • Static data (data know at compile time) use by the user program is stored in the data segment.
  • Dynamic data (data allocated during runtime) by the user program is stored in the heap.
  • The stack is used by the user program to store temporary data during for example subroutine calls.
  • Kernel level code (exception and interrupt handlers) are stored in the kernel text segment.
  • Static data used by the kernel is stored in the kernel data segment.
  • Memory mapped registers for IO devices are stored in the memory mapped IO segment.

Clone repository

Before you continue, you must clone the mips-examples repository.

Use the git command

From the terminal, navigate to a directory where you want the cloned directory to be created and execute the following command.

git clone https://github.com/os-assignments/mips-examples.git

Now you should see something similar to this written to the terminal.

Cloning into 'mips-examples'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 9 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), done.
Checking connectivity... done.

Use the tree command

To get an overview of the cloned repository, use the tree command.

tree mips-examples

Now you should see a tree view of all files and directories in the mips-examples directory.

mips-examples/
├── README.md
├── arrays.s
├── basics.s
├── hello.s
└── jump_and_branches.s

0 directories, 5 files

You are now ready to continue with the Introduction to MARS tutorial.

Introduction to Mars

This is a short guide on how to launch and use Mars. Mars will run on any system (including Windows) as long as you have Java installed. If you prefer, you may download and install Mars on your private computer.

Launch Mars

Log in to the department Linux system. From the Applications menu you find Mars under Programming.

Mars should now start and you should see something similar to this.

IDE overview

Mars is an Integrated Development Environment (IDE) for Mips Assembly Language Programming.

Top level menu

At the top you find the top level menu: File, Edit, Run, Settings, Tools and Help.

Tools and operations

Under the top level menu a collection of icons show some of the most commonly used tools and operations. The most important of these controls are described in the below table.

ControlDescription
Load a file.
Assemble the program in the Edit tab.
Save the current file.
Run the assembled program to completion (or breakpoint).
Execute a single instruction (single-step).
Undo the last instruction (single-step backwards).
Adjust the execution speed.

Edit and Execute

In the middle left you find two tabs: Edit and Execute.

  • The Edit tab will be used to edit assembly code.
  • The Execute tab shows the Text Segment (machine instructions) and Data Segment during execution.

Registers

To the right you find the registers pane. Here the contents of all registers are shown. There are three register tabs:

  1. General purpose registers.
  2. Coprocessor 0 registers.
  3. Coprocessor 1 registers.

Mars Messages and Run I/O

In the lower left corner there are two tabs: Mars Messages and Run I/O.

The Mars Messages tab is used for displaying assembly or runtime errors and informational messages. You can click on assembly error messages to highlight and set focus on the corresponding line of code in the editor.

The Run I/O tab is used at runtime for displaying console output and entering console input as program execution progresses.

The mips-examples repository

Before you continue you should already have cloned the mips-examples repository.

Load hello.s into Mars

Among the examples programs in the misp-exmples repository you find hello.s. Open the file hello.s in the Mars simulator by selecting Open from the File menu. You should now see something similar to this.

After you loaded a program, the source code is available for edit in the Edit pane.

Change font colors

The default font colors might not be the most pleasing on your eyes. Especially the font color for comments may be hard to read. From the top menu, follow these steps to change the font colors:

  1. Settings
  2. Editor …
  3. Now a window named Text editor settings will open.
  4. To the right you find the Syntax styling options.
  5. Un-check the checkbox to override the default font.
  6. Click on the button to the right of the font preview to change the color of the font.
  7. Click on the button Apply and close in the lower left corner of the window to apply your settings.

Assemble

To translate the Mips assembly source code to executable machine instructions an assembler is used.

  • Assemble the file by clicking on the icon with the screwdriver and wrench.

Mars Messages

You should see something similar to the following in the Mars Messages display pane.

Assemble: assembling /path/to/file/hello.s

Assemble: operation completed successfully.

Execute pane

After a successful assembly, the generated machine code instructions together with the source code instructions are shown in the Execute pane.

Run to completion

  • Click on the play icon to run the program to completion.

Run I/O

In the the Run I/O display window you should see the following output.

Hello World!
-- program is finished running --

Symbol table

From the Settings menu, select Show Label Window (symbol table). Now, the following window should appear.

The symbol table shows the actual memory addresses of all labels. For example, we see that the label main is a mnemonic for the memory address 0x00400000. When you click on a label in the symbol table, the address of the label is highlighted in the text or data segment.

Text segment

In the symbol table, click on the label main. Now the following row should be highlighted with a blue border in the Source code column in the Text segment area in the Execute tab.

li $v0, 4 # Systemcall code print_str

If you look at the source code (press the Edit tab) you see that this is the instruction following directly after the label main.

Data segment

In the symbol table, click on the label msg. Now address 0x10010000 should be highlighted with a blue border in the Data Segment area in the Execute tab. The value store at this address is 0x6c6c6548.

If you study the data segment in detail you see that the string "Hello World!" is stored byte for byte starting at address 0x10010000.

Breakpoints and debugging

A very usefull feature of Mars is the ability to set breakpoints. Breakpoints together with single-stepping and backward single-stepping are very powerful when trying to understand or debugging a program.

  • Assemble the file.

Make sure to view the Execute tab. In the Text segment, click the Bkpt (breakpoint) checkbox at address 0x00400000.

This will set a breakpoint on the syscall instruction.

  • Click on the play icon to run the program.

The execution will now halt at the breakpoint (the syscall instruction).

  • Click on the single-step icon to continue the execution one instruction at the time.
  • Click on the single-step-backwards icon to run the execution backwards one instruction at the time.

Study the example programs

You are now ready to continue and study the other example programs in the mips-examples repository.

Mips assembly examples

To help you refresh your MIPS assembly skills you are strongly encouraged to study this small collection of example programs. Each program demonstrates a small collection of features of the MIPS assembly language.

The mips-examples repository

Before you continue you should already have cloned the mips-examples repository. If you have not done this already, follow these instructions before you continue. For each of the example files in the repository:

  1. Read the source code.
  2. Load the program in MARS and single step the program.
  3. Try to understand how the program works.
  4. Add your own experiments.

hello.s

File
hello.s
Description
A small “Hello World” program.
Assembly directives
.data, .asciiz and .text
Instructions
li and la.
System calls
print_string and exit.

basics.s

File
basics.s
Description
The basics of MIPS assembly.
Assembly directives
.data, .text, .space, .word and .asciiz.
Instructions
li, la, lw, add, addi, sw and move.
System calls
print_string, print_int, print_char and exit.

jump_and_branches.s

File
jump_and_branches.s
Description
Unconditional and conditional branches.
Implemented control structures
if-then-else and infinite loop.
Assembly directives
.data, .text, and .asciiz.
Instructions
j, blt and bge.

arrays.c

File
arrays.s
Description
Allocation and usage of arrays.
Data structures
Array of integers and array of strings.

subroutines.s

File
subroutines.s
Description
A short introduction to the basic use of subroutines.
Instructions introduced
jal (Jump And Link) and jr (Jump Register).
Register introduced
$ra (Return Address)
Concepts introduced
The register use convention.