[英]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 count但要計數什么? 計數的位數?
我剛剛解決了這是一個答案
#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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.