[英]Conversion of recursive C function into ARM assembly?
For a homework assignment, I've been given a recursive C function to count integer partitions that I need to convert to ARM assembly. 对于一项家庭作业,我得到了一个递归C函数,用于计算需要转换为ARM程序集的整数分区。 Things I know about ARM assembly:
我对ARM汇编的了解:
1) R0
will hold the return value of a call 1)
R0
将保留呼叫的返回值
2) R1
, R2
, and R3
are argument registers 2)
R1
, R2
和R3
是参数寄存器
The code is as follows: 代码如下:
int count_partitions(int n, int m) {
if (n == 0)
return 1;
else if(n < 0)
return 0;
else if (m == 0)
return 0;
else
return count_partitions(n - m, m) + count_partitions(n, m - 1);
}
I believe I have done the first 3 if & else-if
statements correctly. 我相信我已经正确地完成了前3个
if & else-if
语句。 My logic for the final else
statement was to find count_partitions(n, m-1), store that onto the stack, then find count_partitions(nm, m)
, and add that to the previous return value I got from the stack - however my code does not seem to work? 对于最后的
else
语句,我的逻辑是找到count_partitions(n,m-1),将其存储到堆栈中,然后找到count_partitions(nm, m)
,并将其添加到我从堆栈中获得的上一个返回值-但是我的代码似乎不起作用?
I've attached my attempted solution and have color coded the different segments of C code and their corresponding assembly code. 我已经附上了我尝试过的解决方案,并对C代码的不同段及其相应的汇编代码进行了颜色编码。 Could anyone let me know what's wrong?
谁能让我知道怎么了?
You can use this after the CMP command and jump your function: 您可以在CMP命令之后使用此命令并跳转功能:
BEQ label ; BEQ标签; BRANCH EQUAL
分支平等
BNE label ; BNE标签; BRANCH NOT EQUAL
分支不平等
BLE label ; BLE标签; BRANCH LESS THAN EQUAL
分支机构少于平等
BLT label ; BLT标签; BRANCH LESS THAN
少于分支
BGE label ; BGE标签; BRANCH GREATER THAN EQUAL
分店比平等多
BGT label ; BGT标签; BRANCH GREATER THAN
分支大于
I think I see several problems: 我认为我看到了几个问题:
n
is in r1
. n
在r1
。 This is actually in r0
. r0
。 m
will be in r1
, not r2. m
将在r1
,而不是r2中。 r0
and r1
. r0
和r1
。 One cleaner solution would be to use something like this: 一种更清洁的解决方案是使用如下所示的内容:
_count_partitions:
... ; First part with comparison
; but r1->r0 and r2->r1
push {r4-r5}
mov r4, r0 ; saved value of n
mov r5, r1 ; saved value of m
sub r0, r4, r5 ; n = n-m
bl _count_partitions
sub r1, r5, #1 ; m = m-1
mov r5, r1 ; result of first function
mov r0, r4 ; restore n
bl _count_partitions
add r0, r0, r5 ; cumulative result
pop {r4,r5}
pop {pc}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.