Write a program in assembly language using 2-Dimenisonal Array and display the value 20 in MIPS
#MIPS assembly
.data
height: .byte 4
width: .byte 5
.align 4
array: .word 1,2,3,4,5
.word 2,3,4,5,6
.word 3,4,5,6,7
.word 4,5,6,7,8
msgArray: .asciiz "2-Dimenisonal Array:\n"
msg20: .asciiz "\nDisplay the value 20: "
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, array # begining address in array
la $t1, width
lb $t1, 0($t1) # $t1 = width
la $t2, height
lb $t2, 0($t2) # $t2 = height
li $t3,0 # j - counter array elements in rows
li $t4,0 # i - counter array 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 from $a0
lw $a0,0($s0) # load array[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 array
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 20: "
syscall
li $v0, 1 # print integer from $a0
li $a0,20 # $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