Professional Documents
Culture Documents
Instructions and Addressing
Instructions and Addressing
Instructions and Addressing
in MiniMIPS
Sachin Gajjar
sachin.gajjar@nirmauni.ac.in
Reading Material
• Chapter 5 Instructions and Addressing
• Book - Computer Architecture from Microprocessor to Super
Computer by Behrooz Parahami, Oxford Publications
TOPICS
1. Abstract View of MiniMIPS Hardware
2. Instruction Formats
3. Simple Arithmetic and Logic Instructions
4. Load and Store Instructions
5. Jump and Branch Instructions
6. Addressing Modes
MiniMIPS; is a hypothetical machine that is close to the real MIPS processors, a
family of processors offered by a company MiniMIPS
MiniMIPS (minimal MIPS)
• A RISC (reduced instruction-set computer) –simplicity and uniformity
of instruction formats, designs that aim to execute the most commonly
used operations as fast as possible
• The MiniMIPS (minimal MIPS) is a load/store instruction set,
• meaning that data elements must be copied or "loaded" into registers
before they can be processed;
• operation results also go into registers and must be explicitly copied
back into memory through separate "store" operations.
MiniMIPS (minimal MIPS)
• The assembly language programmer is concerned with:
Registers
Memory locations where data might be stored
Machine instructions that operate on and store data in registers or
memory
Memory and processing subsystems for MiniMIPS.
. . .
Loc 0 Loc 4 Loc 8 m 2 32
4 B / location Memory
up to 2 30 words Loc Loc
m 8 m 4
. . .
32 bit wide, 32 GPP
EIU $0 Execution FPU $0 Floating -
(M ain proc.) $1 & integer (C oproc. 1) $1 point unit
$2 unit $2
$31 $31
Integ er FP
ALU
mul/di v arith
Hi Lo
TMU BadVaddr Tra p &
(C oproc. 0) Status memo ry
Cause unit
Chapter Chapter Chapter EPC
10 11 12
These registers are part of coprocessor 0's register set and are accessed by
the lwc0, mfc0, mtc0, and swc0 instructions.
Storage Conventions
MiniMIPS uses the "big endian" scheme
MSB LSB
Register Register
Instruction file Data cache file
cache (not used)
P $17 ALU
C $18
$24
Figure 5.3 A typical instruction for MiniMIPS and steps in its execution.
2. Instruction Formats
MiniMIPS add & subtract instructions; e.g., compute:
g = (b + c) (e + f)
add $t8,$s2,$s3 # put the sum b + c in $t8
add $t9,$s5,$s6 # put the sum e + f in $t9
sub $s7,$t8,$t9 # set g to ($t8) ($t9)
op rs rt operand / offset
31 25 20 15 0
I 6 bits 5 bits 5 bits 16 bits
Opcode Source Destination Imm ediate operand
or base or data or address offset for load/store/branch
MiniMIPS instructions come in only three formats: register (R), immediate (I), and jump (J).
3. Simple Arithmetic and Logic Instructions
• ADD/SUB instructions work on registers containing whole (32-bit) words.
add $t0,$s0,$s1 # set $t0 to ($s0)+($s1)
sub $t0,$s0,$s1 # set $t0 to ($s0)-($s1)
Below is the machine representations of the instructions
op rs rt rd sh fn
31 25 20 15 10 5 0
R 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 x 0
ALU Source Source Destination Unused add = 32
instruction register 1 register 2 register sub = 34
The arithmetic instructions add and sub have a format that is common to all two-operand ALU
instructions. For these, the fn field specifies the arithmetic/logic operation to be performed.
3. Simple Arithmetic and Logic Instructions
• Logical instructions operate on a pair of operands on a bit-by-bit basis.
Logical instructions in MiniMIPS include the following:
and $t0,$s0,$s1 # set $t0 to ($s0)($s1)
or $t0,$s0,$s1 # set $t0 to ($s0)($s1)
xor $t0,$s0,$s1 # set $t0 to ($s0)($s1)
nor $t0,$s0,$s1 # set $t0 to (($s0)($s1))’
3. Simple Arithmetic and Logic Instructions
• Machine representations for these logic instructions are similar to that
of ADD/SUB, but with the function field holding 36 (100100) for and,
37 (100101) for or, 38 (100110) for xor, and 39 (100111) for nor.
op rs rt rd sh fn
31 25 20 15 10 5 0
R 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 x 0
ALU Source Source Destination Unused add
36 = 32
- 39
instruction register 1 register 2 register sub = 34
3. Simple Arithmetic and Logic Instructions
• Often, one operand of an arithmetic or logical operation is a constant.
• While it is possible to place this constant in a register and then perform the desired
operation on two registers, it would be more efficient to use instructions that
directly specify the desired constant in an I-format (Immediate) instruction.
• Constant must be small enough to fit in 16-bit field that is available for this
purpose.
• Thus, for signed integer values the valid range is from -32,768 to 32,767, while for
hex constants any 4-digit number in the range [0x0000, 0xffff] is acceptable.
addi $t0,$s0,61 # set $t0 to ($s0)+61
3. Simple Arithmetic and Logic Instructions
addi $t0,$s0,61 # set $t0 to ($s0)+61
op rs rt operand / offset
31 25 20 15 0
I 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
addi = 8 Source Destination Imm ediate operand
Instructions such as addi allow us to perform an arithmetic or logic operation for which one operand is a small constant.
Machine representations for these logic instructions are similar to that of addi, but with the opcode
field holding 12 (001100) for andi, 13 (001101) for ori, and 14 (001110) for xori.
One use of logical instructions with I-format is Masking of bits
op rs rt operand / offset
31 25 20 15 0
I 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
addi = 8 Source Destination Imm ediate operand
• Example 5.1: Extracting fields from a word A 32-bit word in $s0 holds a byte of data in bit positions
0-7 and a status flag in bit position 10. Other bits in the word have arbitrary (unpredictable) values.
Unpack the information in this word, putting the data byte in register $t0 and the status flag in register
$t1. At the end, register $t0 should hold an integer in [0, 255] corresponding to the data byte and register
$t1 should hold a nonzero value iff the status flag is 1.
• Solution: Fields of a word can be extracted by ANDing the word with a predefined mask, a word that
has 1s in bit positions of interest and 0s elsewhere. For example, ANDing a word with Ox000000ff
(which in binary has 1s only in its 8 least significant bit positions) has the effect of extracting the
rightmost byte. Any desired flag bit can be similarly extracted through ANDing with a mask that has a
single 1 in the desired position. Thus, the following two instructions accomplish what is required:
andi St0, $s0, Ox00ff #mask with eight 1s at right end
andi $t1, $s0, 0x0400 #mask with single 1 in position 10
• Note that the nonzero value left in $t1 in case the flag bit is 1 is equal to 210= 1024. We will see later than a
shift instruction can be used to leave 0 or l in $t1 according to the flag bit value.
4. Load and Store Instructions
• Basic load and store instructions transfer whole words (32 bits) between memory registers.
• Each such instruction specifies a register and a memory address.
• The register, which is the data destination for load and data source for store, is specified in the rt
field of an I-format instruction.
• The memory address is specified in two parts that are added to yield the address: the l6-bit
signed integer in the instruction is a constant offset which is added to base value in register rs.
• In assembly language format, the source/destination register rt is specified first, followed by the
constant offset and the base register rs in parentheses.
• Putting rs in parentheses is reminiscent of the indexing notation A(i) in high-level languages.
lw $t0,40($s3) # load mem[40+($s3)] into $t0
sw $t0,A($s3) # store ($t0) into mem[A+($s3)]
# “($s3)” means “content of $s3”
Machine Instructions format Load and Store
op rs rt operand / offset
31 25 20 15 0
I 1 0 x 0 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
lw = 35 Base Data Offset rel ati ve to base
sw = 43 register register
Note on base and offset:
Memory The memory address is the sum
of (rs ) and an imm ediate value.
Address in
A[0] Calling one of these the base
base register
A[1] and the other the offset is quite
A[2] arbitrary. It would m ake perfect
sense to interpret the address
.
. Offset = 4i A($s3) as having the base A
. and the offset ($s3). However,
Element i a 16-bit base confines us to a
A[i]
of array A small portion of m emory space.
MiniMIPS lw and sw instructions and their memory addressing convention that allows
for simple access to array elements via a base address and an offset (offset = 4i leads
us to the i th word).
4. Load and Store Instructions
"load upper immediate" (lui) allows us to place an arbitrary constant in a desired register
op rs rt operand / offset
31 25 20 15 0
I 0 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
lui = 15 Unused Destination
Imm ediate operand
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Content of $s0 after the instruction is executed
The lui instruction allows us to load an arbitrary 16-bit value into the upper half of a
register while setting its lower half to 0s.
Example 5.2: Loading arbitrary bit patterns into registers
Show how each of these bit patterns can be loaded into $s0:
0010 0001 0001 0000 0000 0000 0011 1101
1111 1111 1111 1111 1111 1111 1111 1111
Solution
Upper half of bit pattern has hex representation: 0x2110
Lower half has hex representation: 0x003d
lui $s0,0x2110 # put the upper half in $s0
ori $s0,0x003d # put the lower half in $s0
Same can be done, with immediate values changed to 0xffff for the
second bit pattern. But, the following is simpler and faster:
Unconditional jump and jump through register instructions $ra is the symbolic
name for reg. $31
j verify # go to mem loc named “verify” (return address)
jr $ra # go to address that is in $ra;
# $ra may hold a return address
This register is often $ra. and the instruction jr $ra is used to effect a return from a
procedure to the point from which the procedure was called.
Machine representations of MiniMIPS Jump instructions
For the j instruction. the 26-bit address field in the instruction is augmented with 00 to the right and 4 high-order bits of
the program counter (PC) to the left to form a complete 32-bit address. (memory address is of 32 bits)
This is called pseudo direct addressing
op jump target address
31 25 0
J 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
j = 2
x x x x 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
From PC
Effecti ve tar get add ress (3 2 bits)
op rs rt rd sh fn
31 25 20 15 10 5 0
R 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
ALU Source Unuse d Unuse d Unuse d jr = 8
instruction register
Jump instruction j of MiniMIPS is a J-type instruction which is shown along with how its effective
target address is obtained.
The jump register (jr) instruction is R-type, with its specified register often being $ra.
Branch Instructions
• Conditional branch instructions allow us to transfer control to a given address when a condition of interest is
met.
• Conditional branches use PC-relative addressing - the 16-bit signed offset in the instruction is multiplied by 4
and the result added to the PC contents to get a 32-bit branch target address.
bltz $s1,L # branch on ($s1)< 0, L is label
beq $s1,$s2,L # branch on ($s1)=($s2)
bne $s1,$s2,L # branch on ($s1)($s2)
op rs rt operand / offset
31 25 20 15 0
I 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
bltz = 1 Source Zero Relati ve branch distance in words
op rs rt operand / offset
31 25 20 15 0
I 0 0 0 1 0 x 1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
beq = 4 Source 1 Source 2 Relati ve branch distance in words
bne = 5
op rs rt operand / offset
31 25 20 15 0
I 0 0 1 0 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1
slti = 10 Source Destination Imm ediate operand
• If the condition in the "if' statement were i < j, then the first instruction in the sequence
above would be replaced by the following two instructions (the rest would not change):
slt $t0,$s1,$s2 # set $t0 to 1 if i<j
beq $t0,$0,endif # branch if ($t0)=0; i.e. ij
# i.e., i not< j
Solution
Similar to the “if-then” statement, but we need instructions for the “else” part and a
way of skipping the “else” part after the “then” part.
slt $t0,$s2,$s1 # j<i? (inverse condition)
bne $t0,$zero,else # if j<i goto else part
addi $t1,$t1,1 # begin then part: x = x+1
addi $t3,$zero,1 # z = 1
j endif # skip the else part
else: addi $t2,$t2,-1 # begin else part: y = y–1
add $t3,$t3,$t3 # z = z+z
endif:...
6. Addressing Modes
• Addressing mode is method by which location of an operand is specified
within an instruction. MiniMIPS uses six addressing modes
6. Addressing Modes
1. Implied Addressing:
Operand comes from, or result goes to, a predefined place that is not explicitly
specified in the instruction.
jal instruction which always saves address of next instruction in sequence into
register $ra.
6. Addressing Modes
2. Immediate addressing:
Operand is given in the instruction itself.
Examples include addi, andi, ori, and xori instructions in which the second
operand (the lower half of it) is supplied as part of the instruction.
6. Addressing Modes
3. Register addressing:
Operand is taken from, or result placed in, a specified register.
R-type instructions in MiniMIPS specify up to three registers as locations of
their operand(s) and/or result.
Registers are specified by their 5-bit indices.
6. Addressing Modes
4. Base addressing:
Operand is in memory and its location is computed by adding an offset
(16-bit signed integer) to the contents of a specified base register.
This is the addressing mode of 1w and sw instructions.
6. Addressing Modes
5. PC-relative addressing:
Same as base addressing, but with the register always being the program
counter and the offset appended with two 0’s at the right end (word addressing
is always used).
This is addressing mode is used in and branch instruction allows for branching
to other instructions within ± 215 words of the current instruction.
6. Addressing Modes
6. Pseudo direct addressing:
In direct addressing, the operand address is supplied as part of the instruction.
For MiniMIPS, this is impossible, given that 32-bit instructions do not have
enough room to carry full 32-bit addresses.
The j instruction comes close to direct addressing because it contains 26 bits of
the jump targct address which is padded with 00 at the right end and 4 bits from
the program counter at the left to form a full 32-bit address; hence the name
"pscudodirect."
6. Addressing Modes
Addressing Instruction Other el ements invol ved Operand
Extend,
Imm ediate if required
Constant offset Me m
Base addr
Reg base Reg Add Me m
Reg f ile data Me mory data
Constant offset Me m
PC-relati ve
Add addr Me m
Me mory data
PC
Pseudodi rect PC Me m
addr Me m
Me mory data
0 39
31 25 0
J 6 bits 1 0 0 0 0 0 0 0 0 0 0 0 26
0 bits
0 0 0 0 0 0 0 1 1 1 1 0 1 NOR nor rd,rs,rt
Opcode Memory word address (byte address di vided by 4)
Note that the list has been assumed to be nonempty, containing at least the element A[0].
Thank You