2012-02-28 22 views
0

我以爲我有一個想法如何使用add,sub,mult,addi和or或andi,ori,lw,sw,beq,bne,slt,slti ,MFLO。在MIPS中添加所有高達10的數字

我必須確保每一個細節都在那裏,堆棧指針清晰等

任何幫助解決這個好嗎?不是爲了作業,只是爲了學習考試。我試圖解決它,並得到了錯誤,只是想看到一個正確的解決方案,所以我可以看到我做錯了什麼。我會問我的教授,但他今天沒有辦公時間,今晚我需要解決這個問題,因爲我在本週的其餘時間都很忙

+1

請把你在這裏的東西放在這裏,所以我們知道從哪裏開始。順便說一句,這聽起來完全像家庭作業。 – 2012-02-28 19:26:50

+0

意識到,但它不是。發誓對我母親的生活。它是我們明天在測試中使用的「備忘單」。當我回到我的房間時,我可以提出我的確切時間,它在我的電腦上 – zipzapzoop45 2012-02-28 19:38:20

+2

第二天測試的「備忘單」屬於我書中的「作業」範疇 – hirschhornsalz 2012-02-28 19:46:00

回答

1

除非堆棧指針做任何事情,否則沒有真正的理由我們想保留一些東西。然而,在這樣一個簡單的程序中,僅僅使用寄存器更容易。 (僅使用添加,子,多重峯,阿迪,與,或,ANDI,ORI,LW,SW,BEQ,BNE,SLT,的SLTⅠ,MFLO。)

.text 
    .global main 
main: 
    addi $t0, $zero, 10   # (counter) we will start with 10 and go down to zero 
    add  $t1, $zero, $zero  # (sum) our sum, 0 
count: 
    add  $t1, $t1, $t0   # sum += counter 
    addi $t0, $t0, -1   # counter -= 1 
    bne  $t0, $zero, count  # if (counter) goto count 
    add  $v0, $zero, $t1   # return value, our sum 
    #jr  $ra      # return (jr not allowed?) 

如果你真的想利用堆棧存儲局部變量(count和sum),你可以做這樣的事情。但是,正如你所看到的,這是一個相當廣泛和不必要的。

.text 
    .global main 
main: 
    addi $sp, $sp, -12  # Make room on the stack ($ra, sum, counter) 
    sw  $ra, 0($sp)   # save the return address (not really needed) 
    sw  $zero, 4($sp)  # sum variable, set to 0 
    addi $t0, $zero, 10 
    sw  $t0, 8($sp)   # counter variable, set to 10 
count: 
    lw  $t0, 4($sp)   # load sum 
    lw  $t1, 8($sp)   # load counter 
    add  $t0, $t0, $t1  # sum += counter 
    addi $t1, $t1, -1  # counter -= 1 
    sw  $t0, 4($sp)   # save the sum value 
    sw  $t1, 8($sp)   # save the counter 
    bne  $t1, $zero, count # if (counter) goto count 

    lw  $v0, 4($sp)   # return value, the sum 
    lw  $ra, 0($sp)   # restore caller's address 
    addi $sp, $sp, 12  # pop the stack 
    #jr  $ra     # return to caller (jr not allowed?)