簡體   English   中英

當我在數據段中設置其值時,不會讀取mips中的字符串輸入

[英]String input in mips is not read as the way I read it when I set its value in data segment

.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    

上面的程序是程序的一部分,允許用戶輸入一些base(2-16)並將其轉換為另一個base(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時,我無法再使用上述方式逐字節訪問String($ a0不會返回正確的值)。

請問為什么以及如何解決此問題?

您的問題是用戶輸入的字符串包含換行ASCII字符(0xA)。

因此,您應該在發出該行的行中減去2而不是1

   sub     $t2, $t2, 1     #adjust t2

例如:

   sub     $t2, $t2, 2     #adjust t2 taking into account line feed character in user input

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM