2017-02-15 35 views
2

我是MIPS和這個網站的新手(這是我的第一篇文章),所以請在這裏忍受我...我必須輸入一個用戶輸入的數字,整數使用除法(HI/LO)整數,並將餘數存儲到新寄存器中,以便將結果反轉的數字與原始數據進行比較,以查看它是否是迴文。這很好,得到了這個部分(我認爲?),但是一旦我第二次分割並嘗試將第二個餘數加到第一個分部的剩餘部分上,它將簡單地覆蓋寄存器的內容,而不是在它的結尾,對吧?我無法在互聯網上找到答案。我怎樣才能做到這一點?因爲簡單地做'移動'會覆蓋內容,對嗎?這裏是我到目前爲止的代碼將整數移到新的寄存器而不覆蓋MIPS

li $v0, 4 # System call code for print string 
la $a0, Prompt # Load address for Prompt into a0 
syscall 

li $v0, 5 # System call code for read integer 
syscall  # Read the integer into v0 
move $t0, $v0 # Move the value into t0 

move $t9, $t0 

li $s0, 10 # Load 10 into s0 for division 
li $s1, 0 # Load 0 into s1 for division 

div  $t0, $s0 # Divides t0 by 10 

mfhi $t1  # Move remainder into t1 
mflo $t0  # Move quotient into t0 

從本質上講,我想的餘串連在一起,不在一起添加它們或覆蓋寄存器。假設第一個餘數是3,第二個是6,第三個是9.在結尾,我不希望它是18或9.我希望它是369.

回答

0

您的div/mfhi/mflo很好。你需要的是一個有第二個變量的循環。

我創建了等價的C代碼,並將其添加爲一個註釋塊,創造了一個工作計劃[請原諒無償風格清理]:

# int 
# rev(int inp) 
# { 
#  int acc; 
#  int dig; 
# 
#  acc = 0; 
# 
#  while (inp != 0) { 
#   dig = inp % 10; 
#   inp /= 10; 
# 
#   acc *= 10; 
#   acc += dig; 
#  } 
# 
#  return acc; 
# } 

    .data 
Prompt:  .asciiz  "Enter number to reverse:\n" 
nl:   .asciiz  "\n" 

    .text 
    .globl main 

main: 
    li  $v0,4     # System call code for print string 
    la  $a0,Prompt    # Load address for Prompt into a0 
    syscall 

    li  $v0,5     # System call code for read integer 
    syscall       # Read the integer into v0 
    bltz $v0,exit    # continue until stop requested 
    move $t0,$v0     # Move the value into t0 

    li  $t3,0     # initialize accumulator 

    li  $s0,10     # Load 10 into s0 for division 
    li  $s1,0     # Load 0 into s1 for division 

next_digit: 
    beqz $t0,print    # more to do? if no, fly 
    div  $t0,$s0     # Divides t0 by 10 

    mfhi $t1      # Move remainder into t1 (i.e. dig) 
    mflo $t0      # Move quotient into t0 (i.e. inp) 

    mul  $t3,$t3,$s0    # acc *= 10 
    add  $t3,$t3,$t1    # acc += dig 
    j  next_digit    # try for more 

print: 
    li  $v0,1     # print integer syscall 
    move $a0,$t3     # get value to print 
    syscall 

    li  $v0,4 
    la  $a0,nl 
    syscall 

    j  main 

exit: 
    li  $v0,10 
    syscall 
+0

對不起,我從來沒有迴應,多謝了幫幫我! – teddymv