Lecture 06
Lecture 06
Directive Meaning
.align n Align next datum on 2^n boundary.
.asciiz str Place the null-terminated string str in memory.
.byte b1, …, bn Place the n byte values in memory.
.data Switch to the data segment.
.double d1, …, dn Place the n double-precision values in memory.
.float f1, …, fn Place the n single-precision values in memory.
.global sym The label sym can be referenced in other files.
.half h1, …, hn Place the n half-word values in memory.
.space n Allocates n bytes of space.
.text Switch to the text segment.
.word w1, …, wn Place the n word values in memory.
MIPS INSTRUCTIONS
General format:
<optional label> <operation> <operands>
Example:
loop: addu $t2,$t3,$t4 # instruction with a label
subu $t2,$t3,$t4 # instruction without a label
L2: # a label can appear on a line by itself
# a comment can appear on a line by itself
MIPS INSTRUCTIONS
What does this look like in memory?
.data
nums:
.word 10, 20, 30
.text
.globl main
main:
la $t0, nums
lw $t1, 4($t0)
MIPS INSTRUCTION FORMATS
There are three different formats for MIPS instructions.
• R format
• Used for shifts and instructions that reference only registers.
• I format
• Used for loads, stores, branches, and immediate instructions.
• J format
• Used for jump and call instructions.
MIPS INSTRUCTION FORMATS
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R format op rs rt rd shamt funct
I format op rs rt immed
J format op targaddr
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R format op rs rt rd shamt funct
I format op rs rt immed
J format op targaddr
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R format op rs rt rd shamt funct
I format op rs rt immed
J format op targaddr
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
R format op rs rt rd shamt funct
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
I format op rs rt immed
I FORMAT INSTRUCTION ENCODING EXAMPLES
Arithmetic example:
addiu $t0,$t0,1
Fields op rs rt immed
Size 6 bits 5 bits 5 bits 16 bits
Decimal 9 8 8 1
Binary 001001 01000 01000 0000000000000001
Hexadecimal 0x25080001
I FORMAT INSTRUCTION ENCODING EXAMPLES
Fields op rs rt immed
Size 6 bits 5 bits 5 bits 16 bits
Decimal 35 18 17 100
Binary 100011 10010 10001 0000000001100100
Hexadecimal 0x8e510064
I FORMAT INSTRUCTION ENCODING EXAMPLES
Conditional branch example:
L2:instruction Note: Branch displacement is a signed value in
instruction instructions, not bytes, from the current
instruction. Branches use PC-relative
instruction
addressing.
beq $t6,$t7,L2
Fields op rs rt immed
Size 6 bits 5 bits 5 bits 16 bits
Decimal 4 14 15 -3
Binary 000100 01110 01111 1111111111111101
Hexadecimal 0x11cffffd
ADDRESSING MODES
• Addressing mode – a method for evaluating an operand.
• MIPS Addressing Modes
• Immediate – operand contains signed or unsigned integer constant.
• Register – operand contains a register number that is used to access the register file.
• Base displacement – operand represents a data memory value whose address is the sum of some
signed constant (in bytes) and the register value referenced by the register number.
• PC relative – operand represents an instruction address that is the sum of the PC and some signed
integer constant (in words).
• Pseudo-direct – operand represents an instruction address (in words) that is the field concatenated
with the upper bits of the PC.
PC Relative and Pseudo-direct addressing are actually relative to PC + 4, not PC. The reason for this will
become clearer when we look at the design for the processor, so we’ll ignore it for now.
MEMORY ALIGNMENT REQUIREMENTS
• MIPS requires alignment of memory references to be an integer multiple of the size
of the data being accessed.
• These alignments are enforced by the compiler.
• The processor checks this alignment requirement by inspecting the least significant
bits of the address.
Byte: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Half: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0
Word: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX00
Double: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX000
MIPS J FORMAT
• Used for unconditional jumps and function calls.
• The op field is used to identify the type of instruction.
• The targaddr field is used to indicate an absolute target address.
Name Fields
Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
J format op targaddr
J FORMAT INSTRUCTION ENCODING EXAMPLE
Jump example: j L1
Assume L1 is at the address 4194340 in decimal, which is 400024 in hexadecimal.
We fill the target field as an address in instructions (0x100009) rather than bytes
(0x400024). Jump uses pseudo-direct addressing to create a 32-bit address.
• Assume the values of f, g, h, i, and j are associated with registers $t2, $t3, $t4, $t5,
and $t6 respectively. Write MIPS assembly code to perform this assignment assuming
$t7 is available.
USING MIPS ARITHMETIC INSTRUCTIONS
Solution (among others):
if(i == j)
k = k + i;
• Translate into MIPS instructions assuming the values of i, j, and k are associated with
the registers $t2, $t3, and $t4, respectively.
• Translate into MIPS instructions assuming the values of a, b, and c are associated with
the registers $t2, $t3, and $t4 respectively. Assume $t5 is available.
if(a < b)
c = a;
else
c = b;
• Translate into MIPS instructions assuming the values of a, b, and c are associated with
the registers $t2, $t3, and $t4 respectively. Assume $t5 is available.