本文介绍了汇编程序模板,asmSymbolicName和"错误:期望的字符串字面"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GCC的扩展ASM像微软的汇编。根据微软的MASM,我们可以做到以下几点,其中 __ FUNC 是一个C变量:

  MOV EAX,__FUNC

根据,第6.44.3.1的输入操作数的:

When I try and use it in code:

unsigned int func = 1;
...

__asm__ __volatile__ (
  "movl %[__FUNC], %%eax"
  ...

  :
  : __FUNC "" (func)
);

It results in:

cpu.cpp:148:5: error: expected string-literal before ‘__FUNC’
 : __FUNC "" (func)
   ^
cpu.cpp:148:5: error: expected ‘(’ before ‘__FUNC’
cpu.cpp:148:17: error: ‘__FUNC’ was not declared in this scope
...

The best I can tell, I am using __FUNC according to the manual and its description of asmSymbolicName. But obviously I am not since there's an error.

What am I doing wrong?


I also tried the following because the manual told me I could use variable names in surrounding code, but it did not work either:

"movl %[func], %%eax"
...

:
: func "" (func)

And:

"movl %[func], %%eax"
...

:
: func (func)
...
解决方案

This is correct:

However [asmSymbolicName] means asmSymbolicName is literally surrounded by square brackets [ and ] (The outside brackets say that it is an optional parameter). You might have been going for this:

uint32_t func = 1;
uint32_t result;

__asm__ __volatile__ (
    "movl %[func], %[result]"
    : [result]"=a"(result)
    : [func]"g"(func)
);

This takes an input parameter of func makes it available as a symbolic name called func within the assembler template. Output parameter is eax (writable) with a symbolic name of result and eax will be stored in the C variable result when finished. I chose "g" since any immediate value, memory address, or register would be appropriate for the source in movl . You could pass an immediate value rather than a "C" variable with:

: [func]"g"(42)

这篇关于汇编程序模板,asmSymbolicName和"错误:期望的字符串字面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:54