Write a program using a 2-Dimensional Array and display the value 20.
#MIPS assembly
#MARS
.data
height: .byte 4
width: .byte 5
.align 4
array2D: .word 11,12,13,14,15
.word 21,22,23,24,25
.word 31,32,33,34,35
.word 41,42,43,44,45
msgArray: .asciiz "2-Dimenisonal Array:\n"
msg20: .asciiz "\nDisplay the value: "
Space_: .asciiz " "
newLine: .asciiz "\n"
.text # .text section of program (contains executable instructions)
# Executable instructions are stored in program memory
.globl main # identifies the starting point of the program with label "main"
main:
li $v0,4 # print string
la $a0,msgArray # "2-Dimenisonal Array\n"
syscall
la $s0, array2D # begining address in array2D
la $t1, width
lb $t1, 0($t1) # width
la $t2, height
lb $t2, 0($t2) # height
li $t3,0 # j - counter array2D elements in rows
li $t4,0 # i - counter array2D elements in columns
Loop1:
slt $t7, $t4, $t2 # IF(i < height) continue
beq $t7, $zero, endLoop1 # ELSE goto endLoop1
Loop2:
slt $t7, $t3, $t1 # IF(j < width) continue
beq $t7, $zero, endLoop2 # ELSE goto endLoop2
li $v0, 1 # print integer
lw $a0,0($s0) # array2D[i][j] to $a0
syscall # make the syscall
li $v0,4 # print string
la $a0,Space_ # " "
syscall # make the syscall
addi $t3, $t3, 1 # j++
addi $s0, $s0, 4 # next address in array2D
j Loop2
endLoop2:
addi $t4, $t4, 1 # i++
li $v0,4 # print string
la $a0,newLine # "/n"
syscall
li $t3,0 # j=0
j Loop1
endLoop1:
li $v0,4 # print string
la $a0,msg20 # "Display the value: "
syscall
li $v0, 1 # print integer
li $a0,20 #
syscall
li $v0,4 # print string
la $a0,newLine # "\n"
syscall
li $v0, 10 # Sets $v0 to "10" to select exit syscall
syscall # make the syscall Exit
Comments
Leave a comment