C04 IntroMIPSAssembly
C04 IntroMIPSAssembly
MIPS assembly instructions each consist of a single token specifying the command to be
carried out, and zero or more operation arguments:
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
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
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Example:
C code: a = b + c;
“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
C code: a = b + c + d;
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Note that immediates cannot be used with all MIPS assembly instructions; refer to your
MIPS reference card.
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Examples:
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
Example:
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
lw $t0, ($s3)
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)
j absval
j absval + 100
j absval + 100($s3)
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
MIPS
- loading words but addressing bytes
- arithmetic on registers only
# Instruction # Meaning
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
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.
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
j Label # PC = Label
jr $ra # PC = $ra
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
# Instruction Meaning
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Unlike the high-level languages you are accustomed to, MIPS assembly does not include
an instruction, or block syntax, to terminate the program execution.
## 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
MIPS programmers are expected to conform to the following conventions when using the
29 available 32-bit registers:
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
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.
In effect, the assembler supports an extended MIPS architecture that is more sophisticated
than the actual MIPS architecture of the underlying hardware.
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
1010101010101010 0000000000000000
0000000000000000 1001100110011001
ori
1010101010101010 1001100110011001
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
For example:
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
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
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.
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
.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
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
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
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens
Computer Science Dept Va Tech January 2006 Intro Computer Organization ©2006 McQuain & Ribbens