从用户那里读取输入并将其打印回 MIPS 程序集中的控制台
read input from user and print it back on console in MIPS assembly
我正在尝试制作一个简单的程序,它将读取用户的输入并将其打印回控制台,这是我的程序的一部分
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
#Raad the integer and save it in s0
li a0,0 # first argument for stdin is 0
li a1,unknown # second argument is unknown
li a2,unknown # third arg is also unknown
li v0,__NR_read
syscall
move s0, v0
所以我的程序等待用户输入,在输入 1 或 9 后,v0 和 S0 的值(用 gdb 检查)分别是 0x2 而不是 0x1 和 0x9。
编辑:
所以我刚找到这个
sys_read(unsigned int fd, char __user *buf, size_t count);
所以第二个参数是 __user *buf 所以它指向 *buf 那么这是我应该存储它的地方吗?
第三个参数是 size_t 计数,但计数是为了什么?字节数 ?
我刚刚解决了这里是答案
#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
.data
var1: .space 256
我正在尝试制作一个简单的程序,它将读取用户的输入并将其打印回控制台,这是我的程序的一部分
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
#Raad the integer and save it in s0
li a0,0 # first argument for stdin is 0
li a1,unknown # second argument is unknown
li a2,unknown # third arg is also unknown
li v0,__NR_read
syscall
move s0, v0
所以我的程序等待用户输入,在输入 1 或 9 后,v0 和 S0 的值(用 gdb 检查)分别是 0x2 而不是 0x1 和 0x9。
编辑: 所以我刚找到这个
sys_read(unsigned int fd, char __user *buf, size_t count);
所以第二个参数是 __user *buf 所以它指向 *buf 那么这是我应该存储它的地方吗? 第三个参数是 size_t 计数,但计数是为了什么?字节数 ?
我刚刚解决了这里是答案
#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
.data
var1: .space 256