0 Replies - 1596 Views - Last Post: 21 November 2012 - 01:30 PM Rate Topic: -----

#1 jonny89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-November 12

Procedure to print all elements of an array. Error:(Exceptions 4/7)

Posted 21 November 2012 - 01:30 PM

I am new to programming in QtSpim , I can easily print numbers in the console and understand how to use branching and loops. I think that printing out the values of an array in the console seems simple, yet I get two errors when trying to compile the code, these are 'Exception 4', and 'Exception 7'. Does anyone know why I get this error, I think its to do with address size mismatch but don't really understand the concept and can't seem to find any good information regarding it. Thank you for your patience!

.data
randnum:
.word 1,3,5,6,2,7,0,4,8,9 # store the words in successive memory locations, declared data segment and placed code inside
.data
str_1:
.asciiz "\n"

.text
.globl main


wrt_array:

subu $sp,$sp,32 # Stack frame is 32 bytes long
sw $ra,20($sp) # Save return address
sw $fp,16($sp) # Save callerís frame pointer
addu $fp,$sp,28 # Set up frame pointer


sw $s0, 24($sp) #store the lower word of $s0 to address
sw $s1, 28($sp) #store the lower word of $s0 to address

move $s0, $a0 #good practise to do this
move $s1, $a1 #good practise to do this

li $s2, 9 #load constant at 9 into $s2
li $s0, 0 #load the constant at 0 into $s0

loop:

lw $a0, ($s0) # address of s0 into a0
jal wrtln_int # branch to wrtln_int......................2
jr $ra # Return to caller......7




wrtln_int:

subu $sp,$sp,24 # Stack frame is 24 bytes long
sw $ra,20($sp) # Save return address
sw $fp,16($sp) # Save callerís frame pointer
addu $fp,$sp,20 # Set up frame pointer

li $v0, 1 #syscall to print
syscall # print
jal wrtln # Branch to wrtln...................................................2


lw $ra,20($sp) # Restore return address
lw $fp,16($sp) # Restore callerís frame pointer
addu $sp,$sp,24 # Pop stack frame
jr $ra # Return to caller..........6

wrtln:

li $v0, 4 # system call code for print_str
la $a0, str_1 # address of string to print
syscall # print the string
jal wrtlnback # branch to wrtlnback...............................................3
jr $ra # Return to caller..........5


wrtlnback:
addi $s1, $s1, 4 #add 4 to $s1 and store in $s1
addi $s0, $s0, 1 #add 1 to $s0 and save in $s0
bne $s0, $s2, loop #branch to loop if $s0 does not equal $s2


lw $s0, 24($sp)
lw $s1, 28($sp)
lw $ra,20($sp) # Restore return address
lw $fp,16($sp) # Restore callerís frame pointer

addu $sp,$sp,24 # Pop stack frame
jr $ra # Return to caller.................................4



main:

subu $sp,$sp,24 # Stack frame is 24 bytes long
sw $ra,20($sp) # Save return address
sw $fp,16($sp) # Save callerís frame pointer
addu $fp,$sp,20 # Set up frame pointer

la $a0, randnum # load randnum address into a0
li $a1, 9 # load first integer into a1
jal wrt_array # call jump and link to write line..............1

lw $ra,20($sp) # Restore return address
lw $fp,16($sp) # Restore callerís frame pointer
addu $sp,$sp,24 # Pop stack frame
jr $ra # Return to caller...........8

Is This A Good Question/Topic? 0
  • +

Page 1 of 1