簡體   English   中英

我的MIPS代碼中的錯誤在哪里?

[英]where's the bug in my MIPS code?

我需要幫助。 我已經在MARS中編寫了此代碼。 它應該從用戶那里接收一個整數,並將其轉換為HEX。 我已經檢查了幾個小時,據我所知,它應該工作正常。 我只包括了程序的循環和輸出部分,因為這是唯一不起作用的部分。 有人可以指出代碼出了什么問題嗎? 謝謝。

PS:我認為逐位AND搞砸了,我用它掩蓋了低位,但是由於某種原因,它幾乎似乎是在添加而不是ANDing。 :-(

   li $s1, 8                    # Set up a loop counter

環:

   rol $s0, $s0, 4              # Roll the bits left by four bits - wraps highest bits to lowest bits (where we need them!)

   and $t0, $s0, 16           # Mask off low bits (logical AND with 000...01111)
   slti $t1, $t0, 10          # Determine if the bits represent a number less than 10 (slti)
   bne $t1, $zero, MakeLowDigit  # If yes (if lower than 9), go make a low digit

MakeHighDigit:

   subi $t0, $t0, 10            # Subtract 10 from low bits
   addi $t0, $t0, 64             # Add them to the code for 'A' (65), becomes a..f
   j DigitOut

MakeLowDigit:

   addi $t0, $t0, 47             # Combine it with ASCII code for '0', becomes 0..9 

DigitOut:

    move $a0, $t0               # Output the ASCII character
    li $v0, 11
    syscall

    subi $s1, $s1, 1            # Decrement loop counter
    bne $s1, $zero, Loop        # Keep looping if loop counter is not zero

您至少掩蓋了錯誤的常數:

and $t0, $s0, 16               # Mask off low bits (logical AND with 000...01111)

整數16位模式...010000 ,也就是說,它非常四個一結束。 您想要15,即16-1。

通常,要創建設置了n位的右調整位掩碼,您需要計算2 n -1,用C表示法是(1 << n) - 1

除了滾動錯誤的常量外,您的ASCII值設置不正確。 ASCII中的“ A”由數字65表示。在您的代碼中,要為滾動掩碼的結果添加64。 如果最后四位等於10(十進制),換句話說$ t0包含十進制10,則您的代碼將從$ t0中減去10。 因此,$ t0的十進制結果為0。向其添加64時,您的代碼將獲取@字符的ASCII值。

http://www.asciitable.com/

暫無
暫無

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

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