A Minimalistic Introduction To MIPS Instruction
A Minimalistic Introduction To MIPS Instruction
http://www.cs.pitt.edu/~xujie/cs447/MIPS_Instruction.htm
op
rs
rt
rd
op Operation code
Specific Instruction Formats Format 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits R op rs rt rd shamt funct Comments Arithmetic
1 of 5
9/28/2011 5:49 PM
http://www.cs.pitt.edu/~xujie/cs447/MIPS_Instruction.htm
I J
op op
rs
rt
Category Instruction Example Meaning Arithmetic Arithmetic add subtract add a,b,c sub a,b,c a=b+c a=b-c
Smaller is faster. MIPS has 32 32-bit registers,$v0,...$v31, a very large number would increase the clock cycle time.
Good design demands compromise. The compromise represented by the MIPS design, was to make all the instructions the same length, thereby requiring different instruction formats.
Make the common case fast. The MIPS instruction set addresses this principal by making constants part of arithmetic instructions. Furthermore, by loading small constants into the upper 16-bits of a register.
9/28/2011 5:49 PM
http://www.cs.pitt.edu/~xujie/cs447/MIPS_Instruction.htm
Arithmetic Instructions
Example add $1,$2,$3 sub $1,$2,$3 addi $1,$2,10 addu $1,$2,$3 subu $1,$2,$3
Meaning
Comments
Logical Instruction and or and immediate or immediate shift left logical Example and $1,$2,$3 or $1,$2,$3 Meaning $1=$2&$3 $1=$2|$3 Comments 3 register operands 3 register operands AND constant OR constant
Data Transfer Instruction load word store word Example Meaning Comments
lw $1,10($2) $1=Memory[$2+10] memory to register sw $1,10($2) Memory[$2+10]=$1 register to memory $1=10x2^16 load constant into upper 16 bits
Conditional Branch
3 of 5
9/28/2011 5:49 PM
http://www.cs.pitt.edu/~xujie/cs447/MIPS_Instruction.htm
Example
Meaning
Comments
beq $1,$2,10 if($1==$2)go to PC+4+10 Equal test Not equal test Less than compare
branch on not equal bne $1,$2,10 if($1!=$2)go to PC+4+10 set on less then slt $1,$2,$3 if($2<$3)$1=1;else $1=0
Unconditional Jump Instruction jump jump register Example j 1000 jr $31 Meaning go to 1000 go to $31 Comments Jump to target address For switch, procedure return
Assembler Syntax Program includes .data and .text Comments begin with #. Rest of line is ignored. Identifier names are sequence of letters, numbers, underbars (_) and dots (.). Labels are declared by putting them at beginning of line followed by colon. Use labels for variables and code locations. Instruction format: op field followed by one or more operands: addi $t0, $t0, 1 Operands may be literal values or registers. Register is hardware primitive, can stored 32-bit value: $s0 Numbers are base 10 by default. 0x prefix indicates hexadecimal. Strings are enclosed in quotes. May include \n=newline or \t=tab. Used for prompts.
4 of 5
9/28/2011 5:49 PM
http://www.cs.pitt.edu/~xujie/cs447/MIPS_Instruction.htm
To read an integer from the keyboard Set $v0 to 5 syscall The integer entered will be in $v0 To exit Set $v0 to 10 syscall
5 of 5
9/28/2011 5:49 PM