Programming - Assembly File Input/Output Calls

    Hello guys and girls. It's me again. Today we will talk about some other System Calls. Mostly about File Input/Output Calls, but I will also show you some others in a small list, that may come handy sometimes. So, let's get started! 


File System Calls:

    There are 4 System Calls in MIPS that let us create/open a file, read from a file, write into a file and close a file. Let's take a look at all of them one by one.

Open a File:

    To open a file we use system call 13 with parameters the file name, flags and the mode (almost the same way we did it in C). 

So, our Code looks like this:

li $v0, 13 # open file code
la $a0, file_name # having file_name be string in .data
li $a1, 1 # flags are 0: reading, 1: writing
li $a2, 0 # mode is ignored (don't got into more myself)
syscall

# $v0 then contains the file descriptor (negative when error occured)


Read from File:

    To read from a file we use system call 14 with parameters the file descriptor returned from open file, the address we want to store the readed value, the number of bytes/characters to read.

So, our Code looks like this:

li $v0, 14 # read from file code
move $a0, $r1 # having in $r1 the file descriptor stored
la $a1, buffer # having buffer be string in .data
li $a2, 40 # hardcoded buffer length
syscall


Write into File:

        To write into a file we use system call 15 with parameters the file descriptor returned from open file, the output buffer address, the number of bytes/characters to write.

So, our Code looks like this:

li $v0, 15 # write into file code
move $a0, $r1 # having in $r1 the file descriptor stored
la $a1, buffer # having buffer be string in .data
li $a2, 40 # hardcoded buffer length
syscall


Close a File:

   To close a file we use system call 16 with parameter only the file descriptor. So, our Code looks like this:

li $v0, 16 # close file code
move $a0, $r1 # having in $r1 the file descriptor stored
syscall


Full Example Code:

.data
file_name: .asciiz "file.txt"
buffer: .asciiz "This is a line of text.\n"
buffer2: .asciiz "This is another line of text."

.text
main:
# open/create file for writing
li $v0, 13
la $a0, file_name
li $a1, 1
li $a2, 0
syscall
# save file descriptor in $s0
move $s0, $v0 

# write buffer into file
li $v0, 15
move $a0, $s0
la $a1, buffer
li $a2, 25
syscall

# write buffer2 into file
li $v0, 15
move $a0, $s0
la $a1, buffer2
li $a2, 29
syscall

# close file
li $v0, 16
move $a0, $s0
syscall

# terminate programm
li $v0, 10
syscall

Other System Calls:

   Some other pretty useful System Calls I don't needed to use in our little series are:

  • System Call 11, that prints a character
  • System Call 12, that reads a character
  • System Call 17, that terminates program with specific return value

    Those work with the Simulator we use (it depends on the QtSpim version tho). There are many others that you can check here that work in the MARS MIPS Simulator and let us do even more stuff.


This is the end of today's post. Hope you enjoyed it and learned something new!

    This is also the last "basic" Assembly post I will upload. I will continue posting more advanced Assembly stuff from time to time, and maybe we will start some series or something like that to write more difficult programs that take time. 

    After Assembly I will now start out with Logic Design. We will talk about Logical Circuits starting from basic Theory Knowledge stuff, continuing with basic and advanced Circuit simulation using MultiSim and afterwards we will get into the VHDL Programming Language, that let's us write Logical Circuits with Code!

Bye!

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now