Read input from user and print it back on console in MIPS assembly
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Read input from user and print it back on console in MIPS assembly
Im trying to make simple program which will read input from user and print it back to console here is part of mine program
Code:
LEAF(main)
#Print to user enter integer
li a0,1 # first argument fd 1
la a1,prompt # second argument memory location of hello string
li a2,20 # lenght of string to print
li v0,__NR_write # syscall write,they are defined in unistd.h
syscall
#Read the integer and save it in s0
li v,__NR_read
syscall
move s0, v0
So mine program waits for input from user and after entering 1 or 9 the value of v0 and and S0 (inspected with gdb) are 0x2 not 0x1 and 0x9 respectively. So __NR_read require some paramiters in a a0,a1 .. registers. I found this so here are my questions
Code:
asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);
1. firs argument is file descriptor so for stdin fd is 0
2. second argument __char *buf so its pointer to a bufer ? If I place memory address is that correct ?
3. third argument site_t count so its number of how many byts ? what to put here ? 10 ?
EDIT:
So I did tried to place memory location of var1 as second argument like so
Code:
#Read the integer and save it in s0
nop
nop
li a0,0 # firs argument, for stdin is 0 ISTR, see "man 2 read"
la a1,(var1) # second argument load adress of var1 into a1
li a2,10 # third argument is count of byts
li v0,__NR_read
syscall
var1: .space 12
I loved it, I just have some leftowers which caused Segmentation fault, here is whole program
Code:
/*
* hello-1.2/Makefile
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (C) 1995, 1997 by Ralf Baechle
*/
#include <regdef.h>
#include <sys/asm.h>
#include <sys/syscall.h>
EXPORT(__start)
.set noreorder
LEAF(main)
#Prompt for the integer to enter
li a0,1 # first argument fd 1 for iutput
la a1,prompt # second argument memory location of hello string
li a2,20 # lenght of string to print
li v0,__NR_write # syscall write,they are defined in unistd.h
syscall
#Read the integer and save it in s0
nop
nop
li a0,0 # firs argument, for stdin is 0 ISTR, see "man 2 read"
la a1,(var1) # second argument load adress of var1 into a1
li a2,12 # third argument is count of byts
li v0,__NR_read
syscall
move t0, v0
#Print the message
li a0,1 # first argument fd 1
la a1,output # second argument memory location of hello string
li a2,256 # lenght of string to print
li v0,__NR_write # syscall write,they are defined in unistd.h
syscall
#Output the number
nop
nop
quit: # label quit
li v0,__NR_exit # load system call for exit in v0
syscall
nop
END(main)
.data
prompt: .asciiz "Enter an intriger: "
output: .asciiz "\nYou typed the number "
var1: .space 256
How about write the thing as a one-liner in "C," and grab the assembly output from the compiler?
I did tried that but not on one line and the problem is that gcc does not write registers as $t9 rather gcc write it as for example $25, and also I get stuff like
Code:
lui $28,%hi(__gnu_local_gp)
addiu $28,$28,%lo(__gnu_local_gp)
which I dont understand, so for first line it would be load upper immediate %hi would be register hi? and this thing (__gnu_local_gp) I dont know for what it stands for? Global Pointer ? And there is a lot things like so which is not match of help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.