LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2017, 04:17 PM   #1
Vido
Member
 
Registered: Mar 2014
Posts: 38

Rep: Reputation: Disabled
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
but Im getting SIGSEGV, Segmentation fault.

Last edited by Vido; 03-29-2017 at 09:14 AM.
 
Old 03-29-2017, 10:50 AM   #2
Vido
Member
 
Registered: Mar 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-29-2017, 06:42 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
How about write the thing as a one-liner in "C," and grab the assembly output from the compiler?
 
Old 03-30-2017, 10:29 AM   #4
Vido
Member
 
Registered: Mar 2014
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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.

Last edited by Vido; 03-30-2017 at 10:39 AM.
 
  


Reply

Tags
arguments, assembly-language



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
detour in mips assembly ineya Programming 2 05-15-2010 04:56 PM
MIPS: assembly language query ashlesha Linux - Newbie 2 09-11-2006 11:05 PM
generating mips assembly from c code with gcc? lilili Programming 4 05-11-2006 08:37 PM
MIPS assembly question Gnute Programming 1 08-24-2004 05:33 PM
MIPS assembly fenriswolf Programming 0 10-17-2001 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration