在装配引用传递 [英] pass by reference in assembly

查看:213
本文介绍了在装配引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序来计算采用ARM-C间工作的一些指数。我使用LPC1769(皮层立方米)的debuuging。以下是code:

I am trying to write a program to calculate the exponential of a number using ARM-C inter-working. I am using LPC1769(cortex m3) for debuuging. The following is the code:

/*here is the main.c file*/

#include<stdio.h>
#include<stdlib.h>
extern int Start (void);
extern int Exponentiatecore(int *m,int *n);
void print(int i);
int Exponentiate(int *m,int *n);
int main()
{
Start();
return 0;
}


int Exponentiate(int *m,int *n)
{
    if (*n==0)
        return 1;
    else
    {
        int result;
        result=Exponentiatecore(m,n);
        return (result);
    }

}

void print(int i)
{
printf("value=%d\n",i);
}

这是大会code补充了上述C code

this is the assembly code which complements the above C code

.syntax unified
        .cpu cortex-m3
        .thumb
        .align
        .global Start
        .global Exponentiatecore
        .thumb
        .thumb_func

Start:
    mov r10,lr
    ldr r0,=label1
    ldr r1,=label2
    bl Exponentiate
    bl print
    mov lr,r10
    mov pc,lr

Exponentiatecore:    // r0-&m, r1-&n

mov r9,lr
ldr r4,[r0]
ldr r2,[r1]
loop:
mul r4,r4
sub r2,#1
bne loop
mov r0,r4
mov lr,r9
mov pc,lr

label1:
.word 0x02


label2:
.word 0x03

但在调试会话期间,我遇到一个Hardfault错误的Exponentiatecore(M,N)执行。

however during the debug session, I encounter a Hardfault error for the execution of "Exponentiatecore(m,n)".

如被看见在调试窗口。

Name : HardFault_Handler
Details:{void (void)} 0x21c <HardFault_Handler>
Default:{void (void)} 0x21c <HardFault_Handler>
Decimal:<error reading variable>
Hex:<error reading variable>
Binary:<error reading variable>
Octal:<error reading variable>

我是否对齐过程中做一些堆栈损坏或者是有我的跨pretation错了吗?
请好心帮。
三江源提前

Am I making some stack corruption during alignment or is there a mistake in my interpretation? please kindly help. thankyou in advance

推荐答案

有几个问题你code。首先,你有一个无限循环,因为你的SUB指令不设置标志。将其更改为SUBS。接下来的问题是,你不必要操纵LR寄存器。你不从Exponentiatecore调用等功能,所以不碰LR。函数的最后一个指令应是​​BX LR返回给调用者。问题#3是你的乘法指令是错误的。除了服用3个参数,如果乘以自身的数量,那就得快速增长。例如:

There are several problems with your code. The first is that you have an infinite loop because your SUB instruction is not setting the flags. Change it to SUBS. The next problem is that you're manipulating the LR register unnecessarily. You don't call other functions from Exponentiatecore, so don't touch LR. The last instruction of the function should be "BX LR" to return to the caller. Problem #3 is that your multiply instruction is wrong. Besides taking 3 parameters, if you multiplied the number by itself, it would grow too quickly. For example:

ExponentiateCore(10,4);结果,
通过每个循环值:结果
R 4 = 10,n = 4的结果,
R4 = 100,N = 3结果
R4 = 10000,N = 2结果
R4 =亿n = 1的结果。

ExponentiateCore(10, 4);
Values through each loop:
R4 = 10, n = 4
R4 = 100, n = 3
R4 = 10000, n = 2
R4 = 100,000,000 n = 1

问题#4是你改变非易失性寄存器(R4)。除非你保存/恢复它们,你只允许垃圾R0-R3。试试这个:

Problem #4 is that you're changing a non-volatile register (R4). Unless you save/restore them, you're only allowed to trash R0-R3. Try this instead:

Start:
    stmfd sp!,{lr}
    ldr r0,=label1
    ldr r1,=label2
    bl Exponentiatecore // no need to call C again
    bl print
    ldmfd sp!,{pc}

        Exponentiatecore:    // r0-&m, r1-&n

        ldr r0,[r0]
        mov r2,r0
        ldr r1,[r1]
        cmp r1,#0      // special case for exponent value of 0
        moveq r0,#1
        moveq pc,lr    // early exit
    loop:
        mul r0,r0,r2      // multiply the original value by itself n times
        subs r1,r1,#1
        bne loop
        bx lr

这篇关于在装配引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆