如何使用用户输入与组件来创建一个文件 [英] How to create a file using user input with assembly
问题描述
我想在一个文件中记录一个用户输入,采用汇编。
我这个code,但在创建文件时,输入不正确记录在文件上工作。有人可以帮助我?这里是我的code:
。数据文件1:.asciizFILE1.TXT
提示:.asciiz用户输入\\ n
缓冲区:45。空间。文本 LA $ A0,提示
李$ v0,4
系统调用 李$ V0,8
李$ A1,454
系统调用
此举$ S1,$ V0
ĴwriteFile1writeFile1:
李$ V0,13
LA $ A0,文件1
李$ A1,1
李$ A2,0
系统调用
此举$ S6,$ V0 #写
李$ V0,15
此举$ A0,$ S6
LA $ A1,缓冲
李$ A2,45
系统调用 #关
李$ V0,16
此举$ A0,$ S6
系统调用
Ĵ退出退出:李$ V0,10
系统调用
您用户输入电话做的不的设置指针缓存
。因此,它会读入提示
来代替。此外,给定长度 454
而不是[意] 45
。此外,该系统调用确实的不的返回的长度,因此节省了 V0
什么也没做。
固定上述程序后的作品。但是,它会写一个固定长度的输出所以在结尾是二进制0。
我添加了一些code来计算字符串的长度(例如像的strlen
)。我还添加了侧边栏意见大部分线路。我强烈推荐这个任何ASM。总之,这里的修正程序[请原谅无偿风格清理]:
。数据文件1:.asciizFILE1.TXT
提示:.asciiz用户输入\\ n
缓冲区:46。空间
.eqv BUFMAX 45#可用的缓冲区长度(少) 。文本 # 用户提示
LA $ A0,提示#提示字符串
李$#v0,4把系统调用号
系统调用 #读取用户字符串
李$ v0,8
LA $ A0,缓冲区#FIXME - 这是失踪
李$ A1,BUFMAX#FIXME - 这是454
系统调用
###招$ S1,$#V0 FIXME - 什么也不做V0不长 ###李$ S1,BUFMAX#使用固定长度的
###ĴwriteFile1#会做在文件中写入零 #计算字符串长度(例如像strlen的)
此举$ S1,$ A0#点缓冲启动
getlen:
磅$ t0,0($ S1)#得到字节 - 是零?
阿迪$ S1,$#S1,1提前指针
BNE $ T0,$零,getlen#不,环
子$ S1,S1 $,$ A0#摆脱结束指针长度
SUBI $ S1,$#S1,1补偿preincrementwriteFile1:
#打开输出文件
李$#v0,13开放系统调用号
LA $ A0,文件1#文件名
李$#A1,1开写作
李$#a2,0开放模式
系统调用
此举$ S6,$#V0菲尔德斯保存数 #写
李$#v0,15写入系统调用号
此举$ A0,$#S6菲尔德斯获取数
LA $ A1,缓冲区#得到缓冲区指针
此举$ A2,$#S1 GET缓冲区长度
系统调用 # 关
李$ v0,16
此举$ A0,$#S6菲尔德斯获取数
系统调用
Ĵ退出出口:
李$#v0,10退出系统调用号
系统调用
I'm trying to record in a file an user input, using assembly.
I'm working with this code but when the file is created, the input isn't recorded on the file correctly. Someone can help me with this? Here is my code:
.data
file1: .asciiz "file1.txt"
prompt: .asciiz "User entry\n"
buffer: .space 45
.text
la $a0,prompt
li $v0,4
syscall
li $v0, 8
li $a1, 454
syscall
move $s1, $v0
j writeFile1
writeFile1:
li $v0, 13
la $a0, file1
li $a1, 1
li $a2, 0
syscall
move $s6, $v0
#write
li $v0, 15
move $a0, $s6
la $a1, buffer
li $a2, 45
syscall
#close
li $v0, 16
move $a0, $s6
syscall
j exit
exit: li $v0, 10
syscall
Your user input call did not setup the pointer to buffer
. So, it would read into prompt
instead. Also, the given length was 454
instead of the [intended] 45
. Further, this syscall does not return a length, so saving v0
did nothing.
After fixing the above the program works. But, it would write a fixed length output so there were binary zeroes at the end.
I've added some code to calculate the string length (e.g. like strlen
). I've also added sidebar comments to most lines. I highly recommend this for any asm. Anyway, here's the corrected program [please pardon the gratuitous style cleanup]:
.data
file1: .asciiz "file1.txt"
prompt: .asciiz "User entry\n"
buffer: .space 46
.eqv BUFMAX 45 # usable buffer length (one less)
.text
# prompt user
la $a0,prompt # prompt string
li $v0,4 # puts syscall number
syscall
# read user string
li $v0,8
la $a0,buffer # FIXME -- this was missing
li $a1,BUFMAX # FIXME -- this was 454
syscall
###move $s1,$v0 # FIXME -- does nothing v0 is not length
###li $s1,BUFMAX # use fixed length
###j writeFile1 # would do write with zeroes in file
# calculate string length (e.g. like strlen)
move $s1,$a0 # point to buffer start
getlen:
lb $t0,0($s1) # get byte -- is it zero?
addi $s1,$s1,1 # advance pointer
bne $t0,$zero,getlen # no, loop
sub $s1,$s1,$a0 # get length from end pointer
subi $s1,$s1,1 # compensate for preincrement
writeFile1:
# open the output file
li $v0,13 # open syscall number
la $a0,file1 # filename
li $a1,1 # open for writing
li $a2,0 # open mode
syscall
move $s6,$v0 # save fildes number
# write
li $v0,15 # write syscall number
move $a0,$s6 # get fildes number
la $a1,buffer # get buffer pointer
move $a2,$s1 # get buffer length
syscall
# close
li $v0,16
move $a0,$s6 # get fildes number
syscall
j exit
exit:
li $v0,10 # exit syscall number
syscall
这篇关于如何使用用户输入与组件来创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!