具有intel x86-32位汇编的gcc:访问C函数参数 [英] gcc with intel x86-32 bit assembly : accessing C function arguments
问题描述
我正在执行操作系统实施工作.
这是第一个代码:
I am doing an operating system implementation work.
Here's the code first :
//generate software interrupt
void generate_interrupt(int n) {
asm("mov al, byte ptr [n]");
asm("mov byte ptr [genint+1], al");
asm("jmp genint");
asm("genint:");
asm("int 0");
}
我正在使用gcc中的 -masm = intel
选项编译以上代码.还,这不是生成软件中断的完整代码.
我的问题是我收到 n undefined
的错误消息,该如何解决,请帮忙?
I am compiling above code with -masm=intel
option in gcc. Also,
this is not complete code to generate software interrupt.
My problem is I am getting error as n undefined
, how do I resolve it, please help?
它还会在链接时而不是在编译时提示错误,下面是图像
Also it promts error at link time not at compile time, below is an image
推荐答案
When you are using GCC, you must use GCC-style extended asm
to access variables declared in C, even if you are using Intel assembly syntax. The ability to write C variable names directly into an assembly insert is a feature of MSVC, which GCC does not copy.
对于这样的结构,使用单个程序集插入(而不是连续多个)也很重要.GCC可以并且将相对于周围的代码,包括相对于其他程序集插入,重新安排程序集插入,除非您采取特定的步骤来阻止它.
For constructs like this, it is also important to use a single assembly insert, not several in a row; GCC can and will rearrange assembly inserts relative to the surrounding code, including relative to other assembly inserts, unless you take specific steps to prevent it.
这个特殊的结构应该写成
This particular construct should be written
void generate_interrupt(unsigned char n)
{
asm ("mov byte ptr [1f+1], %0\n\t"
"jmp 1f\n"
"1:\n\t"
"int 0"
: /* no outputs */ : "r" (n));
}
请注意,我已经删除了最初的 mov
以及所有与A寄存器有关的要求,而是告诉GCC使用 n
将 n
加载到任何方便的寄存器中>"r" 输入约束.最好在程序集插入中做的尽可能少,并且将寄存器的选择留给编译器.
Note that I have removed the initial mov
and any insistence on involving the A register, instead telling GCC to load n
into any convenient register for me with the "r"
input constraint. It is best to do as little as possible in an assembly insert, and to leave the choice of registers to the compiler as much as possible.
我还已将 n
的类型更改为 unsigned char
以匹配INT指令的实际要求,并且我正在使用 1f
本地标签语法,这样,如果 generate_interrupt
成为内联函数.
I have also changed the type of n
to unsigned char
to match the actual requirements of the INT instruction, and I am using the 1f
local label syntax so that this works correctly if generate_interrupt
is made an inline function.
话虽如此,我恳请您为您的操作系统找到一种不涉及自修改代码的实现策略.好吧,除非您打算从自我修改中获得更多使用,无论如何.
Having said all that, I implore you to find an implementation strategy for your operating system that does not involve self-modifying code. Well, unless you plan to get a whole lot more use out of the self-modifications, anyway.
这篇关于具有intel x86-32位汇编的gcc:访问C函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!