Question
1/1 Exercise 10 The purpose of this exercise is to familiarize with the implementation of procedures (functions) in the proce
1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

solution:

given data:

a

.data
.align 4
Array: .space 500
msg1: .asciiz "Enter Integer : "
msg2: .asciiz " "
msg3: .asciiz "\nSorted array is : "
msg4: .asciiz "Size of array is : "
msg7: .asciiz "Enter numbers below , Enter zero to stop\n"
.text
.globl main
main:

li $v0,4
la $a0,msg7
syscall

addi $t0,$zero,0

li $s0,0 #it will store number of total element

in:
#prompt for integer
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
beq $v0,$zero,done #if enter 0 return
add $t1,$t0,$zero
sll $t1,$t0,2
add $t3,$v0,$zero
#store data at location
sw $t3,Array ( $t1 )
addi $t0,$t0,1

slt $t1,$s0,$t0
addi $s0,$s0,1
j in #jump to read more data

done:
#reduce s0 by 1 as zero based index
subi $s0,$s0,1

add $sp,$sp,-4
la $a0,Array
sw $a0,($sp) #load first parameter
addi $a1,$s0,1 #a1=6
sw $a1,4($sp) #load first parameter
#call Bubsort

jal Bubsort

addi $s5,$s0,1 #get number of element
li $v0,4
la $a0,msg4
syscall
li $v0,1
move $a0,$s5
syscall

#print table
li $v0,4
la $a0,msg3
syscall
la $t0,Array
#s0=5
add $t1,$zero,$zero

printtable:
lw $a0,0($t0)
li $v0,1
syscall
li $v0,4
la $a0,msg2
syscall
addi $t0,$t0,4
addi $t1,$t1,1
slt $t2,$s0,$t1
beq $t2,$zero,printtable

li $v0,10
syscall

Bubsort:

lw $a0,($sp) #load first parameter
lw $a1,4($sp) #load first parameter

#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0

loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 < a1 break;

add $t1,$a1,$zero #counter2=size=6
loop2:

bge $t0,$t1,loop1 #j < = i

#slt $t3,$t1,$t0
#bne $t3,$zero,loop1

addi $t1,$t1,-1 #j--

mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a0 #t7=table[j]
add $t8,$t3,$a0 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)

bgt $t5,$t6,loop2

#switch t5,t6
sw $t5,0($t8)
sw $t6,0($t7)
j loop2

endloop1:
jr $ra

Mars Messages Run I/O Enter numbers below, Enter zero to stop Enter Integer : 1 Enter Integer : 2 Enter Integer : 1 Enter Int

b

.data
.align 4
Array: .space 500
msg1: .asciiz "Enter Integer : "
msg2: .asciiz " "
msg3: .asciiz "\nSorted array is : "
msg4: .asciiz "Size of array is : "
msg7: .asciiz "Enter numbers below , Enter zero to stop\n"
.text
.globl main
main:

li $v0,4
la $a0,msg7
syscall

addi $t0,$zero,0

li $s0,0 #it will store number of total element

in:
#prompt for integer
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
beq $v0,$zero,done #if enter 0 return
add $t1,$t0,$zero
sll $t1,$t0,2
add $t3,$v0,$zero
#store data at location
sw $t3,Array ( $t1 )
addi $t0,$t0,1

slt $t1,$s0,$t0
addi $s0,$s0,1
j in #jump to read more data

done:
#reduce s0 by 1 as zero based index
subi $s0,$s0,1

add $sp,$sp,-4
la $a0,Array
sw $a0,($sp) #load first parameter
addi $a1,$s0,1 #a1=6
sw $a1,4($sp) #load first parameter
#call Bubsort

jal Bubsort

addi $s5,$s0,1 #get number of element
li $v0,4
la $a0,msg4
syscall
li $v0,1
move $a0,$s5
syscall

#print table
li $v0,4
la $a0,msg3
syscall
la $t0,Array
#s0=5
add $t1,$zero,$zero

printtable:
lw $a0,0($t0)
li $v0,1
syscall
li $v0,4
la $a0,msg2
syscall
addi $t0,$t0,4
addi $t1,$t1,1
slt $t2,$s0,$t1
beq $t2,$zero,printtable

li $v0,10
syscall

Bubsort:

lw $a0,($sp) #load first parameter
lw $a1,4($sp) #load first parameter

#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0

loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 < a1 break;

add $t1,$a1,$zero #counter2=size=6
loop2:

bge $t0,$t1,loop1 #j < = i

#slt $t3,$t1,$t0
#bne $t3,$zero,loop1

addi $t1,$t1,-1 #j--

mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a0 #t7=table[j]
add $t8,$t3,$a0 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)

bgt $t5,$t6,loop2

#switch t5,t6
move $s5,$ra #store ra
jal swap
move $ra,$s5 #restore ra
j loop2

endloop1:
jr $ra

swap:
sw $t5,0($t8)
sw $t6,0($t7)
jr $ra #jump to caller

1 Enter Integer : 3 Enter Integer : 2 Enter Integer : 4 Enter Integer : 0 size of array is : 3 sorted array is : 2 3 4 progra

c)

Here the issue to call swap function it that, We need to store the value of $ra somewhere before calling swap function.

Becuase calling another procedure will override the value of $ra. And it should be restored when returning from the function.

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
The purpose of this exercise is to familiarize with the implementation of procedures (functions) in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Write a program that sorts the given sequence of integers values using Select Sort algorithm. Structure of the program should be as follows: "The master" is responsible for communication with the user - input and output of the program. Sorting algorithm s

    Write a program that sorts the given sequence of integers values using Select Sort algorithm. Structure of the program should be as follows: "The master" is responsible for communication with the user - input and output of the program. Sorting algorithm should be implemented as a procedure called from the main module, let’s call it “Sort”. Then all swap operations should be performed by the procedure “Swap” called from procedure “Sort”.

  • Instructions C++ programming Your task is to write a class called Data, stored in a file...

    Instructions C++ programming Your task is to write a class called Data, stored in a file named Data.h. Your class should be able to store a collection (vector) of integers. In addition to the appropriate constructors, your class should also have the following methods. void add (int number); Adds a number to the data set. void print (); Prints out the entire data set on a single line, separated by space. void sort (); Sorts the data set in ascending...

  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Have you ever wanted to predict the future? Well the Magic Eight Ball does just that....

    Have you ever wanted to predict the future? Well the Magic Eight Ball does just that. The original game was a softball sized “8-ball”. You would ask a question, shake it up and look at the result. There are 20 responses…10 positive, 5 negative, and 5 are vague. For this project, we want to recreate this, but give the ability to read in a set of responses, and add additional responses, and print out all of the responses in alphabetical...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT