write assembly language programs to: -define procedures and call them. -create l
ID: 3746739 • Letter: W
Question
write assembly language programs to:
-define procedures and call them.
-create loops
-use syscall operations to display integers and strings on the console window
-use syscall operations to read integers from the keyboard.
Assignment Description:
Implement a MIPS assembly language program that defines main, readArray and searchElement procedures.
The readArray takes an array of integers as its parameter, reads in integers from a user to fill the array and also print each value as long as it is with the number of elements specified by the parameter "howMany" and "length".
The searchElement procedure takes parameters of an array of integers, and its length, and asks a user how many integers to read in and calls the readArray procedure. It also asks a user to enter an integer to search. Then it should go through the array to see if each number is same as the integer to search. If an element in the array is the same as the integer to search, double its content, by multiplying it by 2, and set the variable found to 1 so that it can print out a message to say whether it was found or not. Then it prints out the updated content of the array.
The main procedure asks a user how many times to call the searchElement procedure, and it repeats it based on the entered number.
The following shows how it looks like in a C program:
The following is a sample output (user input is in bold):
Enter an integer to specify how many times to repeat:
2
Specify how many numbers should be stored in the array (at most 9):
8
Enter an integer:
1
Enter an integer:
-12
Enter an integer:
53
Enter an integer:
-4
Enter an integer:
5
Enter an integer:
32
Enter an integer:
1
Enter an integer:
7
The array content:
1
-12
53
-4
5
32
1
7
Enter an integer to search:
1
Result Array Content:
2
-12
53
-4
5
32
2
7
The entered element was found
Specify how many numbers should be stored in the array (at most 9):
7
Enter an integer:
1
Enter an integer:
5
Enter an integer:
1
Enter an integer:
-5
Enter an integer:
-2
Enter an integer:
3
Enter an integer:
2
The array content:
1
5
1
-5
-2
3
2
Enter an integer to search:
8
Result Array Content:
1
5
1
-5
-2
3
2
The entered element was not found
Explanation / Answer
.data
array: .space 40 #10 element integer array
mess_prompt: .asciiz " Enter an integer "
repeat_prompt: .asciiz " How many times it should be repeated :"
element_prompt: .asciiz " No of elements to be stored in an array (not more than 9):"
array_content: .asciiz " Array content :"
new_line: .asciiz " "
search_element_prompt: .asciiz " enter the integer to be searched :"
match_found_prompt: .asciiz " The entered element is found"
match_not_found_prompt: .asciiz " The entered element is not found"
.globl main
.text
main:
li $v0, 4 #Print string
la $a0, repeat_prompt #load prompt
syscall
li $v0, 5 #read int
syscall
move $s0, $v0 #store input in saved register
REPEAT_LOOP:
beq $s0,$0,EXIT
li $v0, 4 #Print string
la $a0, element_prompt #load prompt
syscall
li $v0, 5 #read int
syscall
move $s1, $v0 #store input in saved register
la $a0,array
move $a1,$s1
jal readLoop_func
la $a0,array
move $a1,$s1
jal printLoop_func
li $v0, 4 #Print string
la $a0, search_element_prompt #load prompt
syscall
li $v0, 5 #read int
syscall
move $s2, $v0 #store input in saved register
la $a0,array
move $a1,$s1
move $a2,$s2
jal searchElement
move $s3,$v0
la $a0,array
move $a1,$s1
jal printLoop_func
beq $s3,$0,NO_MATCH
li $v0, 4 #Print string
la $a0, match_found_prompt #load prompt
syscall
b NEXT
NO_MATCH:
li $v0, 4 #Print string
la $a0, match_not_found_prompt #load prompt
syscall
NEXT:
addi $s0,$s0,-1
b REPEAT_LOOP
EXIT:
li $v0,10
syscall
readLoop_func:
li $t0,0
move $t1,$a0
move $t2,$a1
readLoop:
beq $t0, $t2, END_OF_READLOOP #loop condition
li $v0, 4 #Print string
la $a0, mess_prompt #load prompt
syscall
li $v0, 5 #read int
syscall
move $t3, $v0 #store input in array
sw $t3,0($t1)
li $v0,1
move $a0,$t3
syscall
addi $t0, $t0, 1 #increment counter
addi $t1, $t1, 4 #increment address
b readLoop
END_OF_READLOOP:
jr $ra
printLoop_func:
li $t0,0
move $t1,$a0
move $t2,$a1
li $v0, 4 #Print string
la $a0, array_content #load prompt
syscall
printLoop:
beq $t0, $t2, END_OF_PRINTLOOP #loop condition
li $v0, 1 #print int
lw $a0, 0($t1)
syscall
li $v0, 4 #Print string
la $a0, new_line #load prompt
syscall
addi $t0, $t0, 1 #increment counter
addi $t1, $t1, 4 #increment address
b printLoop
END_OF_PRINTLOOP:
jr $ra
searchElement:
li $t0,0
move $t1,$a0
move $t2,$a1
move $t3,$a2
search_loop:
beq $t0, $t2, END_OF_SEARCHLOOP #loop condition
lw $t4,0($t1)
beq $t4,$t3,MATCH_FOUND
addi $t0, $t0, 1 #increment counter
addi $t1, $t1, 4 #increment address
b search_loop
END_OF_SEARCHLOOP:
li $v0,0
jr $ra
MATCH_FOUND:
sll $t4,$t4,1
sw $t4,0($t1)
li $v0,1
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.