# Statement Format MIPS assembly language statements have the following format label: opcode operand,operand,operand # comment Label identifies the statement.

## Presentation on theme: "Statement Format MIPS assembly language statements have the following format label: opcode operand,operand,operand # comment Label identifies the statement."— Presentation transcript:

Statement Format MIPS assembly language statements have the following format label: opcode operand,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\$t0,y addi \$t0,\$t0,7 lw\$t1,z sub\$t0,\$t0,\$t1 sw \$t0,x li\$v0,10 syscall.data x:.word0 y:.word0 z:.word0

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\$t0,x addi\$t0,312 lw\$t1,y mult\$t1,\$t0 mflo\$t0 #should check for overflow in HIGH lw\$t1,z div\$t0,\$t1 mflo\$t0 sw\$t0,x li\$t1,5 div\$t0,\$t1 mfhi\$t0 sw\$t0,k

Basic Branching The branch instructions have the format branchopreg, 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 opreg, 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 –beqzreg, label –bgereg,reg,label –bgtreg,reg,label –blereg,reg,label –bltreg,reg,label –bnezreg,label –blabel

Control Structures - If if (a { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3392462/slides/slide_11.jpg", "name": "Control Structures - If if (a

If – else if (x<0) { y=y+1; x=-x; } else { z=z+1; y=-x; } x=x/2; lw\$t0,x bgez\$t0,el lw\$t1,y addi\$t2,\$t1,1 sw\$t2,y sub\$t0,\$0,\$t0 sw\$t0,x blast el:lw\$t3,z addi\$t3,\$t3,1 sw\$t3,z sub\$t3,\$0,\$t0 sw\$t3,y last:sra\$t0,\$t0,1 sw\$t0,x

While while (a { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3392462/slides/slide_13.jpg", "name": "While while (a