Untitled
unknown
plain_text
8 months ago
2.1 kB
1
Indexable
Never
Write a program allowing user input all elements of 2D array M[3][3] and print sum of (M[0][0]+ M[1][1]+ M[2][2]) .data prompt_row1_col1: .asciiz "Enter the value of A[0][0]: " prompt_row1_col2: .asciiz "Enter the value of A[0][1]: " prompt_row1_col3: .asciiz "Enter the value of A[0][2]: " prompt_row2_col1: .asciiz "Enter the value of A[1][0]: " prompt_row2_col2: .asciiz "Enter the value of A[1][1]: " prompt_row2_col3: .asciiz "Enter the value of A[1][2]: " prompt_row3_col1: .asciiz "Enter the value of A[2][0]: " prompt_row3_col2: .asciiz "Enter the value of A[2][1]: " prompt_row3_col3: .asciiz "Enter the value of A[2][2]: " M: .space 36 size: .word 3 .text .globl main main: la $t0, M # load array M li $v0, 4 la $a0, prompt_row1_col1 syscall li $v0, 5 syscall sw $v0, 0($t0) li $v0, 4 la $a0, prompt_row1_col2 syscall li $v0, 5 syscall sw $v0, 4($t0) li $v0, 4 la $a0, prompt_row1_col3 syscall li $v0, 5 syscall sw $v0, 8($t0) li $v0, 4 la $a0, prompt_row2_col1 syscall li $v0, 5 syscall sw $v0, 12($t0) li $v0, 4 la $a0, prompt_row2_col2 syscall li $v0, 5 syscall sw $v0, 16($t0) li $v0, 4 la $a0, prompt_row2_col3 syscall li $v0, 5 syscall sw $v0, 20($t0) li $v0, 4 la $a0, prompt_row3_col1 syscall li $v0, 5 syscall sw $v0, 24($t0) li $v0, 4 la $a0, prompt_row3_col2 syscall li $v0, 5 syscall sw $v0, 28($t0) li $v0, 4 la $a0, prompt_row3_col3 syscall li $v0, 5 syscall sw $v0, 32($t0) jal sumDiagonal move $a0, $v0 # v0 has the sum li $v0, 1 syscall # End program li $v0, 10 syscall sumDiagonal: li $v0, 0 # sum = 0 lw $t1, size li $t4, 0 # index = 0 sumLoop: mul $t2, $t4, $t1 # t2 = rowIndex * colSize add $t2, $t2, $t4 # + colIndex mul $t2, $t2, 4 # * dataSize add $t2, $t2, $t0 # + baseAddress lw $t3, ($t2) add $v0, $v0, $t3 # sum = sum + mdArray[i][i] addi $t4, $t4, 1 blt $t4, $t1, sumLoop # if i < size jr $ra
Leave a Comment