Answer to Question #298128 in Assembler for sksss

Question #298128

Let the following array as matrix; write the MIPS code to transpose it.

class_marks: .word 10,11,12,13

.word 20,21,22,23

.word 30,31,32,33

.word 40,41,42,43


1
Expert's answer
2022-02-15T09:32:03-0500
#MIPS MARS assembly
#************************************************
    .data
class_marks:    .word    10, 11, 12, 13   
                .word    20, 21, 22, 23
                .word    30, 31, 32, 33
                .word    40, 41, 42, 43
matT:    .space 64    
col:    .word    4
row:    .word    4
endl:    .asciiz    "\n"
space:    .asciiz    " "

    .text
main:
    la    $a0, class_marks    # address class_marks
    la    $a3, matT            # address matT
    lw    $a1, col
    lw    $a2, row
    jal matTrans    
    
    la    $a0, matT    # address  matT
    lw    $a1, row
    lw    $a2, col
    jal    matPrint    # print marix matT
    
    li    $v0, 4        # print string  
    la    $a0, endl    # end line
    syscall    
    
    li    $v0, 10     # finished .. stop .. return
    syscall
########################################################            
# print matrix
# input:
#     $a0    Address of matrix
#     $a1    col
#     $a2    row
#######################################################
matPrint:
    add $t0,$zero,$a1    # col
    mul $t0,$t0,$a2        # size = col*row  ($t0)
    add $t1,$zero,1        # i = 1    ($t1)
    add $t2,$zero,$a0    # address of matrix ($t2)
    add $t3,$zero,$a1    # counter of colums
while_matPrint:
    slt $t7, $t0, $t1    # if(size > i)-> $t7 = 0 (goto continue_1)
    beq $t7,$zero, continue_matPrint    # $t7 == 0 goto continue_1
    j matPrintEnd                # else goto matPrintEnd
    
continue_matPrint:    
    li    $v0, 1
    lw    $a0, 0($t2)        # print mat[...]
    syscall
        
    li    $v0, 4        # print string
    la    $a0, space
    syscall
    
    beq $t3,$t1, newLine    # if end of row goto print newLine
    j continue_2matPrint                # else print next mat[...]
    
newLine:    
    li    $v0, 4    # print endl
    la    $a0, endl
    syscall    
    add $t3,$t3,$a1        # $t3 = number of last element in row
    
continue_2matPrint:
    addi $t2, $t2, 4    # address of next in matrix
    addi $t1, $t1, 1    # i++
    j    while_matPrint        
    
matPrintEnd:    
    jr    $ra
    
#*****************************************************
# Transport matrix class_marks into matT
# input:
#     $a0    Address of matrix's class_marks
#     $s3    Address of matT
#     $a1    col
#     $a2    row
# ****************************************************
matTrans:
    addi $sp, $sp, -20     # make space on stack to store  registers
    sw $s0, 4($sp)
    sw $s1, 8($sp)
    sw $s2, 12($sp)
    sw $s3, 16($sp)

    add  $t0,$zero,$a1        # number col
    mul  $t0,$t0,$a2        # size class_marks = col*row  ($t0)
    add  $s3,$zero,1        # counter of size
    add  $t1,$zero,1        # i counter of col in class_marks
    add  $t2,$zero,$a0    # address of matrix class_marks
    add  $t3,$zero,$a1    # number colums in class_marks
    addi $t4,$zero,0    # j counter of row in class_marks
    add  $s0,$zero,$a3    # address  matT  ($s0)
    sll  $t5,$a2,2        # offset_Col in matT (col)
while:
    slt $t7, $t0, $s3    # if(size > i)-> $t7 = 0
    beq $t7,$zero, continue    # $t7 == 0 goto continue
    j matTransEnd                # else goto End
continue:    
    lw $s1,0($t2)    # read from class_marks
    sw $s1,0($s0)    # write into MatT    
    
    beq $t3,$t1, newRow    # if end of row goto newRow
    j continue_2        # else next element class_marks[...]
    
newRow:    
# determine the address of the next column in the matT
    addi $t4,$t4,1        # j counter of row in class_marks
    add $s0,$zero,$a3    # address  matT  ($s0)
    sll $t6,$t4,2        # offset_Row in matT (begin new col in matT)
    add $s0,$s0,$t6        # address new col in matT-offset
    sub $s0,$s0,$t5     # -offset_Col
    add $t1,$zero,0        # i = 0    ($t1)  
    add $t3,$zero,$a1        # $t3 = number of  element in row
    
continue_2:
    addi $t2, $t2, 4    # address of next in class_marks
    addi $t1, $t1, 1    # i++ counter col (in row A)
    addi $s3, $s3, 1    #   size
    add  $s0, $s0, $t5    # address of next in col matT
    j    while        
    
matTransEnd:
    lw $s3, 16($sp)
    lw $s2, 12($sp)
    lw $s1, 8($sp)
    lw $s0, 4($sp)
     addi $sp, $sp, 20
         jr    $ra   

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS