Docsity

MIPS Assembly Code: Comparing Two Arrays and Identifying Bugs, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
Problem 1:
1. addi $s2, $s4, 3
I-format instr: op = 8, rs = 20, rt = 18, imm = 3.
Encoding: 0x22920003
2. j func1
J-format: op = 2, address = 2000
Encoding: 0x080007D0
3. beq $s2, $s3, label 1 #label precedes the 10th instr after this branch
I-format: op = 4, rs = 18, rt = 19, imm = 9
Encoding: 0x12530009
4. sw $s0, 8($sp)
I-format: op = 43, rs = 29, rt = 16, imm = 8
Encoding: 0xAFB00008
5. slt $t2, $s3, $s4
R-format, op = 0, rs = 19, rt = 20, rd = 10, shamt = 0, funct = 42
Encoding: 0x0274502A
Problem 2:
sll $t0, $t3, 9 #shift $t3 by 9 bits and store in $t0
srl $t0, $t0, 15 # shift $t0 right by 15 bits
Problem 3:
sll $a2, $a2, 2 #max I = 2500 * 4
sll $a3, $a3, 2 #max j = 2500 * 4
add $v0, $zero, $zero #$v0 = 0
add $t0, $zero, $zero # i=0
outer: add $t4, $a0, $t0 # $t4 = address of array1[i]
lw $t4, 0($t4) # $t4 =array1[i]
add $t1, $zero, $zero #j=0
inner: add $t3, $a1, $t1 #$t3= address of arrary2[j]
lw $t3, 0($t3) # $t3=array2[j]
bne $t3, $t4, skip # if (array1[i]!=array2[j]) skip $v0+=1
pf3

Partial preview of the text

Download MIPS Assembly Code: Comparing Two Arrays and Identifying Bugs and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

Problem 1:

  1. addi $s2, $s4, 3 I-format instr: op = 8, rs = 20, rt = 18, imm = 3. Encoding: 0x
  2. j func J-format: op = 2, address = 2000 Encoding: 0x080007D
  3. beq $s2, $s3, label 1 #label precedes the 10th^ instr after this branch I-format: op = 4, rs = 18, rt = 19, imm = 9 Encoding: 0x
  4. sw $s0, 8($sp) I-format: op = 43, rs = 29, rt = 16, imm = 8 Encoding: 0xAFB
  5. slt $t2, $s3, $s R-format, op = 0, rs = 19, rt = 20, rd = 10, shamt = 0, funct = 42 Encoding: 0x0274502A

Problem 2:

sll $t0, $t3, 9 #shift $t3 by 9 bits and store in $t srl $t0, $t0, 15 # shift $t0 right by 15 bits

Problem 3:

sll $a2, $a2, 2 #max I = 2500 * 4 sll $a3, $a3, 2 #max j = 2500 * 4 add $v0, $zero, $zero #$v0 = 0 add $t0, $zero, $zero # i= outer: add $t4, $a0, $t0 # $t4 = address of array1[i] lw $t4, 0($t4) # $t4 =array1[i] add $t1, $zero, $zero #j= inner: add $t3, $a1, $t1 #$t3= address of arrary2[j] lw $t3, 0($t3) # $t3=array2[j] bne $t3, $t4, skip # if (array1[i]!=array2[j]) skip $v0+=

addi $v0, $v0, 1 # $v0++ skip addi $t1, $t1, 4 # j++ bne $t1, $a3, inner # loop if j != 2500 * 4 addi $t0, $t0, 4 # i++ bne $t0, $a2, outer # loop if i != 2500 * 4 The code determines the number of matching elements between the two arrays and returns this number in register $v0.

Problem 4:

Bugs in the MIPS program:  Bug 1: Count ($v0) is initialized to zero, not –1 to avoid counting zero word.  Bug 2: Count ($v0) is not incremented.  Bug 3: Loops if word copied is equal to zero rather than not equal. Bug-free version: addi $v0, $zero, -1 # Initialize to avoid counting zero word loop: lw, $v1, 0($a0) # Read next word from source addi $v0, $v0, 1 # Increment count words copied sw $v1, 0($a1) # Write to destination addi $a0, $a0, 4 # Advance pointer to next source addi $a1, $a1, 4 # Advance pointer to next destination bne $v1, $zero, loop # Loop if word copied != zero

Problem 5:

Pseudoinstruction Solution

  1. move $t1, $t2 add $t1, $t2, $zero
  2. clear $t0 add $t0, $zero, $zero
  3. beq $t1, small, L addi $at, $zero, small