.data
val: .space 64
ask1: .asciiz "\nEnter input number base(2-16): "
ask2: .asciiz "\nEnter a value of that base: "
ask3: .asciiz "\nEnter output number base(2-16): "
result: .asciiz "\nThe result is: "
line: .asciiz "\n"
.text
.globl main
main:
la $a0, ask1 #ask for input number base
li $v0, 4
syscall
li $v0, 5 # syscall 5 to read the int, store in $v0
syscall # actually read the int
move $t1, $v0 # store input(number base) in $t1.
la $a0, ask2 #ask for input value
li $v0, 4
syscall
li $v0, 8 #read the input value
la $a0, val
la $a1, 64
syscall
li $v0, 4
syscall
la $a0, ask3 #ask for output number base
li $v0, 4
syscall
li $v0, 5 # syscall 5 to read the int, store in $v0
syscall # actually read the int
move $t5, $v0 # store output(number base) in $t5.
li $t2,0 #counter
li $t3,0 #sum
li $t4,1 #power
li $s1,65 #asciiz code of "A"
li $s2,10 #for converting int to string in getStrVal
#t5=o.base, t1=i.base
getLength: #getting length of string
lb $t0, val($t2)
add $t2, $t2, 1 #t2 = length of string
bne $t0, $zero, getLength
sub $t2, $t2, 1 #adjust t2
#get lowest place value
sub $t2, $t2, 1 #counter--
la $t0, val($t2) #load address
lb $a0, ($t0) #load character to a0
上面的程序是程序的一部分,让用户输入一些基数(2-16)将其转换为另一个基数(2-16)。
当我编程时,我将val设置为类似于val:.asciiz" A123"。
当我像这样访问它时,它运作良好
#get lowest place value
sub $t2, $t2, 1 #counter--
la $t0, val($t2) #load address
lb $a0, ($t0) #load character to a0
但是,当我更改它以要求用户将字符串输入到val时,我不能再使用上面的方法逐字节地访问字符串($ a0不返回正确的值)。
我可以问为什么以及如何解决这个问题?
答案 0 :(得分:1)
您的问题是用户输入的字符串包含换行符ASCII字符(0xA)。
因此,您应该在发布
的行中减去2而不是1 sub $t2, $t2, 1 #adjust t2
e.g:
sub $t2, $t2, 2 #adjust t2 taking into account line feed character in user input