0% found this document useful (0 votes)
47 views19 pages

MIPS Assembly Programming Overview

The document provides an overview of MIPS assembly programming, detailing the structure and usage of MIPS instructions, operands, and registers. It explains the types of memory in MIPS architecture, including program text, static data, heap, stack, kernel, and memory-mapped I/O. Additionally, it emphasizes the importance of registers in operations and the load/store architecture of MIPS, which facilitates data transfer between memory and registers.

Uploaded by

rusiruchapana
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)
47 views19 pages

MIPS Assembly Programming Overview

The document provides an overview of MIPS assembly programming, detailing the structure and usage of MIPS instructions, operands, and registers. It explains the types of memory in MIPS architecture, including program text, static data, heap, stack, kernel, and memory-mapped I/O. Additionally, it emphasizes the importance of registers in operations and the load/store architecture of MIPS, which facilitates data transfer between memory and registers.

Uploaded by

rusiruchapana
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/ 19

MIPS Assembly Programming

CS3221: Assembly Programming


Quick Example
Here is an example of one of the simplest and most common MIPS instructions.
add $t0, $t1, $t2
This MIPS instruction symbolizes the machine instruction for adding the contents of
register t1 to the contents of register t2 and storing the result in t0.
MIPS Instruction Operands
• MIPS instructions consist of operations on one or more operands.
• Operands in MIPS fit into one of three categories.
• Integer constants
• Registers
• Memory
Integer Constant Operands
• Integer constant operands are used frequently. For example, while
looping over an array, we might continually increment an index to
access the next array element.
• To avoid saving the constant elsewhere and having to retrieve it
during every use, MIPS allows for immediate instructions which can
include a constant directly in the instruction.
• A simple example is add immediate:
addi $s3, $s3, 4 # adds 4 to the value in $s3 and stores in $s3
Integer Constants
• Generally represented with 16 bits, but they are extended to 32 bits
before being used in an operation.
• Most operations use signed constants, although a few support
unsigned.
• Signed constants are sign-extended
• Integer constants can be represented in MIPS assembly instructions
using decimal, hexadecimal, or octal values.
• A reflection of design principle 3, make the common case fast.
• Because constants are used frequently, it is faster and more energy efficient
to support instructions with built-in constants rather than fetching them from
memory all the time.
Registers
We’ve already seen some simple register usage in our two example MIPS
instructions.
add $t0, $t1, $t2
addi $s3, $s3, 4

In these instructions, $t0, $t1, $t2, and $s3 are all registers. Registers are special
locations built directly into the hardware of the machine. The size of a MIPS register
is 32 bits. This size is also commonly known as a word in MIPS architecture.
Registers
• There are only 32 (programmer visible) 32-bit registers in a MIPS processor.
• Intel x86 has 8 registers, x86-64 has 16 registers
• Reflects design principle 2, smaller is faster.
• Having a small number of registers ensures that accessing a desired register is fast
since they can be kept closer.
• Also means that fewer bits can be used to identify registers à decreases instruction
size.
• Registers also use much less power than memory accesses.
• MIPS convention is to use two-character names following a dollar sign.
• Register 0: $zero – stores the constant value 0.
• Registers 16-23: $s0-$s7 – saved temporaries (variables in C code).
• Registers 8-15: $t0-$t7 – temporaries.
MIPS Registers
• Registers are a limited number of memory values that exist directly in
the CPU.
• In order to do anything useful with data values in memory, they must
first be loaded into 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.
MIPS Registers
• $zero ($0) - a special purpose register that always contains a constant value
of 0.
• It can be read, but cannot be written.
• $at ($1) - a register reserved for the assembler.
• If the assembler needs to use a temporary register (e.g. for pseudo instructions), it
will use $at, so this register is not available for use programmer use.
• $v0-$v1 ($2-$3) – registers are normally used for return values for
subprograms.
• $v0 is also used to input the requested service to syscall.
• $a0-$a3 ($4-$7) - registers are used to pass arguments (or parameters) into
subprograms.
MIPS Registers
• $t0-$t9 ($8-$15, $24-$25) - registers are used to store temporary
variables.
• The values of temporary variables can change when a subprogram is called.
• $s0-$s8 ($16-$24) - registers are used to store saved values.
• The values of these registers are maintained across subprogram calls.
• $k0-$k1 ($26-$27) - registers are used by the operating system, and
are not available for use programmer use.
• $gp ($28) - pointer to global memory.
• Used with heap allocations.
MIPS Registers
• $sp ($29) – stack pointer, used to keep track of the beginning of the
data for this method in the stack.
• $fp ($30) – frame pointer, used with the $sp for maintaining
information about the stack.
• This text will not use the $fp for method calls.
• $ra ($31) – return address: a pointer to the address to use when
returning from a subprogram.
Types of memory - MIPS
• 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.
• Reserved - This memory 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.
Types of memory - MIPS
• 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.
• Kernel - (Addresses 0x9000 0000 - 0xffff 0000)
• Kernel memory is used by the operating system, and so is not accessible to the user.
Types of memory - MIPS
• MMIO - (Addresses 0xffff 0000 - 0xffff 0010)
• Memory Mapped I/O, which is used for any type of external data not in
memory, such as monitors, disk drives, consoles, etc.
Types of memory - MIPS
Memory Operands
• Before we talk about memory operands, we should talk generally
about how data is stored in memory.
• As we said before, memory contains both data and instructions.
• von Neumann architecture vs. Harvard architecture
• Memory can be viewed as a large array of bytes.
• The beginning of a variable or instruction is associated with a specific element
of this array.
• The address of a variable or instruction is its offset from the beginning of
memory.
Memory Operands
• For a large, complex data structure, there are likely many more data
elements than there are registers available. However, arithmetic
operations occur only on registers in MIPS (This is why MIPS is a
load/store architecture).
• To facilitate large structures, MIPS includes data transfer instructions
for moving data between memory and registers.
• As an example, assume we have the following C code, where A is an
array of 100 words.
Memory Operands
• Let’s say g and h are associated with the registers $s1 and $s2
respectively. Let’s also say that the base address of A is associated
with register $s3.

• To compile this statement into MIPS, we’ll need to use the load word
instruction to transfer A[8] into a register.
lw $t0,32($s3) # load the element at a 32 byte offset from $s3
add $s1,$s2,$t0
• There is an equivalent store word instruction for storing data to
memory as well.
MIPS Directives

You might also like