Programming - Assembly Input/Output Calls

in #programming7 years ago (edited)

    Hello again, in today's post I will talk about System calls and how we can use them to get input from the user, print into the Console (output) and even terminate I programm. I already gave you Code for the same stuff last time, but this time you will learn exactly how it works and I will give you another Code also! So, without further do, let's get started!


System Calls:

   System Calls are requests our programm does to the kernel of the operating system, when it wants to do something. This something can be: hardware-related stuff ( like accessing hard drives, getting keyboard input or printing into console ), creation and execution of processes or threads ( we will talk about all that more in Linux in some weeks ) etc. In MIPS Assembly we have to use some so called System Call Codes that will tell the system which operation we want to do.

This Codes are:

  • 1, for printing integer
  • 2, for printing float
  • 3, for printing double
  • 4, for printing string
  • 5, for reading integer
  • 6, for reading float
  • 7, for reading double
  • 8, for reading string
  • 9, for dynamic memory allocation
  • 10, for terminating programm

           and even more...

Execution of an System Call:

    To execute a system call we have to use the syscall instruction after setting up some registers. We have to put the integer value of our system call inside of the $v0 register using the li instruction like this: li $v0, value. When printing we have to set the argument inside of the $a0 or $f12 register using move ( integer/string output or float/double ) in the same way like that: move $a0, address/register. When getting input, our input will be stored inside of the $v0 or $f0 register (integer/string or float/double) and we have to move it again to another register for saving it.

So, if we want to get an input from the user and print it right back we could use the following Code:

# get integer input
li $v0, 5
syscall
# move it to register $t0
move $t0, $v0
# print integer
li $v0, 1
move $a0, $t0
syscall


Coding Section:

    Here some Codes for you, to understand some stuff better, now that I showed you how it works. First comes the Code from the last post and then I have another one for you!

Code 1:

.data
# Constant strings to be output to the terminal
promptInt: .asciiz "Please input an integer: "
resultInt: .asciiz "Next integer is: "
linefeed: .asciiz "\n"
enterkey: .asciiz "Press any key to end program."

.text
main:
# prompt for an integer
li $v0,4 # code for print_string
la $a0,promptInt # point $a0 to prompt string
syscall # print the prompt

# get an integer from the user
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
move $t0,$v0 # move the resulting int to $t0

# compute the next integer
addi $t0, $t0, 1 # t0 <-- t0 + 1

# print out text for the result
li $v0,4 #code for print_string
la $a0,resultInt # point $a0 to result string
syscall # print the result string

# print out the result
li $v0,1 # code for print_int
move $a0,$t0 # put result in $a0
syscall # print out the result

# print out a line feed
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed

# wait for the enter key to be pressed to end program
li $v0,4 # code for print_string
la $a0,enterkey # point $a0 to enterkey string
syscall # print enterkey

# wait for input by getting an integer from the user
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0

# All done, thank you!
li $v0,10 # code for exit
syscall # exit program


Code 2:

.data
# Constant strings to be output to the terminal
givex: .asciiz "x: "
givey: .asciiz "y: "
space: .asciiz " "
linefeed: .asciiz "\n"
end: .asciiz "End of Program!"

.text
main:
# print message for getting x
li $v0,4 # code for print_string
la $a0,givex # point $a0 to prompt string
syscall # print the prompt

# get x as input
li $v0,5 # code for read_int
syscall #get int from user --> returned in $v0
move $t0,$v0 # move the resulting int to $t0

# print message for getting y
li $v0,4 # code for print_string
la $a0,givey # point $a0 to prompt string
syscall # print the prompt

# get y as input
li $v0,5 # code for read_int
syscall # get int from user --> returned in $v0
move $t1,$v0 # move the resulting int to $t1

# print new line
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed

#swap x and y values
move $s0,$t1 # move the resulting int to $s0
move $t1,$t0 # move the resulting int to $t1
move $t0,$s0 # move the resulting int to $t0

# print message before printing x
li $v0,4 # code for print_string
la $a0,givex # point $a0 to prompt string
syscall # print the prompt

# print new swapped x value
li $v0,1 # code for print_int
move $a0,$t0 # put result in $a0
syscall # print out the result

# print space
li $v0,4 # code for print_string
la $a0,space # point $a0 to prompt string
syscall # print the prompt

# print message before printing y
li $v0,4 # code for print_string
la $a0,givey # point $a0 to prompt string
syscall # print the prompt

# print new swapped y value
li $v0,1 # code for print_int
move $a0,$t1 # put result in $a0
syscall # print out the result

# print new line
li $v0,4 # code for print_string
la $a0,linefeed # point $a0 to linefeed string
syscall # print linefeed

# print programm end message
li $v0,4 # code for print_string
la $a0,end # point $a0 to end string
syscall # print end

# terminate program
li $v0,10 # code for exit
syscall # exit program


This was the end of today's post, hope you enjoyed it :) 

Next time we will get into Branches and Jumps that will help us create if/else statements and for/while loops.

Until next time, bye!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64383.21
ETH 3098.60
USDT 1.00
SBD 3.89