MIPS ISA and MIPS Assembly CS301 Prof. Szajda
Administrative • HW #2 due Wednesday (9/11) at 5pm • Lab #2 due Friday (9/13) 1:30pm • Read Appendix B5, B6, B.9 and Chapter 2.5-2.9 (if you have not already done so!)
MIPS ISA
MIPS ISA • Small number of simple instructions (RISC) w Instructions are fixed size of 32b w Very rigid structure • Load/Store Architecture w Few addressing modes
Register File • Register file w 32 integer w 32 single precision floating point • Integer What’s going on here?! w Volatile - scratch registers § $t0 - $t9 ($8-$15, $24-$25) w Non-volatile - called function does save/ restore § $s0 - $s7 ($16-$23)
Special Integer Registers • Zero w $0 • Return values w $v0, $v1 ($2, $3) • Function arguments w $a0-$a3 ($4-$7) w If more than 4 registers required, then place parameters “above” frame pointer (at higher address) • Stack pointer - $sp ($29) • Frame pointer - $fp ($30) w Usually not used in our examples • Return address - $ra ($31)
Floating Point Registers • 32 single-precision registers w $f0, $f1, ..., $f31 • Double precision uses 2 single precision fp registers w reference even numbered registers • Special registers w Return values § $f0-$f3 w Function arguments § $f12 - $f15
Data Movement Instructions • move rd, rs w Move register rs to rd • mov.d fd, fs w Move double precision fp register fs to fd • mtc1 fd, rs move to coprocessor 1 w Move rs register to fd • mfc1 rd, fs move from coprocessor 1 w Move fp register fs to rd
Load/Store Instructions • la rt, address w load computed address (not contents of location) into register rt • lw rt, address w load word in memory location address into register rt • sw rt, address w store value in register rt into memory at location address • li rt, imm w load integer constant imm into register rt • l.d rt, address w load double precision fp value from memory location address into fp register rt and rt+1
Arithmetic Instructions • add rd, rs, rt w rd = rs + rt • addi rd, rs, imm w rd = rs + imm • sub rd, rs, rt w rd = rs - rt multiply (without overflow) • mul rd, rs, rt w rd = rs × rt puts low order 32 bits of product in rd • div rd, rs, rt divide (without overflow) w rd = rs / rt • Floating point versions exist usually with a .d tacked on ( add.d )
Shift Operators • sll rd, rt, sa shift left logical w rd = rt << sa w Zero fill • sra rd, rt, sa shift right arithmetic w rd = rt >> sa w Sign fill • srl rd, rt, sa shift right logical w rd = rt >> sa w Zero fill
Comparison Instructions • slt rd, rs, rt w Set register rd to 1 if rs < rt, otherwise set rd to 0 • slti rd, rs, imm w Set register rd to 1 if rs < imm, otherwise set rd to 0 • Similar instructions for greater than • seq rd, rs, rt w Set rd to 1 if rs == rt, otherwise set rd to 0
Branch Instructions • 16-b instruction o fg set field (2 15 -1 forward, 2 15 back) • b label w Unconditionally branch to instruction at label • beq rs, rt, label w Conditionally branch to label if rs == rt • bne, bgt, bge, ...
Jump Instructions • 26-bit address field • j target w Unconditionally jump to instruction at target • jal target jump and link w Unconditionally jump to instruction at target w Save address of next instruction in register $ra w bal is like jal however jal must be used if target is from another file
Assembly Programs
Note: You will only be allowed to use the instructions listed in Appendix B.10 in your programs! 16
# assign.asm # simple program to modify a global variable .data # add what follows to the data section x: .word 5 # create global integer variable x. Set to 5. .text # add what follows to the text .align 2 # Align on word boundaries .globl main # "exports" the symbol main so it is # accessible to other modules main: # we don't need a frame la $t0, x # $t0 = &x lw $t1, 0($t0) # $t1 = x addi $t1,$t1,2 # $t1 = $t1 + 2 sw $t1, 0($t0) # x = $t1 jr $ra # return - main is a function, too
Assembly File • Segments w .data § Integer (.word), character (.byte), arrays of these, the “z” is required if you want § String (.asciiz) your string to be null terminated! w .text § Instructions § main should be first instruction and needs to be specified as .globl an “assembler directive”
MIPS Labels • MIPS assembly code contains instructions • Data and instructions can be prefaced with a label followed by a colon w main: • Instructions can have operands that are: w registers w constants/immediates w addresses § Explicit numbers § Register + o fg set § Labels • Assembler will replace labels with corresponding addresses when creating machine language
Examples Suppose $s0 = a, $s1 = b, $s2 = c, $s3 = d • Write MIPS instructions for the following code: b = (c - 1) × (a + d)
Examples Suppose $s0 = a, $s1 = b, $s2 = c, $s3 = d • Write MIPS instructions for the following code: if(a > b) c = c+1; else d = d+1;
Examples Suppose A is an array of 10 integers w How would I declare A as a global array? Suppose $s0 = A, $s1 = g, $s2 = h, $s3 = i • Write MIPS instructions for the following code: g = h +A[i];
Using Strings • ASCII w 8-bit used to represent characters • To access individual characters, use w lb $t0, 0($sp) # read character w sb $t0, 0($sp) # write character • Strings w Array of characters w Terminated with byte whose value is 0 (null)
Examples Suppose A is a string w How would I declare A as a global variable? Suppose $s0 = A, $s1 = g, $s2 = h, $s3 = i • Write MIPS instructions for the following code: g = A[i];
Recommend
More recommend