Statement Format MIPS assembly language statements have the
Statement Format • MIPS assembly language statements have the following format label: opcode operand, operand # comment • Label identifies the statement and is optional • Opcode is the command • There may be 0 -3 operands depending on the instruction • Comments are optional but helpful • Comments can be on a line by themselves
Register Conventions • MIPS assembler does not IMPOSE these conventions, however it does support them. • This way functions can be easily exchanged and teams can work on a single product • More later
Command Formats • To make sure you use the proper command, look at the operands and their data type • Some commands take 3 registers as operands • Some take an actual number (immediate) as one of the operands • Some take a label (of a variable or statement) as one of the operands • Some take a memory location specified by a base/offset pair (Macro assembler allows labels for the memory location)
Arithmetic • Let’s translate: x=y+7 -z; • Assembler. text lw addi lw sub sw li syscall. data x: . word y: . word z: . word $t 0, y $t 0, 7 $t 1, z $t 0, $t 1 $t 0, x $v 0, 10 0
Multiply • Let’s look at Britton’s appendix C about the Multiply instruction • It multiplies the contents of two registers (remember the result can be BIG) and stores the lower 32 bits of the result in the LOW register and the upper 32 bits in the HIGH register. • How do we work with HIGH and LOW? – Move from High mfhi reg – Move from Low mflo reg
Divide • When we divide, there is a quotient and a remainder (we are in the integer world) • Again, looking at appendix C, Divide (div) takes 2 registers, divides the first by the second, and places the quotient in LOW and the remainder in HIGH • Divide unsigned (divu) treats the values as unsigned (positive) values. There is a multiply unsigned (multu) also.
Another Expression • Let’s translate the following: x=y*(x+312)/z k=x mod 5 • In assembler lw $t 0, x addi $t 0, 312 lw $t 1, y mult $t 1, $t 0 mflo $t 0 #should check for overflow in HIGH lw div mflo sw li div mfhi sw $t 1, z $t 0, $t 1 $t 0, x $t 1, 5 $t 0, $t 1 $t 0, k
Basic Branching • The branch instructions have the format branchop reg, label • The branchops can be: – bgez - branch if greater or equal to 0 – bgtz - branch if greater than 0 – blez – branch if less or equal to 0 – bltz – branch if less than 0
Equality • Branch on equality uses op reg, label • Ops are – beq – bne
Macro Branches • The assembler will generate an equivalent set (1 -2) of basic assembler instructions for each of these – – – – beqz bge bgt ble blt bnez b reg, label reg, reg, label
Control Structures - If if (a<b) { x=a+1; y=b-a; } a=a+2; lw lw bge addi sw sub sw pos 1: addi $t 0, a $t 1, b $t 0, $t 1, pos 1 $t 2, $t 0, 1 $t 2, x $t 2, $t 1, $t 0 $t 2, y $t 0, 2
If – else if (x<0) { y=y+1; x=-x; } else { z=z+1; y=-x; } x=x/2; el: last: lw bgez lw addi sw sub sw b lw addi sw sub sw sra sw $t 0, x $t 0, el $t 1, y $t 2, $t 1, 1 $t 2, y $t 0, $t 0, x last $t 3, z $t 3, 1 $t 3, z $t 3, $0, $t 0 $t 3, y $t 0, 1 $t 0, x
While while (a<b) { a=a+1; b=b-1; } lw lw rep: bge addi b out: sw sw $t 0, a $t 1, b $t 0, $t 1, out $t 0, 1 $t 1, -1 rep $t 0, a $t 1, b
For for (i=0; i<100; i++) { sum=sum+i; even=even+2; } add $t 0, $0 #li $t 0, 0 li $t 1, 100 lw $t 2, sum lw $t 3, even loop: add $t 2, $t 0 addi $t 3, 2 addi $t 0, 1 blt $t 0, $t 1, loop sw……i, sum, even
Macro Instructions • Instructions in assembler that generate more than one machine language instruction or a very different machine instruction than what is stated. – Allows for smaller and faster instruction set – Look at Britton, appendix D – Some are just one statement.
- Slides: 15