Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i. Write a MIPS program that reads and stores an array of 10 records, each recor

ID: 3796759 • Letter: I

Question

i. Write a MIPS program that reads and stores an array of 10 records, each record consists of name of type string of up to 40 characters, age of type integer, and salary of type integer.

ii. Write a program that prints the array of records of Part(i) in a readable format

iii. Write a program that swaps any adjacent records, and then prints the entire array. The program must ask the user to enter the record numbers to be swapped.

The following code illustrates how a record can be read, stored, and printed.

#The following reads two records Emp1 and Emp2, each record consists of two attributes

name and of type string of up to 40 characters and salary of type integer. and then

prints both records

.data

Emp1: .space 44

Emp2: .space 44

newline: .asciiz" "

.text

#read and store the name of the first record.

la $a0, Emp1

li $a1, 40

li $v0, 8

syscall

#reads and store the salary of the first record.

li $v0, 5

syscall

sw $v0, 40($a0)

#read and store the name of the second record.

la $a0, Emp2

li $a1, 40

li $v0, 8

syscall

#read and store the salary of the second record.

li $v0, 5

syscall

sw $v0, 40($a0)

# print the name of first record.

li $v0,4

la $a0, Emp1

syscall

#print the salary of the first record.

li $v0, 1

lw $t0, 40($a0)

move $a0, $t0

syscall

# start a new line

li $v0,4

la $a0, newline

syscall

# print the name of the second record.

li $v0,4

la $a0,Emp2

syscall

# print the salary of the second record.

li $v0, 1

lw $t0, 40($a0)

move $a0, $t0

syscall

Explanation / Answer

/* program of mips*/

.data
array: .space 100
input: .asciiz "Enter at least 4 integers: Enter the number 999 to exit "
output: .asciiz "The array in ascending order: "
commas: .asciiz ","

.text
.globl main
main:

la $a1, array #loads a pointer to array into $a1
li $a2,9 #loads 9 into $a2
li $t0,0
li $t1,999

loops:

la $a0, input #loads input text into $a
li $v0, 4 #loads 4 into $v0 (prints string)
syscall   
li $v0, 5 #loads 5 into $v0 (read interger)
syscall   
beq $v0,$t1,swap
addi $t0,$t0,4 #add 4 to $t0, save to $t0
sw $v0, ($a1) #stores input into array
addi $a1, $a1,4 #add 4 to $a1, save to $a1
j loops

swap:

la $t4, array #loads array to $t4
la $t1, array #loads array to $t1
addi $t1,$t1,4 #add 4 to $t1, save to $t1
la $t8,array #loads array to $t8
add $t8,$t0,$t8 #add $t8 to $t0, save to $t8
la $t9,array
add $t9,$t0,$t9 #add $t9 to $t0, save to $t9
addi $t9,$t9,-4 #subtracts 4 from $t9, save to $t9

loop:

lw $t2,($t4) #load input into $t2
lw $t3,($t1) #load input into $t3
bgt $t2,$t3,loop1 #if $t2 > $t3, go to loops
#blt $t2,$t3,loop1 #if $t2 < $t3, go to loops
sw $t3,($t4) #store $t3 in $t4
sw $t2,($t1) #store $t2 in $t1

loop1:

addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t1,$t8,loop #if $t1<$t8, go to loop
addi $t4,$t4,4 #add 4 to $t4, save to $t4
move $t1,$t4
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t4,$t9,loop #if $t4<$t9, to go loop

print:

la $a1,array #loads array to $a1
la $a0, output #loads output to $a0
li $v0, 4 #loads 4 into #v0
syscall

loop2:

blez $t0, done #if $t0<=0, go to done
li $v0, 1 #loads 1 into $v0
lw $a0, 0($a1) #load an inout into $a0
syscall
la $a0, commas #loads commas into $a0
li $v0, 4 #loads 4 into $v0
syscall
addi $a1, $a1, 4 #add 4 to $a1, save to $a1
addi $t0, $t0, -4 #subtracts 4 from #t0, save to $t0
j loop2

done:
j done