I have to do an assignment for class, I have it all complete as far as the math needed but i need to output to a file and can't seem to figure it out.

I have to print out prime numbers and I'm trying to follow the example on this page:
http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html

It tells you how to output characters (string) to a file, but when I try to output an integer, I get

Runtime exception at 0x00400034: address out of range 0x00000003

I am using the MARS MIPS simulator. Here is the code example from the page I linked; instead of the 'quick brown fox' sentence, I am trying to print out the integer 3.

    .data
fout:   .asciiz "testout.txt"      # filename for output
buffer: .asciiz "The quick brown fox jumps over the lazy dog."
    .text

li $t0, 1
li $t1, 2
add $t3, $t0, $t1
move $t0, $t3


###############################################################
# Open (for writing) a file that does not exist
li   $v0, 13       # system call for open file
la   $a0, fout     # output file name
li   $a1, 1        # Open for writing (flags are 0: read, 1: write)
li   $a2, 0        # mode is ignored
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 
###############################################################
# Write to file just opened
li   $v0, 15       # system call for write to file
move $a0, $s6      # file descriptor 
lb   $a1, ($t0)   # address of buffer from which to write
li   $a2, 44       # hardcoded buffer length
syscall            # write to file
###############################################################
# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file
###############################################################

Also I tied to be brave and do an assembly version of itoa but I get the same error at RUNTIME.

 ItoA:
 ddi $t0, $zero, 10
 div $a0, $t0       # $a0/10
 mflo $a1           # $a1 = quotient
 addi $a1, $a1, 0x30    # convert to ASCII
 mfhi $t0           # $t0 = remainder
 addi $t0, $t0, 0x30    # convert to ASCII
 sll $t0, $t0, 8
 or $a3, $a1, $t0
 jr $ra
 EndItoA:

This is the first MIPS assembly language assignment that I have had and any help is appreciated.

Thanks in advance.

The problem is actually two-fold: first, you are loading a value where you need an address; and second, you are trying to write out an integer with a function which expects a character buffer. The solution to the first problem is to change the code so that it puts the value into a character buffer, then gets the address of said buffer for the syscall argument:

    .data
fout:   .asciiz "testout.txt"      # filename for output
buffer: .resb 33                   # set aside a space of 32 characters

    .text

li $t0, 1
li $t1, 2
add $t3, $t0, $t1
la $t0, buffer
sb $t3, ($t0)

###############################################################
# Open (for writing) a file that does not exist
li   $v0, 13       # system call for open file
la   $a0, fout     # output file name
li   $a1, 1        # Open for writing (flags are 0: read, 1: write)
li   $a2, 0        # mode is ignored
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 
###############################################################
# Write to file just opened
li   $v0, 15       # system call for write to file
move $a0, $s6      # file descriptor 
move $a1, $t0      # address of buffer from which to write
li   $a2, 44       # hardcoded buffer length
syscall            # write to file
###############################################################
# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file
###############################################################

This still leaves you with the problem that you are riting out the binary value rather than its ASCII representation. This is where your itoa() function comes in, and I will leave it to you to fix (I'm short on time right now, sorry - I might comer back to this later if I have the chance to).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.