Saturday, February 15, 2020

Open, write, close file in MIPS

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ------------------------------------------------------- #

.data
str_exit:  .asciiz "write_file_test.txt"
str_data:  .ascii "This is a test!\nSecond line\n"
str_data_end:

# ------------------------------------------------------- #
.text

file_open:
    li $v0, 13
    la $a0, str_exit
    li $a1, 1
    li $a2, 0
    syscall            # File descriptor is returned in $v0

file_write:
    move $a0, $v0      # Syscall 15 (file descriptor in $a0)
    li $v0, 15
    la $a1, str_data
    la $a2, str_data_end
    la $a3, str_data
    subu $a2, $a2, $a3 # computes the length of the string
    syscall

file_close:
    li $v0, 16         # $a0 already has the file descriptor
    syscall

# ------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment