0
這是作業。我一直在試圖弄清楚我現在做了什麼錯誤。分配是:ARM loop cmp問題
考慮下面的代碼在C:
int foo(int a, int b)
{
if (a > b) return 0;
if (a == b) return b;
return a + foo(a+1, b);
}
實現在裝配此功能。從 函數分支回來後,輸出應如下所示。如果用戶輸入4 然後10:4 10 49
END
如果用戶輸入10,則4:
END
我的代碼,以便遠爲:
@main program
_start:
mov sp,#0x100000 @ set up stack
ldr r4,=0x101f1000
@ ASCII codes stored
@ at [r4] get printed
@ get input
bl get_int
mov r5, r0
bl get_int
mov r6, r0
@ mov a to r0 b to r1
mov r0, r5
mov r1, r6
@ branch to the function you write
bl foo
@ print the number in r0 after branching back from your function
bl print10
@ branch to exit
b my_exit
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ Your code starts here @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
foo:
cmp r0,r1
blt Less
bge More
mov r0,r1
b Exit
Less:
mov r3,r0
add r0,r0,#1
add r3,r3,r0
cmp r0,r1
blt More
mov r0,r3
More:
mov r0,#0
Exit:
bx lr
我遇到的問題是當我運行它時,不管輸入什麼數字,我總是得到0的輸出。
我不確定你在'Less:'上試圖做什麼,但它是不正確的。你應該做的就是用參數'a + 1,b'調用'foo'並返回結果加上'a'的當前值。 – Michael
'bge'不是[正確的條件](https://community.arm.com/groups/processors/blog/2010/07/16/condition-codes-1-condition-flags-and-codes)for'更多',它只是'bgt',因爲你想等於返回一個不同的值(a == b或R0 == R1)。您可以使用遞歸或* inline *其餘的'foo',而不使用遞歸。我會檢查[編譯器的功能](https://godbolt.org/g/ivsdCn)。 –
謝謝你artless。我將其改爲bgt,並能夠更明確地指出我做錯了什麼。最後我把它改成了L2和L2。得到它爲我需要的結果工作。再次感謝你 – Fireurza