简体   繁体   English

如何在内联汇编中使用 if-else?

[英]How to use if-else in inline assembly?

I'm working on STM32CubeIDE.我正在研究 STM32CubeIDE。

To mix C and arm assembly, we initially used EXPORT.为了混合 C 和 arm 程序集,我们最初使用了 EXPORT。

Below is main.c:下面是main.c:

#include <stdio.h>

extern int calc(int num, int* cnt);
int main()
{
    int cnt = 5;
    calc(4, &cnt);
    return 0;
}

And then calc.s:然后计算:

AREA calculator, CODE
EXPORT calc
ALIGN
calc:PROC
    PUSH {r4,r5,lr}
    MOV r5, #13
    UDIV r4, r0, r5
    MUL r5, r4, r5
    SUB r4, r0, r5
    CMP r4,#0
    BEQ ace
    CMP r4,#8
    BGT jqk
then:   ADD r4, r4, #1
    B exit
ace:ADD [r1],#1
    MOV r4, #11
    B exit
jqk:MOV r4, #10
exit:MOV r0, r4
    POP {r4, r5, pc}
    ENDP

I put the two files in the same place and built main.c, but I get an error that says it's an unknown external reference.我将这两个文件放在同一个地方并构建了 main.c,但我得到一个错误,说它是一个未知的外部引用。

So after giving up, I tried to put the ASM sentence in the c file using inline assembly.所以放弃后,我尝试使用内联汇编将ASM语句放入c文件中。

calc    PROC
    PUSH    {r4, lr}
    AND     r4, r0, #12
    CMP     r4, #0
    BEQ ace
    CMP r4, #8
    BGT jqk
then    ADD r4, r4, #1
    B   exit
ace ADD r1, r1, #1
    MOV r4, #11
    B   exit
jqk MOV r4, #10
exit    MOV r0, r4
    ENDP

I've written these assembly codes, and I've adapted them to inline grammar.我已经编写了这些汇编代码,并将它们改编为内联语法。 reference : Labels in GCC inline assembly参考: GCC 内联汇编中的标签

int calc(int num, int *cnt)
{
    int rst=0;
    int c = *cnt;
    __asm volatile("MOV R0, %0": :"r"(num));
    __asm volatile("AND R0, R0, %0": :"r"(0x12));
    __asm volatile("MOV R1, %0": :"r"(c));
    __asm volatile("CMP R0, #0");
    __asm volatile("BEQ ace%=");
    __asm volatile("CMP R0,#8");
    __asm volatile("BGT jqk%=");
    __asm volatile("ADD R2, R0, #1");
    __asm volatile("B exit%=");
    __asm volatile("ace%=: ADD R1, R1, #1");
    __asm volatile("MOV R2, #11");
    __asm volatile("B exit%=");
    __asm volatile("jqk%=: MOV R2, #10");
    __asm volatile("exit%=: LDR %0, R2":"=r"(rst):);
    __asm volatile("LDR %0, R1":"=r"(c):);
    __asm volatile("POP {R0, R1, R2}");
    *cnt = c;
    return rst;
}

But even in this case, an error appears.但即使在这种情况下,也会出现错误。 What should I change in my code?我应该在我的代码中更改什么?

''' '''

int calc(int num, int *cnt)
{
    int tmp = num % 13;
    if (tmp == 0)
    {
        tmp = *cnt;
        *cnt = tmp+1;
        return 11;
    }
    else if (tmp > 8)
        return 10;
    else
        return tmp + 1;
}

''' '''

You actually did the right thing initially, putting the asm code in a separate .s file and not using inline asm at all.实际上,您最初做了正确的事情,将 asm 代码放在单独的 .s 文件中,根本不使用内联 asm。 The only thing is that you need to explicitly compile and link the calc.s file along with main.c唯一的事情是您需要显式编译和链接 calc.s 文件以及 main.c

cc -o program main.c calc.s

should compile and assemble both files and link them.应该编译和组装这两个文件并链接它们。 If you're using an IDE, you need to specify both main.c and calc.s as source files of the project.如果您使用的是 IDE,则需要将 main.c 和 calc.s 都指定为项目的源文件。

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

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