2017-03-09 119 views
1

所以我的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 

我對壞格式表示歉意 - 這是我問過的第一個問題。謝謝!

回答

0

您的實際問題在代碼中更加遠了(例如,當您第一次使用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 
+0

非常感謝!第二次體重不足是我的疏忽 - 我甚至沒有注意到它。 –

相關問題