Mips

5,599 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,599
On SlideShare
0
From Embeds
0
Number of Embeds
58
Actions
Shares
0
Downloads
127
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mips

  1. 1. MIPS Architecture <ul><li>Memory organisation </li></ul><ul><ul><li>the purpose of memory is to store groups of bits and deliver them to the processor (for loading into regs) </li></ul></ul><ul><ul><li>memory can hold both data and program </li></ul></ul><ul><ul><li>SPIM simulator: </li></ul></ul><ul><ul><ul><li>0x0040 0000 - text segment/program instructions </li></ul></ul></ul><ul><ul><ul><li>0x1000 0000 - data segment </li></ul></ul></ul><ul><ul><ul><li>0x7FFF FFFF and decreasing addresses - stack segment </li></ul></ul></ul><ul><ul><li>a word is the number of bits that can be transferred at one time on the data bus and stored in a reg </li></ul></ul><ul><ul><li>Mips: 32-bits/4 bytes; others use different sizes </li></ul></ul>
  2. 2. MIPS Architecture <ul><li>storage order of words </li></ul><ul><ul><li>little-endian - store the little end of the word first </li></ul></ul><ul><li>processor organisation </li></ul><ul><ul><li>registers </li></ul></ul><ul><ul><li>where it stores information </li></ul></ul><ul><ul><li>what it does in executing instructions </li></ul></ul><ul><ul><li>MIPS: </li></ul></ul><ul><ul><ul><li>all registers are 32-bit </li></ul></ul></ul><ul><ul><ul><li>PC (program counter) holds address of next instruction </li></ul></ul></ul><ul><ul><ul><li>32 general purpose registers (can use name or number) </li></ul></ul></ul><ul><ul><ul><li>instruction set: load & store, operations, jump & branch, others </li></ul></ul></ul>
  3. 3. MIPS Architecture <ul><li>Registers </li></ul><ul><li>First reg ($0) is a constant 0 and cannot be changed </li></ul><ul><li>Last reg ($31/$ra) stores the return address from a proc. </li></ul><ul><li>$1($at) - Assembler temporary. We will avoid using it. </li></ul><ul><li>$v0 - $v1 - values returned by a function </li></ul><ul><li>$a0 - $a3 - arguments to a function </li></ul><ul><li>$t0 - $t9 - temporary use. Use freely but if you call a procedure that procedure may change it. </li></ul><ul><li>$s0 - $s7 - saved by procedures. When you are writing a proc you must save and restore previous values. </li></ul>
  4. 4. MIPS Architecture <ul><li>Instruction set </li></ul><ul><li>load and store </li></ul><ul><ul><li>load data from memory into a reg, or store reg contents in memory </li></ul></ul><ul><ul><li>lw $t0, num1 #load word in num1 into $t0 </li></ul></ul><ul><ul><li>sw $t0, num2 # store word in $t0 into num2 </li></ul></ul><ul><ul><li>li $v0, 4 # load immediate value (constant) 4 into $v0 </li></ul></ul><ul><li>operations: arithmetic and logic </li></ul><ul><ul><li>perform the operation on data in 2 regs and store result in 3rd reg </li></ul></ul><ul><ul><li>add $t0, $t3, $t4 # $t0 = $t3 + $t4 </li></ul></ul><ul><li>jump and branch </li></ul><ul><ul><li>alter the PC to control flow of program (if and loop statements) </li></ul></ul><ul><li>specialised instructions (e.g. floating point instructions) </li></ul>
  5. 5. Basic Assembly Language Instructions <ul><li>Levels </li></ul><ul><li>binary machine instructions (in hex) </li></ul><ul><li>symbolic actual machine instructions (regs shown: $31) </li></ul><ul><li>assembly language instructions </li></ul><ul><li>RISC (not always 1:1 correspondence) </li></ul><ul><li>Load and Store instructions </li></ul><ul><li>move data between memory and the processor </li></ul><ul><li>always 2 operands, a register and an address </li></ul><ul><li>register is always first, direction of movement is opposite </li></ul><ul><ul><li>lw $t0, Num1 # load word, $t0=Num1 </li></ul></ul><ul><ul><li>sw $t0, Num2 # store word, Num2=$t0 </li></ul></ul><ul><li>load immediate: li $vo, 4 </li></ul><ul><li>load address: la $a0, prompt_message (note diff with lw) </li></ul>
  6. 6. Basic Assembly Language Instructions <ul><li>Arithmetic </li></ul><ul><li>done in registers, with 3 operands </li></ul><ul><li>add Rdest, Rsrc1, src2 </li></ul><ul><li>(x + 5 - y) * 35/3 lw $t0, x # load x from memory </li></ul><ul><ul><li>lw $t1, y # load y from memory </li></ul></ul><ul><ul><li>add $t0, $t0, 5 # x + 5 </li></ul></ul><ul><ul><li>sub $t0, $t0, $t1 # x + 5 - y </li></ul></ul><ul><ul><li>mul $t0, $t0, 35 # (x + 5 - y) * 35 </li></ul></ul><ul><ul><li>div $t0, $t0, 3 # (x + 5 - y) * 35/3 </li></ul></ul><ul><li>arithmetic overflow, signed numbers </li></ul><ul><ul><li>add, sub, mulo, div check for overflow (not mul) </li></ul></ul><ul><ul><li>“ arithmetic overflow” and program usually stops </li></ul></ul><ul><li>unsigned numbers: addu, subu, mulou, divu </li></ul>
  7. 7. Basic Assembly Language Instructions <ul><li>Input and Output - System calls </li></ul><ul><li>difficult and specialised job </li></ul><ul><li>SPIM: 10 basic services </li></ul><ul><li>call code always goes in $v0 and the system is called with syscall </li></ul><ul><li>Service Code Arguments Results </li></ul><ul><li>print int 1 $a0 = integer signed decimal integer printed in window </li></ul><ul><li>print str 4 $a0 = str address string printed in window </li></ul><ul><li>read int 5 (none) $v0 holds integer that was entered </li></ul><ul><li>read str 8 $a0=store address characters are stored </li></ul><ul><li>$a1= length limit </li></ul><ul><li>exit 10 (none) Ends the program </li></ul>
  8. 8. Basic Assembly Language Instructions <ul><li>Control structures </li></ul><ul><li>if, while, for </li></ul><ul><li>PC determines order of program execution </li></ul><ul><li>unconditional jump </li></ul><ul><ul><li>puts the address of a label in the PC </li></ul></ul><ul><ul><li>execution continues from that point </li></ul></ul><ul><ul><li>jar also saves the old PC value as a “return address” in register $ra ($31) </li></ul></ul><ul><li>Inst Example Meaning </li></ul><ul><li>j label j do_it_again next instruction is at the label do_it_again: </li></ul><ul><li>jal label jal my_proc execution of procedure my_proc will start.$ra holds address of instruction following the jal </li></ul><ul><li>jr Rsrc jr $ra Return from procedure call by putting $ra value back into the PC </li></ul>
  9. 9. Basic Assembly Language Instructions <ul><li>Control structures </li></ul><ul><li>conditional jump </li></ul><ul><ul><li>the condition is always a comparison of 2 registers, or a reg and a constant </li></ul></ul><ul><ul><li>normally use signed numbers (if unsigned, add a ‘u’ to the command) </li></ul></ul><ul><ul><li>can compare with z using $0 as src2 (or add ‘z’ to command) </li></ul></ul><ul><li>Instruction Branch to label if Unsigned </li></ul><ul><li>beq Rsrc1,Src2,label Rsrc1 = Src2 </li></ul><ul><li>bne Rsrc1,Src2,label Rsrc1 <> Src2 </li></ul><ul><li>blt Rsrc1,Src2,label Rsrc1 < Src2 bltu </li></ul><ul><li>bgt Rsrc1,Src2,label Rsrc1 > Src2 bgtu </li></ul><ul><li>ble Rsrc1,Src2,label Rsrc1 <= Src2 bleu </li></ul><ul><li>bge Rsrc1,Src2,label Rsrc1 >= Src2 bgeu </li></ul>
  10. 10. Basic Assembly Language Instructions <ul><li>Control structures </li></ul><ul><li>IF constructs: choosing alternatives (if, if-then-else) </li></ul>IF num < 0 then num = - num ENDIF IF temperature >= 25 then activity = “go swimming” else activity = “ride bicycle” endif print (activity) lw $t0, num bgez $t0, endif0 # must be ltz ----- sub $t0, $0, $t0 # 0 - num sw $t0, num endif0: # num is now non-negative lw $t0, temperature blt $t0, 25, else25 la $a0, swim j endif25 else25: la $a0, cycle endif25: li $v0, 4 # print string call code syscall Note: “branch around” and jump ins needed before else
  11. 11. Basic Assembly Language Instructions <ul><li>Control structures </li></ul><ul><li>loops: while and for (both pre-test loops), repeat (post-test loop) </li></ul>
  12. 12. Basic Assembly Language Instructions <ul><li>Control structures </li></ul>sum = 0 num = 0 WHILE num >= 0 DO sum = sum + num read (num) ENDWHILE (write 10 lines) FOR countdown = 10 downto 1 DO print “Hello again …” ENDFOR mov $t0, $0 # sum = 0 mov $v0, $0 # num = 0 while5: bltz $v0, endwhile5 # $v0 < 0? add $t0, $v0 #$v0 switches role here li $v0, 5 # read int call syscall j while5 endwhile5: # set up print arguments before loop la $a0, hello_again li $v0, 4 # print string call li $t0, 10 # countdown for10: beqz $t0, endfor10 syscall # print another line sub $t0, 1 # decr countdown j for10 # continue for loop endfor10
  13. 13. Basic Assembly Language Instructions <ul><li>Instruction formats and addressing modes </li></ul><ul><li>Instruction formats </li></ul><ul><ul><li>register </li></ul></ul><ul><ul><li>immediate </li></ul></ul><ul><ul><li>jump </li></ul></ul><ul><li>Addressing modes </li></ul><ul><ul><li>immediate - value built in to the instruction </li></ul></ul><ul><ul><li>register - register used for data </li></ul></ul><ul><li>Memory referencing - used with load and store instructions </li></ul><ul><ul><li>label - fixed address built in to the instruction </li></ul></ul><ul><ul><li>indirect - register contains the address </li></ul></ul><ul><ul><li>Base addressing - field of a record </li></ul></ul><ul><ul><li>Indexed addressing - element of an array </li></ul></ul>
  14. 14. Basic Assembly Language Instructions <ul><li>Instruction formats </li></ul><ul><li>all instructions fit in 32-bit words </li></ul><ul><li>decoding starts with the leftmost 6-bits, the op code </li></ul><ul><li>Register </li></ul><ul><li>For operations using only registers as operands, the operation code is 000000 </li></ul><ul><li>6 bits 5 bits 5 bits 5 bits 5 bits 6 bits </li></ul><ul><li>000000 src reg target reg Dest shift amt function </li></ul><ul><li>000000 01010 00111 00101 00000 100010 </li></ul><ul><li>the last line of the table gives as an example a subtract instruction, sub $5, $10, $7 </li></ul><ul><li>3 5-bit fields identify the 3 regs: src, target/src, dest (diff from assembly) </li></ul>
  15. 15. Basic Assembly Language Instructions <ul><li>Instruction formats (Register) </li></ul><ul><li>flow between registers and ALU </li></ul><ul><li>add $t3, $t1, $t2 </li></ul><ul><ul><li>$t1 = $9, $t2 = $10, $t3 = $11 </li></ul></ul>
  16. 16. Basic Assembly Language Instructions <ul><li>Immediate </li></ul><ul><li>16-bit immediate (constant), 6-bit op, 2 regs </li></ul><ul><li>addi $t1, $t2, 0xfffe (actually adds 0xfffffffe, -2) </li></ul><ul><li>shorter numbers are sign extended </li></ul><ul><li>6 bits 5 bits 5 bits 16 bits </li></ul><ul><li>op code src reg target reg immediate value </li></ul><ul><li>001000 01010 01001 1111 1111 1111 1110 </li></ul>
  17. 17. Basic Assembly Language Instructions <ul><li>Jump </li></ul><ul><li>use all 26-bits following the op code for jump address </li></ul><ul><li>last 2 bits are always 0 (word alignment) </li></ul><ul><li>upon execution, the 26-bit jump target is shifted left 2 bits and stored in the PC, causing the jump </li></ul><ul><li>6 bits 26 bits - jump target (words) </li></ul><ul><li>j 0x0040000c </li></ul><ul><li>000010 0x0100003 </li></ul><ul><li>can only target the first 256Mb of memory </li></ul><ul><li>workarounds: reserve “low” memory, use jump-reg instructions </li></ul>
  18. 18. Basic Assembly Language Instructions <ul><li>Addressing Modes - Immediate </li></ul><ul><li>usually use lower 16-bits, uses 2 instructions if > 16-bits </li></ul><ul><li>PC-relative addressing - offset or displacement used </li></ul><ul><li>Examples of instructions with immediate data: </li></ul><ul><li>addi $t0,t1,65 sub $t0,7 #assembled as: addi $t0, $t0, -7 li $t3, 0x12345678 #assembled as </li></ul><ul><li>lui $at, 0x1234 ori $t3, $at, 0x5678 #puts it all together bgez $t5, 16 #skip 4 instructions ahead if # $t5 is non-negative </li></ul><ul><li># (4 is encoded in #immediate field) </li></ul>
  19. 19. Basic Assembly Language Instructions <ul><li>Addressing Modes - Immediate </li></ul><ul><li>faster than memory </li></ul><ul><li>some instructions reference registers exclusively </li></ul><ul><li>32 regs, only 5 bits to specify a register </li></ul><ul><li>Examples: </li></ul><ul><li>addu $t3, $t1, $t2 # add (unsigned) $t3 = $t1 + $t2 </li></ul><ul><li>sub $t0, $t3 # subtract (signed) $t0 = $t0 - $t3 </li></ul>
  20. 20. Basic Assembly Language Instructions <ul><li>Memory Addressing </li></ul><ul><li>can address up to 4GB (slower access) </li></ul><ul><li>label - fixed address built into the instruction </li></ul><ul><ul><li>also known as direct addressing </li></ul></ul><ul><ul><li>lw $t0, MyNumber # load the word into $t0 </li></ul></ul><ul><ul><li>sb $t9, firstInitial # store rightmost 8 bits of $t9 in data seg </li></ul></ul><ul><li>indirect - register contains the address </li></ul><ul><ul><li>a register can be used as a pointer </li></ul></ul><ul><ul><li>e.g. stepping through a string one step at a time </li></ul></ul><ul><ul><li>catStr: .asciiz “cat” </li></ul></ul><ul><ul><li>la $t0, catStr # load address i.e. put address of catStr into $t0 </li></ul></ul><ul><ul><li>lb $t1, ($t0) # ‘c’ is now in $t1 </li></ul></ul><ul><ul><li>addi $t0, 1 # point to next character </li></ul></ul><ul><ul><li>lb $t2, ($t0) # ‘a’ is now in $t2 </li></ul></ul>
  21. 21. Basic Assembly Language Instructions <ul><li>Memory Addressing - indirect addressing (cont.) </li></ul><ul><li>base addressing </li></ul><ul><ul><li>useful for referring to fields or a record or input </li></ul></ul><ul><ul><li>e.g. telephone numbers: area code/prefix/number/extension </li></ul></ul><ul><ul><li>4 short ints (16-bit halfwords) </li></ul></ul><ul><ul><li>the extension starts 6 bytes from the beginning of the record </li></ul></ul><ul><ul><li>add offset 6 to a register pointing to the record </li></ul></ul><ul><ul><li>la $t0, MyPhone # $t0 points to a telephone record </li></ul></ul><ul><ul><li>lh $t1, 6($t0) # $t1 loaded with the extension no. in record </li></ul></ul>
  22. 22. Basic Assembly Language Instructions <ul><li>Memory Addressing - indirect addressing (cont.) </li></ul><ul><li>indexed addressing </li></ul><ul><ul><li>useful for indexing arrays </li></ul></ul><ul><ul><li>e.g. copying a 10 byte string to another string </li></ul></ul><ul><ul><li>li $t1, 9 # $t0 indexes arrays </li></ul></ul><ul><ul><li>copyloop: </li></ul></ul><ul><ul><li>lb $t1, str1($t0) # $t1 used to transfer character </li></ul></ul><ul><ul><li>sb $t1, str2($t0) # to str2 </li></ul></ul><ul><ul><li>sub $t10, 1 # decrement array index </li></ul></ul><ul><ul><li>bgez $t0, copyloop # repeat until $t0 < 0 </li></ul></ul><ul><li>if moving words, need to decrement by 4 instead of 1 </li></ul><ul><li>assembler actually uses 3 machine ins for each indexed ins </li></ul>
  23. 23. Basic Assembly Language Instructions <ul><li>Memory Addressing -the bottom line </li></ul><ul><li>sb $t1, str2($t0) (address was 0x1001000C; 0x1001 = 4097) </li></ul><ul><li>Underlying instruction: sb $9, 12($1) </li></ul><ul><li>op code sb rs ($1) rt - $9 immediate (offset) 0x000c </li></ul><ul><li>101000 00001 01001 0000 0000 0000 1100 </li></ul>

×