简体   繁体   English

ARM汇编递归功能

[英]ARM assembly recursive power function

Need to convert the following C code into ARM assembly subroutine: 需要将以下C代码转换为ARM汇编子例程:

 int power(int x, unsigned int n) 
 {          
   int y; 
   if (n == 0)  
     return 1; 
   if (n & 1) 
     return x * power(x, n - 1); 
   else 
   { y = power(x, n >> 1); 
     return y * y; 
   } 
 }     

Here's what I have so far but cant figure out how to get the link register to increment after each return (keeps looping back to the same point) 这是我到目前为止的内容,但无法弄清楚如何在每次返回后使链接寄存器递增(基普循环回到同一点)

pow             CMP             r0, #0
                MOVEQ           r0, #1
                BXEQ            lr
                TST             r0, #1
                BEQ             skip
                SUB             r0, r0, #1
                BL              pow
                MUL             r0, r1, r0
                BX              lr
skip            LSR             r0, #1
                BL              pow
                MUL             r3, r0, r3
                BX              lr

The BL instruction does not automatically push or pop anything from the stack. BL指令不会自动从堆栈中压入或弹出任何内容。 This saves a memory access. 这样可以节省内存访问。 It's the way it works with RISC processors (in part because they offer 30 general purpose registers.) 这就是它与RISC处理器一起工作的方式(部分是因为它们提供30个通用寄存器)。

STR   lr, [sp, #-4]!   ; "PUSH lr"
BL    pow
LDR   lr, [sp], #4     ; "POP lr"

If you repeat a BL call, then you want to STR / LDR on the stack outside of the loop. 如果重复BL调用,那么您想在循环外的堆栈上进行STR / LDR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM