我有这份作业,它必须执行以下所有操作:
我必须:编写一个MIPS程序,该程序从用户输入中读取最多4个字符的字符串。该程序必须使用循环来处理用户输入。该程序不得包含子程序。然后,假定用户输入的基数为33。将其转换为十进制整数。请帮我。我的代码中出现错误。另外,我不知道如何将字符串转换为整数,然后再转换为基数10。我以为我做对了,但是现在很困惑。
我写了我的代码,但是似乎不起作用。
这是我的代码:
.data
Ask:
.asciiz "\n Please Enter 4 Characters\n"
userInput: .space 20
Answer:
.asciiz "\n You Entered : \n"
.text
main:
#addi $t0, 0
li $v0, 4
la $a0, Ask #display question
syscall
li $v0, 8 #get input
la $a0, userInput
li $a1, 20
syscall
li $v0, 4
la $a0, Answer
syscall
li $v0, 4
la $a0, userInput
syscall
lb $s1, 1($a0) #last digit
lb $s2 2($a0) #third
lb $s3 3($a0) #second digit
lb $s4, 4($a0) #first digit
lb $t0, 0($a0) #checks for \n
addi $t1, $zero, 10 #takes in \n
addi $t7, $zero, 33 #gets value to multiply
addi $s0, $zero, 1089 #gets 33^2
addi $t5, $zero, 97 #gets 64, smaller than ascii code for 'A'
addi $t6, $zero 96 #gets 96, smaller than ascii code for 'a'
addi $t2, $zero, 65 #loads 47 into $t2, smaller than the ascii code for '0'
#addi $t3, $zero, 55
beq $t0, $t1, Label1
Label1:
blt $s1, $t2, L1
blt $s1, $t5, L2
bgt $s1, $t6, L3
L1:
addi $s1, $s1, -48 #subtracts 48 from $s1 which is 48 to get int from 0 to 9
L2:
addi $s1, $s1, -55 #gets values from 10 to 33
L3:
addi $s1, $s1, -87 #gets values from 10 to 33 for small caps
blt $s2, $t2, L4
blt $s2, $t5, L5
bge $s2, $t6, L6
L4:
addi $s2, $s2, -48
L5:
addi $s2, $s2, -55
L6:
addi $s2, $s2, -87
blt $s3, $t2, L7
blt $s3, $t5, L8
bgt $s3, $t6, L9
L7: addi $s3, $s3, -48
L8: addi $s3, $s3, -55
L9: addi $s3, $s3, -87
blt $s4, $t2, L10
blt $s4, $t5, L11
bgt $s4, $t6, L12
L10:
addi $s4, $s4, -48
L11:
addi $s4, $s4, -55
L12:
addi $s4, $s4 , -87
syscall
add $s5, $zero, 0
add $s5,$s5, $s1
mult $s2, $t7
mflo $s2
add $s5, $s5, $s2
mult $s3, $s0
mflo $s3
add $s5, $s5, $s3
mult $s0, $t7
mflo $s0
mult $s4, $s0
mflo $s4
mfhi $s0
add $s5, $s5, $s4
add $s5, $s5, $s0
syscall
#li $v0, 1
#la $a0, $s5
#la $v0, 4
#lw $a0, s4
#last system call of the program will be very last instruction
li $v0, 10
syscall
答案 0 :(得分:2)
对于初学者:
lb $s1, 1($a0) #last digit
lb $s2 2($a0) #third
lb $s3 3($a0) #second digit
lb $s4, 4($a0) #first digit
lb $t0, 0($a0) #checks for \n
您在a0中有一个4字符串-假设它是ABCD类型
所以:
0($a0) will be A
1($a0) will be B
2($a0) will be C
3($a0) will be D
接下来假设s1为'A'
Label1:
blt $s1, $t2, L1 # 65
blt $s1, $t5, L2 # 97
bgt $s1, $t6, L3 # 96
L1:
addi $s1, $s1, -48 #subtracts 48 from $s1 which is 48 to get int from 0 to 9
L2:
addi $s1, $s1, -55 #gets values from 10 to 33
L3:
addi $s1, $s1, -87 #gets values from 10 to 33 for small caps
'A'是65,因此将跳到L2,减去-55,而不是10,然后落入L3,然后减去87得出-77。
如果它是'9'(57)而不是'A',它将转到L1,减去48,下降到L2,减去55,下降到L3,减去87。
因此,对于每个字母,请检查,类似的事情应该发生:
L1:
addi $s1, $s1, -48 #subtracts 48 from $s1 which is 48 to get int from 0 to 9
j DoneL13
L2:
addi $s1, $s1, -55 #gets values from 10 to 33
j DoneL13
L3:
addi $s1, $s1, -87 #gets values from 10 to 33 for small caps
DoneL13:
最后,您有一些sys调用-不确定它们的含义是什么,但是您猜测它神奇地意味着要知道您要在s5中打印该值吗?