Show transcribed image text
Expert Answer
- Comment
Program Screenshot
max.s
Sample Output in QtSPIM
Program to Copy
max.s
.data strA: .asciiz "Original Array:\n" strB: .asciiz "Second Array:\n" newline:.asciiz "\n" space: .asciiz " " # This is the start of the original array Original:.word 200, 270, 250, 100 .word 205, 230, 105, 235 .word 190, 95, 90, 205 .word 80, 205, 110, 215 # The net statement allocates roomfor the resuls in 4*4 16 bytes # Max: .space 16 .align 2 .globl main .text main: la $a0,Original # $a0= address of the array li $a1,4 # $a1= number of rows li $a2,4 # $a2=number of columns jal findMax # call procedure findMax for finding maximum in each rows #Print a message "Original Array:\n" li $v0, 4 # load 4 into $v0 (for printing a string) la $a0,strA # load address of string to register $a0 syscall # print the corresponding string to console la $a0,Original # $a0= address of the array li $a1,4 # $a1= number of rows li $a2,4 # $a2=number of columns jal printArray # call printArray for printing original array #Print a message "Second Array:\n" li $v0, 4 # load 4 into $v0 (for printing a string) la $a0,strB # load address of string to register $a0 syscall # print the corresponding string to console la $a0,Max # $a0= address of the array li $a1,1 # $a1= number of rows li $a2,4 # $a2=number of columns jal printArray # call printArray for printing second array #follwing lines used to exit OS STOP: li $v0, 10 syscall # Procedure findMax # Arguments:$a0= address of the array,# $a1= number of rows, # $a2=number of columns findMax: move $s0,$a0 # $s0= $a0 li $t0,0 # $t0=0 ,loop control register (i) la $s1,Max # load $s1 with address of array Max L1: bge $t0,$a1,Ret # if i>=Number of rows, then goto label Ret li $t1,0 # $t1=0 ,loop control register (j) lw $t5,0($s0) # load $t5 with element at position i-th row and 0 -th column as maximum of that row L2: bge $t1,$a2,Next # if j>=Number of columns, then goto label Next lw $t6,0($s0) # load $t6 with element at position i-th row and j -th column bge $t5,$t6 Skip # if the current maximum of the row >=current array element in $t6, then skip the element move $t5,$t6 # otherwise change maximum Skip: addi $t1,$t1,1 # increment j addi $s0,$s0,4 # increment $s0 by 4 getting next element j L2 # Repeat from L2 Next: sw $t5,0($s1) # Store the row maimum to array Max addi $s1,$s1,4 # increment $s1 by 4 for storing next maximum addi $t0,$t0,1 # increment i j L1 # Repeat from L1 Ret: jr $ra # Return from proceedure # Procedure printArray # Arguments:$a0= address of the array,# $a1= number of rows, # $a2=number of columns printArray: move $s0,$a0 # $s0= $a0 li $t0,0 # $t0=0 ,loop control register (i) PrnL1: bge $t0,$a1,PrnRet # if i>=Number of rows, then goto label PrbRet li $t1,0 # $t1=0 ,loop control register (j) PrnL2: bge $t1,$a2,PrnNext # if j>=Number of columns, then goto label PrnNext lw $a0,0($s0) # load $a0 with element at position i-th row and j -th column li $v0,1 # System call service for printing anumber syscall # print the number in $a0 #Print a message soace li $v0, 4 # load 4 into $v0 (for printing a string) la $a0,space # load address of string to register $a0 syscall # print the corresponding string to console addi $t1,$t1,1 # increment j addi $s0,$s0,4 # increment $s0 by 4 getting next element j PrnL2 # Repeat from PrnL1 PrnNext: # Print a message new line li $v0, 4 # load 4 into $v0 (for printing a string) la $a0,newline # load address of string to register $a0 syscall # print the corresponding string to console addi $t0,$t0,1 # increment j j PrnL1 # Repeat from PrnL1 PrnRet: jr $ra # Return from proceedure
ليست هناك تعليقات:
إرسال تعليق