0% found this document useful (0 votes)
89 views

C04 IntroMIPSAssembly

This document discusses the MIPS assembly language. It covers basic concepts like MIPS instruction format, registers, data types, and arithmetic, logical and load/store instructions. Examples are provided to illustrate how to write simple MIPS assembly programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

C04 IntroMIPSAssembly

This document discusses the MIPS assembly language. It covers basic concepts like MIPS instruction format, registers, data types, and arithmetic, logical and load/store instructions. Examples are provided to illustrate how to write simple MIPS assembly programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CS 3204 Operating Systems

MIPS Assembly Language Basic Assembly 1

We will study the MIPS assembly language as an exemplar of the concept.

MIPS assembly instructions each consist of a single token specifying the command to be
carried out, and zero or more operation arguments:

<opcode> arg0, arg1, ... argN

The tokens are separated by commas and (optionally) whitespace. Indentation is


insignificant to the assembler, but is certainly significant to the human reader.

MIPS command tokens are short and mnemonic (in principle). For example:

add lw sw jr

The MIPS reference card bound in the front of P&H includes a complete listing of
all the MIPS commands you will need to understand and use.

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

MIPS Assembly Language Basic Assembly 2

MIPS command arguments include:


- hardware registers
- offset and base register
- literal constants (immediate arguments)
- labels

Registers are specified either as $k, where 0 ≤ k ≤ 31, or using the symbolic names
shown earlier.

Of course, MIPS assembly also allows comments. Simply, all characters from a ‘#’
character to the end of the line are considered a comment.

There are also some special directives, but those can wait...

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 1


CS 3204 Operating Systems

MIPS Hello World Basic Assembly 3


# PROGRAM: Hello, World!

.data # Data declaration section


out_string: .asciiz "\nHello, World!\n"

.text # Assembly language instructions


main: # Start of code section
li $v0, 4 # system call code for printing string = 4
la $a0, out_string # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1, ...

This illustrates the basic structure of an assembly language program.


- data segment and text segment
- use of label for data object (which is a zero-terminated ASCII string)
- use of registers
- invocation of a system call

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

MIPS Assembly Arithmetic Instructions Basic Assembly 4

All arithmetic and logical instructions have 3 operands

Operand order is fixed (destination first):

<opcode> <dest>, <src1>, <src2>

Design Principle: simplicity favors regularity.

Example:

C code: a = b + c;

MIPS ‘code’: add $s3, $s1, $s5

“The natural number of operands for an operation like addition is three…requiring every
instruction to have exactly three operands, no more and no less, conforms to the
philosophy of keeping the hardware simple”

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 2


CS 3204 Operating Systems

Assembly Arithmetic Instructions Basic Assembly 5

Design Principle: simplicity favors regularity.

Of course this complicates some things...

C code: a = b + c + d;

MIPS pseudo-code: add a, b, c


add a, a, d add
addi
addiu
Operands must be registers (or immediates), only 32 registers are provided addu
Each register contains 32 bits div
mult
Design Principle: smaller is faster. multu
sub
Why? subu
...

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Immediates Basic Assembly 6

In MIPS assembly, immediates are literal constants.

Many instructions allow immediates to be used as parameters.

addi $t0, $t1, 42 # note the opcode


li $t0, 42 # actually a pseudo-instruction

Note that immediates cannot be used with all MIPS assembly instructions; refer to your
MIPS reference card.

Immediates may also be expressed in hexadecimal: 0xFFFFFFFF

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 3


CS 3204 Operating Systems

MIPS Assembly Logical Instructions Basic Assembly 7

Logical instructions also have 3 operands:

<opcode> <dest>, <src1>, <src2>

Examples:

and $s0, $s1, $s2 # bitwise AND


andi $s0, $s1, 42
or $s0, $s1, $s2 # bitwise OR
ori $s0, $s1, 42
nor $s0, $s1, $s2 # bitwise NOR (i.e., NOT OR)
sll $s0, $s1, 10 # logical shift left
srl $s0, $s1, 10 # logical shift right

QTP: MIPS assembly doesn’t include the logical operation not. Why?

How would you achieve the effect of a logical not operation in MIPS assembly?

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Assembly Load and Store Instructions Basic Assembly 8

Transfer data between memory and registers

Example:

C code: A[12] = h + A[8];

MIPS code: lw $t0, 32($s3) # load word


add $t0, $s2, $t0
sw $t0, 48($s3) # store word

Can refer to registers by name (e.g., $s2, $t2) instead of number

Load command specifies destination first: opcode <dest>, <address>


Store command specifies destination last: opcode <dest>, <address>

Remember arithmetic operands are registers or immediates, not memory!

Can’t write: add 48($s3), $s2, 32($s3)

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 4


CS 3204 Operating Systems

Addressing Modes Basic Assembly 9

In register mode the address is simply the value in a register:

lw $t0, ($s3)

In immediate mode the address is simply an immediate value in the instruction:

lw $t0, 0

In base + register mode the address is the sum of an immediate and the value in a
register:

lw $t0, 100($s3)

There are also various label modes:

j absval
j absval + 100
j absval + 100($s3)

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

An Assembly Language Example Basic Assembly 10

Can we figure out the code?


void swap(int v[], int k) {
Conventions for procedure calls:
int temp; - arg0 is in register $4 (aka $a0)
temp = v[k]
v[k] = v[k+1]; - arg1 is in register $5 (aka $a1)
v[k+1] = temp;
} - …
- return address is in register $31 ($ra)

swap: # need label to jump to in call


sll $t0, $a1, 2 # calculate offset of v[k]
add $t0, $a0, $t0 # add offset to array base address
lw $t2, 0($t0) # load v[k] into register
lw $t3, 4($t0) # load v[k+1] into register
sw $t3, 0($t0) # store register v[k] to v[k+1]
sw $t2, 4($t0) # store register v[k+1] to v[k]
jr $ra # return to caller

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 5


CS 3204 Operating Systems

So far we’ve learned… Basic Assembly 11

MIPS
- loading words but addressing bytes
- arithmetic on registers only

# Instruction # Meaning

add $s1, $s2, $s3 # $s1 = $s2 + $s3


sub $s1, $s2, $s3 # $s1 = $s2 – $s3
lw $s1, 100($s2) # $s1 = Memory[$s2+100]
sw $s1, 100($s2) # Memory[$s2+100] = $s1

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Conditional Branch Instructions Basic Assembly 12

Decision making instructions


- alter the control flow,
- i.e., change the "next" instruction to be executed

MIPS conditional branch instructions:

bne $t0, $t1, <label> # branch on not-equal


# PC = &<label> if $t0 != $t1
beq $t0, $t1, <label> # branch on equal

Labels are strings of alphanumeric characters, underscores and periods, not beginning
with a digit. They are declared by placing them at the beginning of a line, followed
by a colon character.

bne $s0, $s1, Miss


if ( i == j )
add $s3, $s0, $s1
h = i + j;
Miss: ....

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 6


CS 3204 Operating Systems

Unconditional Branch Instructions Basic Assembly 13

MIPS unconditional branch instructions:

j Label # PC = Label
jr $ra # PC = $ra

if ( i != j ) beq $s4, $s5, Lab1


h = i + j; add $s3, $s4, $s5
else j Lab2
h = i – j; Lab1: sub $s3, $s4, $s5
Lab2: ...

Can you build a simple for loop?

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Conditional Set Instructions Basic Assembly 14

MIPS conditional set instructions:

slt $t0, $s0, $s1 # $t0 = 1 if $s0 < $s1


# $t0 = 0 otherwise
slti $t0, $s0, <imm> # $t0 = 1 if $s0 < imm
# $t0 = 0 otherwise

There are similar unsigned versions.

if ( i < j ) # $s3 == i, $s4 == j


goto A; slt $t1, $s3, $s4
else beq $zero, $t1, B
goto B;
A: # code...
j C
B: # code...
C:

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 7


CS 3204 Operating Systems

So far we’ve learned: Basic Assembly 15

# Instruction Meaning

add $s1,$s2,$s3 # $s1 = $s2 + $s3


sub $s1,$s2,$s3 # $s1 = $s2 – $s3

lw $s1,100($s2) # $s1 = Memory[$s2+100]


sw $s1,100($s2) # Memory[$s2+100] = $s1

bne $s4,$s5,L0 # Next instr. is at L0 if $s4 ≠ $s5


beq $s4,$s5,L1 # Next instr. is at L1 if $s4 = $s5

j Label # Next instr. is at Label

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Custom Control Flow Basic Assembly 16

We have: beq, bne, what about Branch-if-less-than?

Recall the set-less-than instruction:


slt $t0, $s1, $s2 if ($s1 < $s2)
$t0 = 1
else
$t0 = 0;

We can use this instruction to build "blt $s1, $s2, Label"


— can now build general control structures

Note that the assembler needs a register to do this,


— there are policy of use conventions for registers

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 8


CS 3204 Operating Systems

Program Termination Basic Assembly 17

Unlike the high-level languages you are accustomed to, MIPS assembly does not include
an instruction, or block syntax, to terminate the program execution.

MIPS programs can be terminated by making a system call:

## Exit
li $v0, 10 # load code for exit system call in $v0
syscall # make the system call to exit

Without such code, the system would attempt to continue execution into the memory
words that followed the final instructions of the program. That rarely produces graceful
results.

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Policy of Use Conventions Basic Assembly 18

MIPS programmers are expected to conform to the following conventions when using the
29 available 32-bit registers:

Name Register number Usage


$zero 0 the constant value 0
$v0-$v1 2-3 values for results and expression evaluation
$a0-$a3 4-7 arguments
$t0-$t7 8-15 temporaries
$s0-$s7 16-23 saved
$t8-$t9 24-25 more temporaries
$gp 28 global pointer
$sp 29 stack pointer
$fp 30 frame pointer
$ra 31 return address

Register 1 ($at) is reserved for the assembler, 26-27 for operating system

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 9


CS 3204 Operating Systems

Pseudo-Instructions Basic Assembly 19

You may have noticed something is odd about a number of the MIPS instructions that
have been covered so far. For example:

li $t0, 0xFFFFFFFF

Now, logically there's nothing wrong with wanting to place a 32-bit value into one of the
registers.

But there's certainly no way the instruction above could be translated into a 32-bit
machine instruction, since the immediate value alone would require 32 bits.

This is an example of a pseudo-instruction. A MIPS assembler or interpreter like SPIM


may be designed to support such extensions that make it easier to write complex
programs.

In effect, the assembler supports an extended MIPS architecture that is more sophisticated
than the actual MIPS architecture of the underlying hardware.

Of course, the assembler must be able to translate every pseudo-instruction into a


sequence of valid MIPS assembly instructions.

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Pseudo-Instruction Examples Basic Assembly 20

move $t1, $t2 # $t1 <-- $t2

or $t1, $t2, $zero # recall: x OR 0 == x

li $t1, <imm> # $t1 = 32-bit imm value

# e.g., suppose <imm> is 0x23A0FB17


#
# The assembler sometimes needs a register in which it can
# store temporary values. The register $at is reserved for
# such use.
lui $at, 0x23A0 # put upper byte in upper byte of reg,
# and 0s in the lower byte

ori $t1, $at, 0xFB18 # put lower byte into reg

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 10


CS 3204 Operating Systems

lui and ori Details Basic Assembly 21

We'd like to be able to load a 32-bit constant into a register


Must use two instructions, new "load upper immediate" instruction

lui $t0, 43690 # 1010101010101010


low-order bits
filled with
1010101010101010 0000000000000000 zeros

Then must get the lower order bits right, i.e.,

ori $t0, $t0, 39321 # 1001100110011001

1010101010101010 0000000000000000

0000000000000000 1001100110011001
ori

1010101010101010 1001100110011001

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

MIPS Directives Basic Assembly 22

Directives are special reserved identifiers used to communicate instructions to the


assembler.

Directives begin with a period character.

For example:

.data # mark beginning of a data segment


.text # mark beginning of a text (code) segment
.align # specify memory alignment of data
.space # allocate uninitialized space in memory
.word # store values in successive words
.byte # store values in successive bytes
.asciiz # store zero-terminated character sequence
.globl # specify global scope for a label

Note: technically, directives are not part of the MIPS assembly language, but rather are
instructions to the assembler to take certain actions.

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 11


CS 3204 Operating Systems

MIPS System Calls Basic Assembly 23


Service Call code Arguments Result
print integer 1 $a0 = integer to print
print float 2 $f12 = float to print
print double 3 $f12 = double to print
print string 4 $a0 = addr of null-term string
read integer 5 $v0 contains integer read
read float 6 $f0 contains float read
read double 7 $f0 contains double read
read string 8 $a0 = address of input buffer
$a1 = maximum # of chars to
be read
sbrk 9 $a0 = # of bytes to allocate $v0 contains address of allocated
memory
exit 10
print character 11 $a0 = character to print
read character 12 $a0 contains character read

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

MIPS System Calls Basic Assembly 24


Service Call code Arguments Result
open file 13 $a0 = addr of null-term string $a0 contains file descriptor
containing file name
$a1 = flags
$a2 = mode
read from file 14 $a0 = file descriptor $a0 contains number of chars read
$a1 = addr of input buffer
$a2 = max # of chars to read
write to file 15 $a0 = file descriptor $a0 contains number of chars
$a1 = address of output buffer written
$a2 = # of chars to write
close file 16 $a0 = file descriptor
exit2 17 $0 = termination result

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 12


CS 3204 Operating Systems

Data Basic Assembly 25

Basic fact: at the machine language level there are no explicit data types, only
contents of memory locations. The concept of type is present only
implicitly in how data is used.

declaration: reserving space in memory, or deciding that a certain data item will reside
in a certain register.

Directives are used to reserve or initialize memory:

.data # mark beginning of a data segment


.asciiz "a string" # declare and initialize a string
.byte 13, 14, -3 # store values in successive bytes
.space 16 # alloc 16 bytes of space
.word 13, 14, -3 # store values in successive words

A complete listing of MIPS/SPIM directives can be found in P&H Appendix A.

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

Arrays Basic Assembly 26

First step is to reserve sufficient space for the array.


Array elements are accessed via their addresses in memory, which is convenient if you’ve
given the .space directive a suitable label.
.data
list: .word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
size: .word 10
. . .
la $t1, list # get array address
li $t2, 0 # set loop counter
print_loop:
beq $t2, $t3, print_loop_end # check for array end

lw $a0, ($t1) # print value at the array pointer


li $v0, 1
syscall

addi $t2, $t2, 1 # advance loop counter


addi $t1, $t1, 4 # advance array pointer
j print_loop # repeat the loop
print_loop_end:

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 13


CS 3204 Operating Systems

Array Example Basic Assembly 27

This is part of the palindrome example from the course website:

.data
string_space: .space 1024
...
# prior to the loop, $t1 is set to the address of the first
# char in string_space, and $t2 is set to the last one
test_loop:
bge $t1, $t2, is_palin # if lower pointer >= upper
# pointer, yes

lb $t3, ($t1) # grab the char at lower ptr


lb $t4, ($t2) # grab the char at upper ptr
bne $t3, $t4, not_palin # if different, it's not

addu $t1, $t1, 1 # advance lower ptr


subu $t2, $t2, 1 # advance upper ptr
j test_loop # repeat the loop
...

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

MIPS Instruction Summary Basic Assembly 28


MIPS assembly language
Category Instruction Example Meaning Comments
add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers

Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers

add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register
store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper immediate lui $s1, 100 $s1 = 100 * 2
16 Loads constant in upper 16 bits

branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100

branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0

set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0

jump j 2500 go to 10000 Jump to target address


Uncondi- jump register jr $ra go to $ra For switch, procedure return
tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 14


CS 3204 Operating Systems

Other Issues Basic Assembly 29

Not discussed yet:


- support for procedures
- linkers, loaders, memory layout
- stacks, frames, recursion
- manipulating strings and pointers
- interrupts and exceptions
- system calls and conventions

Some of these we'll talk more about later

Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens

©William D McQuain, January 2005 15

You might also like