Asm代码解释
以下 GCC 内联汇编取自 LuaJit 的 coco 库。有人可以逐行解释它的作用吗?
static inline void coco_switch(coco_ctx from, coco_ctx to)
{
__asm__ __volatile__ (
"movl $1f, (%0)\n\t"
"movl %%esp, 4(%0)\n\t"
"movl %%ebp, 8(%0)\n\t"
"movl 8(%1), %%ebp\n\t"
"movl 4(%1), %%esp\n\t"
"jmp *(%1)\n" "1:\n"
: "+S" (from), "+D" (to) : : "eax", "ebx", "ecx", "edx", "memory", "cc");
}
谢谢
The following GCC inline asm is taken from LuaJit's coco library. Can someone provide a line by line explanation of what it does?
static inline void coco_switch(coco_ctx from, coco_ctx to)
{
__asm__ __volatile__ (
"movl $1f, (%0)\n\t"
"movl %%esp, 4(%0)\n\t"
"movl %%ebp, 8(%0)\n\t"
"movl 8(%1), %%ebp\n\t"
"movl 4(%1), %%esp\n\t"
"jmp *(%1)\n" "1:\n"
: "+S" (from), "+D" (to) : : "eax", "ebx", "ecx", "edx", "memory", "cc");
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的ASM对细节有点模糊,但我想我可以给你一个大概的想法。
ESP:堆栈指针,EBP:基址指针。
将标签 1 的地址(在最后一行定义)移至参数 0(来自)。
将寄存器 ESP 的内容移至 (from + 4)。
将寄存器EBP的内容移入(from + 8)。
将(to+8)的内容移入寄存器EBP。
将(to+4)的内容移入寄存器ESP。
跳转到(to)中包含的地址。
“1:”是跳转标签。
“+S”声明“源”(读取)参数,“+D”声明目标(写入)参数。语句末尾的寄存器列表是“clobber”列表,可能被 ASM 代码修改的寄存器列表,因此编译器可以采取措施保持一致性(即,不依赖于 ECX 仍然包含相同的值)和以前一样)。
我猜 coco_ctx 的意思是“coco context”。因此:该函数将当前堆栈帧保存在“from”结构中,并将堆栈帧设置为“to”结构中保存的内容。基本上,它从当前函数跳转到另一个函数。
My ASM is a bit fuzzy about the details, but I think I can give you a general idea.
ESP: Stack pointer, EBP: Base pointer.
Move address of label 1 (defined on last line) into parameter 0 (from).
Move the content of register ESP into (from + 4).
Move the content of register EBP into (from + 8).
Move the content of (to + 8) into register EBP.
Move the content of (to + 4) into register ESP.
Jump to address contained in (to).
The "1:" is a jump label.
"+S" declares a "source" (read) parameter, "+D" a destination (write) parameter. The list of registers at the end of the statement is the "clobber" list, a list of registers possibly modified by the ASM code, so the compiler can take steps to maintain consistency (i.e., not relying on e.g. ECX still containing the same value as before).
I guess that coco_ctx means "coco context". So: The function saves the current stack frame in the "from" structure, and sets the stack frame to what's saved in the "to" structure. Basically, it jumps from the current function into another function.
DevSolar 有正确的答案 - 我只是补充一点,您可以更多地了解 EBP 和 ESP 的用途 这里。
DevSolar has the right answer -- I'll just add that you can learn a little more about what EBP and ESP are for here.