MIPS字符串第一个字符没有显示
问题描述:
所以我的MARS MIPS模拟器发生了一个奇怪的问题。MIPS字符串第一个字符没有显示
我的字符串采用用户的名字看起来是删除他们的名字的第一个字符。我无法弄清楚为什么我的生活。
.data
height: .asciiz "enter your height"
weight: .asciiz "enter your weight"
little: .float 18.5
medium: .float 25
large: .float 30
fpconstant: .float 703
bmi: .float
under: .asciiz "This is considered underweight"
health: .asciiz "This is considered healthy"
overweight: .asciiz "This is considered overweight"
obese: .asciiz "This is considered obese."
name: .asciiz ", here are your BMI results\n"
prompt: .asciiz "Please enter your name"
buffer: .space 20
.text
main: li $v0, 4
la $a0, prompt
syscall
li $v0, 8
syscall
la $a0, buffer # load byte space into address
li $a1, 20 # allot the byte space for string
move $t6, $a0 # save string to t6
syscall
li $v0, 4
la $a0, weight
syscall
li $v0, 6
syscall
movf.s $f1, $f0 #f1 now contains weight, will continue after this correction has been made.
l.s $f2, fpconstant
mul.s $f0, $f1, $f2
movf.s $f1, $f0
movf.s $f12, $f0
li $v0, 4
la $a0, height
syscall
li $v0, 6
syscall
movf.s $f2, $f0 #f2 now contains height, will continue after this correction has been made.
mul.s $f0, $f2, $f2
movf.s $f2, $f0
movf.s $f12, $f0
div.s $f3, $f1, $f2 #f3 is our BMI
movf.s $f12, $f3
la $a0, buffer # reload byte space to primary address
move $a0, $t6 # primary address = t6 address (load pointer)
li $v0, 4 # print string
syscall #Problem
la $a0, name
move $t0, $a0
syscall
li $v0, 2
syscall
l.s $f4, little
c.lt.s $f3,$f4
bc1t underweight #Branch if specified FP condition flag true (BC1T, not BCLT) : If Coprocessor 1 condition flag specified by immediate is true (one) then branch to statement at label's address # Compare less than single precision : If $f0 is less than $f1, set Coprocessor 1 condition flag 0 true else set it false
l.s $f4, medium
c.lt.s $f3,$f4
bc1t mid
l.s $f4, large
c.lt.s $f3,$f4
bc1t big
li $v0, 4
la $a0, obese
syscall
li $v0, 10
syscall
underweight:
li $v0, 4
la $a0, under
syscall
li $v0, 10
syscall
li $v0, 10
syscall
mid:
li $v0, 4
la $a0, health
syscall
li $v0, 10
syscall
big:
li $v0, 4
la $a0, overweight
syscall
li $v0, 10
syscall
我对坏格式表示歉意 - 这是我问过的第一个问题。谢谢!
答
您的实际问题在代码中更加远了(例如,当您第一次使用syscall 8
读取名称时)。该la $a0,buffer
被的syscall
(即漏洞)
后即将此外,它似乎你与一些syscall
有点宽松。 (例如underweight:
有两个syscall 10
但只有一个是必需的)。 我已经注释了你的代码并清理了一些注释,并修复了这个错误。我也重新格式化了它:
.data
height: .asciiz "enter your height: "
weight: .asciiz "enter your weight: "
little: .float 18.5
medium: .float 25
large: .float 30
fpconstant: .float 703
bmi: .float
under: .asciiz " This is considered underweight"
health: .asciiz " This is considered healthy"
overweight: .asciiz " This is considered overweight"
obese: .asciiz " This is considered obese."
name: .asciiz ", here are your BMI results\n"
prompt: .asciiz "Please enter your name: "
buffer: .space 20
.text
main:
li $v0,4
la $a0,prompt
syscall
j fix # skip over [broken] original code
# ------------------------------------------------------------------------
# NOTE/BUG: here is the original code
li $v0,8
syscall
la $a0,buffer # load byte space into address
li $a1,20 # allot the byte space for string
move $t6,$a0 # save string to t6
syscall
li $v0,8
syscall
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# NOTE/FIX: here is the fixed code
fix:
la $a0,buffer # get address of name buffer
li $a1,20 # allot the byte space for string
li $v0,8
syscall
move $t6,$a0 # save string to t6
# strip newline for pretty print
strip:
lbu $t0,0($a0) # get char
addiu $a0,$a0,1 # advance to next char
beqz $t0,strip_done # EOS? if yes, done
bne $t0,0x0A,strip # newline? if no, loop
sb $zero,-1($a0) # strip newline
strip_done:
# ------------------------------------------------------------------------
li $v0,4
la $a0,weight
syscall
li $v0,6
syscall
# f1 now contains weight, will continue after this correction has been made.
movf.s $f1,$f0
l.s $f2,fpconstant
mul.s $f0,$f1,$f2
movf.s $f1,$f0
movf.s $f12,$f0
li $v0,4
la $a0,height
syscall
li $v0,6
syscall
# f2 now contains height, will continue after this correction has been made.
movf.s $f2,$f0
mul.s $f0,$f2,$f2
movf.s $f2,$f0
movf.s $f12,$f0
div.s $f3,$f1,$f2 # f3 is our BMI
movf.s $f12,$f3
la $a0,buffer # reload byte space to primary address
move $a0,$t6 # primary address = t6 (load pointer)
li $v0,4 # print string
# NOTE: this is _not_ the true source of the problem -- see above
syscall # Problem
la $a0,name
move $t0,$a0
syscall
li $v0,2
syscall
l.s $f4,little
c.lt.s $f3,$f4
# Branch if specified FP condition flag true (BC1T, not BCLT) :
# If Coprocessor 1 condition flag specified by immediate is true (one) then
# branch to statement at label's address
# Compare less than single precision :
# If $f0 is less than $f1, set Coprocessor 1 condition flag 0 true
# else set it false
bc1t underweight
l.s $f4,medium
c.lt.s $f3,$f4
bc1t mid
l.s $f4,large
c.lt.s $f3,$f4
bc1t big
li $v0,4
la $a0,obese
j result
underweight:
la $a0,under
j result
mid:
la $a0,health
j result
big:
la $a0,overweight
j result
result:
li $v0,4
syscall
li $v0,10
syscall
非常感谢!第二次体重不足是我的疏忽 - 我甚至没有注意到它。 –