MIPS Assembly Language Programming CDA 3101 Fall 2010 Discussion Session 07 MIPS Assembly Language Programming 1 1
Instruction Register add $t0, $t1, $t2 ## RF[Rd] =RF[Rs] +RF[Rt] Register – Format (add Rd, Rs, Rt) Op-code Rs Rt Rd Shamt Function-code 6 5 5 5 5 6 add $t0, $t1, $t2 ## RF[Rd] =RF[Rs] +RF[Rt] 2 2 2
Instruction Register addi $t0, $t1, 100 ## RF[Rt] =RF[Rs] + se Imm Immediate – Format (addi Rt, Rs, Imm) Op-code Rs Rt Immediate 6 5 5 16 2’s Complement ? addi $t0, $t1, 100 ## RF[Rt] =RF[Rs] + se Imm se Imm = {16 {immediate[15]}, immediate} beq Rs, Rt, Label ##if(RF[Rs] == Rf[Rt]) PC = PC +4 + BranchAddr* *BranchAddr = {14{immediate[15]}, immediate, 2’b0} 3 3 3
Instruction Register j Label ## PC = JumpAddr Jump– Format (j addr) Op-code Address 6 26 j Label ## PC = JumpAddr JumpAddr = { PC[31:28], address, 2’b0} 4 4 4
Problem Assume the address start from 0x00400024, translate the following MIPS code into the corresponding machine code. lable1: addi $t0, $0, 1 0x00400024 sll $t0, $t0, 2 0x00400028 bgez $t0, lable1 0x0040002C and $s0, $s0, $t0 0x00400030 lable2: bne $s0, $0, lable3 0x00400034 lb $t4, 8($s4) 0x00400038 sb $t4, -8($s4) 0x0040003C lable3: j lable1 0x00400040 5 5 5
Problem [0x00400024] 001000 00000 01000 0000000000000001 ;addi $8, $0, 1 [0x00400028] 000000 00000 01000 01000 00010 000000 ;sll $8, $8, 2 [0x0040002c] 000001 01000 00001 1111111111111101 ;bgez $8 -12 [lable1-0x0040002c - 4] [0x00400030] 000000 10000 01000 10000 00000 100100 ;and $16, $16, $8 [0x00400034] 000101 10000 00000 00000000 0000 0010 ;bne $16, $0, 8 [lable3-0x00400034- 4] [0x00400038] 100000 10100 01100 0000000000001000 ;lb $12, 8($20) [0x0040003c] 101000 10100 01100 1111111111111000 ;sb $12, -8($20) [0x00400040] 000010 00 00010000 00000000 00001001 ;j 0x00400024 [lable1] 6 6 6