Assembly 如何在MIPS中设置指向asciiz字符串的指针
我试图将用户输入的asciiz字符串转换为整数。这是我目前掌握的代码。如果我硬编码一个字符串,然后使用命令la$s1,hardCodedString,它就可以完美地工作。但是,当我尝试获取用户输入时,没有打印任何内容。是不是因为la不是正确的命令 我应该如何在$s1中存储指向当前$a0中输入的asciiz字符串的指针Assembly 如何在MIPS中设置指向asciiz字符串的指针,assembly,input,mips,Assembly,Input,Mips,我试图将用户输入的asciiz字符串转换为整数。这是我目前掌握的代码。如果我硬编码一个字符串,然后使用命令la$s1,hardCodedString,它就可以完美地工作。但是,当我尝试获取用户输入时,没有打印任何内容。是不是因为la不是正确的命令 我应该如何在$s1中存储指向当前$a0中输入的asciiz字符串的指针 .data theStr: .asciiz "Enter whatever it is: \n" errorMessage1: .asciiz "Erroooor1\n" e
.data
theStr: .asciiz "Enter whatever it is: \n"
errorMessage1: .asciiz "Erroooor1\n"
errorMessage2: .asciiz "Erroooor2\n"
num: .asciiz "123"
input1: .space 10
.text
main:
la $a0, theStr #prints Enter
li $v0, 4
syscall
li $v0, 8 #read a string into a0
la $a0, input1
syscall
move $s1, $a0 #HERE IS THE LINE IN QUESTION
li $t0, 10
li $s2,0
lp:
lbu $t1, ($s1) #load unsigned char from array into t1
beq $t1, $0, FIN #NULL terminator found
blt $t1, 48, error #check if char is not a digit (ascii<'0')
bgt $t1, 57, error #check if char is not a digit (ascii>'9')
addi $t1, $t1, -48 #converts t1's ascii value to dec value
mul $s2, $s2, $t0 #sum *= 10
add $s2, $s2, $t1 #sum += array[s1]-'0'
addi $s1, $s1, 1 #increment array address
j lp #jump to start of loop
FIN:
move $a0, $s2
li $v0, 1
syscall
.data
theStr:.asciiz“输入任何内容:\n”
errorMessage1:.asciiz“errooor1\n”
errorMessage2:.asciiz“errooor2\n”
编号:.asciiz“123”
输入1:。空格10
文本
主要内容:
la$a0,请输入STR
李$v0,4
系统调用
li$v0,8#将字符串读入a0
la$a0,输入1
系统调用
移动$s1,$a0#这是有问题的行
李:10美元
李2,0美元
lp:
lbu$t1,($s1)#将无符号字符从数组加载到t1中
beq$t1,$0,FIN#找到空终止符
blt$t1,48,错误#检查字符是否不是数字(ascii'9')
addi$t1,$t1,-48#将t1的ascii值转换为dec值
mul$s2,$s2,$t0#总和*=10
添加$s2、$s2、$t1#sum+=数组[s1]-“0”
addi$s1,$s1,1#增量数组地址
j lp#跳转到循环起点
鳍:
移动$a0,$s2
李$v0,1
系统调用
假设系统调用没有更改,$a0
该指令正常。我已经在spim中进行了测试,并且它保持不变。否则,只需通过la$s1,input1
重新加载即可。还要注意的是,输入字符串中会有换行符,因此在到达NULL终止符之前(如果输入系统调用甚至将其放入缓冲区),代码将分支到error
标签。我假设您未显示的错误
标签将跳过打印。您应该始终显示所有代码。在系统调用8之前,您没有将$a1设置为要读取的字符数。您应该将立即数加载到$a1,以便让编译器知道要读取的字符数。
像这样:
la $a0, input1
li $a1, 4 #The compiler will take the first 3 characters as input
li $v0, 8 #read a string into a0
syscall