Arithmetic Ops
Arithmetic Ops
ARCHITECTURE
Arithmetic Ops
MIPS Instruction Set
This chapter introduces the concept of a computer's instruction set, MIPS
which is the language a computer understands to command its hardware. Microprocessor without
Interlocked Pipeline Stages.
MIPS is used as the example throughout the book.
Arithmetic Operations
Arithmetic_Ops
Answer
Arithmetic_Ops
Answer
Arithmetic_Ops
Addressing
In memory, data is stored in a single-dimensional array, with the address acting as the index.
For instance, in a memory array, Memory[2] would represent the third element of the array.
Load Instruction
To access a word in memory, MIPS uses the lw (load word) instruction.
This instruction loads data from memory into a register.
The address for the memory location is computed by adding a constant
value to the contents of a register, forming the address.
Design Principle
CPU
Register 1 Register 2 Register 3
…
Memory
…
3 100
2 10
1 101
0 1
Byte
Address
Data
Page 69
Example 3
Assume variable h is associated with register $s2 and the base address of the array A is in $s3.
What is the MIPS assembly code for the C assignment statement below?
A[12] = h + A[8];
Answer
Arithmetic_Ops
Improved Efficiency
Constants can be directly used in arithmetic instructions via the addi (add immediate) instruction.
Arithmetic_Ops
Arithmetic_Ops
MIPS operands
Name Example Comments
$s0–$s7, $t0–$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
32 registers $a0–$a3, $v0–$v1, $gp, arithmetic, register $zero always equals 0, and register $at is
$fp, $sp, $ra, $at reserved by the assembler to handle large constants.
Accessed only by data transfer instructions. MIPS uses byte
Memory[0], Memory[4], …,
2 30
memory words addresses, so sequential word addresses differ by 4. Memory holds
Memory[4294967292]
data structures, arrays, and spilled registers.
Category Instruction Example Meaning Comments
add add $s1,$s2,$s3 $s1 = $s2 + $s3 Three register operands
Arithmetic subtract sub $s1,$s2,$s3 $s1 = $s2 – $s3 Three register operands
load word lw $s1,20($s2) $s1 = Memory[$s2 + 20] Word from memory to register
store word sw $s1,20($s2) Memory[$s2 + 20] = $s1 Word from register to memory
load half unsigned lhu $s1,20($s2) $s1 = Memory[$s2+ 20] Halfword memory to register
Data transfer load byte lb $s1,20($s2) $s1 = Memory[$s2+ 20] Byte from memory to register
load byte unsigned lbu $s1,20($s2) $s1 = Memory[$s2+ 20] Byte from memory to register
store byte sb $s1,20($s2) Memory[$s2+ 20] = $s1 Byte from register to memory
load linked word ll $s1,20($s2) $s1 = Memory[$s2+ 20] Load word as 1st half of atomic swap
store condition. word sc $s1,20($s2) Memory[$s2+20]=$s1;$s1=0 or 1 Store word as 2nd half of atomic swap
and and $s1,$s2,$s3 $s1 = $s2 & $s3 Three reg. operands; bit-by-bit AND
nor nor $s1,$s2,$s3 $s1 = ~ ($s2 | $s3) Three reg. operands; bit-by-bit NOR
Logical and immediate andi $s1,$s2,20 $s1 = $s2 & 20 Bit-by-bit AND reg with constant
shift left logical sll $s1,$s2,10 $s1 = $s2 << 10 Shift left by constant
shift right logical srl $s1,$s2,10 $s1 = $s2 >> 10 Shift right by constant
branch on equal beq $s1,$s2,25 if ($s1 == $s2) go to PC + 4 + 100 Equal test; PC-relative branch
branch on not equal bne $s1,$s2,25 if ($s1!= $s2) go to PC + 4 + 100 Not equal test; PC-relative
set on less than slt $s1,$s2,$s3 if ($s2 < $s3) $s1= 1; else $s1 = 0 Compare less than; for beq, bne
Conditional branch
set on less than unsigned sltu $s1,$s2,$s3 if ($s2 < $s3) $s1= 1; else $s1 = 0 Compare less than unsigned
set less than immediate slti $s1,$s2,20 if ($s2 < 20) $s1= 1; else $s1= 0 Compare less than constant
set less than immediate unsigned sltiu $s1,$s2,20 if ($s2 < 20) $s1= 1; else $s1= 0 Compare less than constant unsigned
Unconditional jump jump register jr $ra go to $ra For switch, procedure return
jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call
Unsigned Numbers
x = xn−1 2n−1 + xn−2 2n−2 + ⋯ + x1 21 + x0 20
Range: 0 to +2n – 1. Using 32 bits 0 to +4,294,967,295
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 0000 0010two = 2ten
Examples
The 16-bit representation of 2ten is 0000 0000 0000 0010two .
To convert this to 32 bits, the 0 is replicated 16 times, and the rest of the original bits are placed in the right half:
0000 0000 0000 0000 0000 0000 0000 0010two = 2ten