MIPS coding Complete MIPS code data save word

  • Slides: 20
Download presentation
MIPS coding

MIPS coding

Complete MIPS code. data save: . word 10, 10, 10, 11, 12, . text.

Complete MIPS code. data save: . word 10, 10, 10, 11, 12, . text. globl main: li $s 3, 0 li $s 5, 10 la $s 6, save Loop: sll $t 1, $s 3, 2 add $t 1, $s 6 lw $t 0, 0($t 1) bne $t 0, $s 5, Exit addi $s 3, 1 j Loop Exit: done: li $v 0, 10 # these two lines are to tell the simulator to stop syscall

Complete MIPS code • It has a data segment and a code (text) segment.

Complete MIPS code • It has a data segment and a code (text) segment. • The beginning of the data segment in the assembly source code is indicated as. data and followed by several declarations such as – A: . word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 meaning an array of words whose starting address is associated with label ``A. ’’

Complete MIPS code • The text segment in the source code usually starts with.

Complete MIPS code • The text segment in the source code usually starts with. text. globl main: where ``main’’ is the label associated with the address of the first instruction of the code. • And the code usually ends with li $v 0, 10 # telling the simulator to stop syscall • Comment with `#’

Exercise 1 • Suppose we have three arrays, A, B, C, all of size

Exercise 1 • Suppose we have three arrays, A, B, C, all of size 10. Now we want to set C[i] = min(A[i], B[i]) for all 0<= i <= 9. 9/5/2021 week 04 -3. ppt 5

Exercise 1 • Suppose we have three arrays, A, B, C, all of size

Exercise 1 • Suppose we have three arrays, A, B, C, all of size 10. Now we want to set C[i] = min(A[i], B[i]) for all 0<= i <= 9. • First, we need a loop to walk through the elements (done before) • Second, we need to be able to read the elements (done before) • Third, we need to be able to compare two numbers (done before) • Fourth, we need to write back to the memory (easy) 9/5/2021 week 04 -3. ppt 6

. data A: B: C: . word 12, 34, 67, 1, 45, 90, 11,

. data A: B: C: . word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19. word 90, 2, 93, 66, 8, 120, 121, 11, 33, 9. word 0, 0, 0, 0 . text. globl main: la $s 0, A la $s 1, B la $s 2, C li $s 3, 10 li $t 0, 0 LOOP: sll $t 4, $t 0, 2 add $t 5, $t 4, $s 0 lw $t 1, 0($t 5) add $t 6, $t 4, $s 1 lw $t 2, 0($t 6) slt $t 5, $t 1, $t 2 beq $t 5, $0, L 1 ori $t 8, $t 1, 0 j L 2 L 1: ori $t 8, $t 2, 0 L 2: add $t 6, $t 4, $s 2 sw $t 8, 0($t 6) addi $t 0, 1 bne $t 0, $s 3, LOOP done: li $v 0, 10 syscall # using $t 0 as i # # # # # $t 4 = i * 4 $t 5 will have the address of A[i] $t 1 has A[i] $t 6 will have the address of B[i] $t 2 has B[i] set $t 5 to be 1 if A[i] < B[i] if $t 5 == 0, goto L 1. in this case, A[i] >= B[i] setting $t 8 to be A[i] always remember to jump in an if else! # setting $t 8 to be B[i] # # now $t 6 has the address of C[i] now C[i] has the minimum of A[i] and B[i] i ++ go back if not yet 10 times

SPIM • Run codes with SPIM is a simulator. – Use any editor to

SPIM • Run codes with SPIM is a simulator. – Use any editor to write the source file, save it as an. asm file. – Run PCSpim, load the source file. – F 10 to step through the code. Monitor how the registers change. – F 5 to run the code – Can set breakpoints for debugging • SPIM can be downloaded at http: //pages. cs. wisc. edu/~larus/spim. html • Lots of good references online, like https: //www. cs. tcd. ie/~waldroj/itral/spim_ref. html

Representing Instructions in Computers • Note that computers only have 0’s and 1’s •

Representing Instructions in Computers • Note that computers only have 0’s and 1’s • Before we can load MIPS instructions into memory, they need to be translated into machine instructions, which consist of only 0’s and 1’s – In other words, we need to encode or represent instructions – The symbolic representation of machine instructions is called assembly language – The binary representation of instructions is called machine language • A sequence of instructions in binary form is called machine code 9/5/2021 CDA 3100 9

Example 9/5/2021 CDA 3100 10

Example 9/5/2021 CDA 3100 10

MIPS Instruction Encoding • Each MIPS instruction is exactly 32 bits – R-type (register

MIPS Instruction Encoding • Each MIPS instruction is exactly 32 bits – R-type (register type) – I-type (immediate type) – J-type (jump type) op rs rt rd op rs rt 16 bit address or constant op 9/5/2021 shamt funct 26 bit address CDA 3100 11

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10 rd 6 5 shamt 0 funct rd rt add $4, $3, $2 rs 31 26 25 21 20 16 15 11 10 6 5 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 opcode rs rt rd shamt funct 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 Encoding = 0 x 00622020 9/5/2021 CDA 3100 12

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10 rd 6 5 shamt 0 funct rd rt sub $s 0, $t 1 rs 31 21 20 26 25 opcode rs 16 15 rt 11 10 rd 6 shamt 5 0 funct Encoding = 9/5/2021 CDA 3100 13

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10

R-Type Encoding 31 26 25 opcode 21 20 rs 16 15 rt 11 10 rd 6 5 shamt 0 funct rd rt sub $16, $8, $9 rs 31 21 20 26 25 16 15 11 10 6 5 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 opcode rs rt rd shamt funct 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 Encoding = 0 x 01098022 9/5/2021 CDA 3100 14

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate Value rt Immediate lw $5, 3000($2) rs 31 26 25 21 20 16 15 0 1 0 0 0 1 0 1 1 1 0 0 0 opcode rs rt Immediate Value 1 0 0 0 1 0 0 1 0 1 1 1 0 0 0 Encoding = 0 x 8 C 450 BB 8 9/5/2021 CDA 3100 15

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate Value rt Immediate sw $5, 3000($2) rs 31 26 25 opcode 16 15 21 20 rs rt 0 Immediate Value Encoding = 9/5/2021 CDA 3100 16

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate Value rt Immediate sw $5, 3000($2) rs 31 26 25 21 20 16 15 0 1 0 1 1 0 0 0 0 1 1 1 0 0 0 opcode rs rt Immediate Value 1 0 1 1 0 0 0 0 1 1 1 0 0 0 Encoding = 0 x. AC 450 BB 8 9/5/2021 CDA 3100 17

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate Value rt Immediate addi $s 0, 95 rs 31 26 25 opcode 16 15 21 20 rs rt 0 Immediate Value Encoding = 9/5/2021 CDA 3100 18

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate

I-type Encoding 31 26 25 opcode 21 20 rs 16 15 0 rt Immediate Value rt Immediate addi $s 0, 95 rs 31 26 25 21 20 16 15 0 0 0 1 0 0 0 01 0 0 0 0 1 1 1 opcode rs rt Immediate Value 0 0 1 0 0 0 0 0 0 0 1 1 1 Encoding = 0 x 2210005 F 9/5/2021 CDA 3100 19

J-type Encoding 31 26 25 0 opcode Jump Address j 0 x 0040007 c:

J-type Encoding 31 26 25 0 opcode Jump Address j 0 x 0040007 c: the address of the instruction to jump to. When encoding it, take the bit 2 to bit 27. 31 26 25 0 0 0 1 0 0 0 j 0 x 0040007 c 0 0 0 1 0 0 0 0 1 1 1 opcode Jump Address 0 0 0 0 1 0 0 0 0 0 1 1 1 Encoding = 0 x 0810001 F