Write the following program in MIPS:
a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44
b) declare a variable called size which stores the number of element in array A, that is 10.
c) write a subroutine to search for a number stored in an array
and return true or false. In C++ the subroutine is as
follows:
search(array, size, number_To_Search)
e.g. search(A, 10, 12)
The subroutine should return 0 for false or 1 for true in register
$v0.
d) The parameters to the search subroutine are stored in
$a0=stores base address of A, $a1=stores the number of elements in
A that is 10, $a2=stores the number to search for (e.g. 12)
e) In C++, you would write:
for (int i=0; i<size; i++)
if A[i] == number_to_search return true;
return false
Your MIPS program is a translation of the C++ code.
f) You can test your program by writing the following
instructions:
la $a0, A
lw $a1, size
addi $a2, $zero, 12
jal search # search for 12 in the array A of size
10
lw $a0, $v0 # subroutine search result is store in $v0;
its value is moved to $a0
lw $v0, 1 # code to print integer value in
$a0
syscall
lw $v0, 10 # code to terminate program
syscall
subroutine in MIPS to search an element is given below:
search(int*, int, int):
daddiu $sp,$sp,-48
sd $fp,40($sp)
move $fp,$sp
sd $4,16($fp)
move $3,$5
move $2,$6
sll $3,$3,0
sw $3,24($fp)
sll $2,$2,0
sw $2,28($fp)
sw $0,0($fp)
.L5:
lw $3,0($fp)
lw $2,24($fp)
slt $2,$3,$2
beq $2,$0,.L2
nop
lw $2,0($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $3,0($2)
lw $2,28($fp)
bne $2,$3,.L3
nop
li $2,1 # 0x1
b .L4
nop
.L3:
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
b .L5
nop
.L2:
move $2,$0
.L4:
move $sp,$fp
ld $fp,40($sp)
daddiu $sp,$sp,48
j $31
nop
.LC0:
.word 3
.word 5
.word 8
.word 10
.word 12
.word 2
.word 76
.word 43
.word 90
.word 44
MIPS for declaring array, variable size and calling subroutine search is given below:
ld $2,%got_page(.LC0)($28)
daddiu $3,$2,%got_ofst(.LC0)
ldl $3,7($3)
ldr $3,%got_ofst(.LC0)($2)
move $6,$3
daddiu $3,$2,%got_ofst(.LC0)
ldl $4,15($3)
ldr $4,8($3)
move $5,$4
daddiu $3,$2,%got_ofst(.LC0)
ldl $4,23($3)
ldr $4,16($3)
daddiu $3,$2,%got_ofst(.LC0)
ldl $7,31($3)
ldr $7,24($3)
move $3,$7
daddiu $2,$2,%got_ofst(.LC0)
ldl $7,39($2)
ldr $7,32($2)
move $2,$7
sd $6,8($fp)
sd $5,16($fp)
sd $4,24($fp)
sd $3,32($fp)
sd $2,40($fp)
li $2,10 # 0xa
sw $2,0($fp)
lw $3,0($fp)
daddiu $2,$fp,8
li $6,12 # 0xc
move $5,$3
move $4,$2
ld $2,%got_disp(search(int*, int, int))($28)
move $25,$2
nop
Thank You.
Get Answers For Free
Most questions answered within 1 hours.