- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在使用 GCC 的内联汇编器功能时,我尝试创建一个立即退出进程的函数,类似于 C 标准库中的 _Exit
。
这里是相关的源代码:
void immediate_exit(int code)
{
#if defined(__x86_64__)
asm (
//Load exit code into %rdi
"mov %0, %%rdi\n\t"
//Load system call number (group_exit)
"mov $231, %%rax\n\t"
//Linux syscall, 64-bit version.
"syscall\n\t"
//No output operands, single unrestricted input register, no clobbered registers because we're about to exit.
:: "" (code) :
);
//Skip other architectures here, I'll fix these later.
#else
# error "Architecture not supported."
#endif
}
这适用于调试构建(使用 -O0
),但只要我在任何级别打开优化,我都会收到以下错误:
immediate_exit.c: Assembler messages:
immediate_exit.c:4: Error: unsupported for `mov'
所以我查看了两个构建的汇编程序输出(为了清楚起见,我删除了 .cfi*
指令和其他内容,如果有问题我可以再次添加)。调试版本:
immediate_exit:
.LFB0:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
mov -4(%rbp), %rdi
mov $231, %rax
syscall
popq %rbp
ret
优化后的版本:
immediate_exit:
.LFB0:
mov %edi, %rdi
mov $231, %rax
syscall
ret
因此优化版本试图将 32 位寄存器 edi
放入 64 位寄存器 rdi
,而不是从 rbp< 加载它
,我认为这是导致错误的原因。
现在,我可以通过将“m”指定为 code
的寄存器约束来解决此问题,这会导致 GCC 从 rbp
加载,而不管优化级别如何。但是,我宁愿不那样做,因为我认为编译器及其作者比我更清楚把东西放在哪里。
所以(最后!)我的问题是:我如何说服 GCC 使用 rdi
而不是 edi
进行汇编输出?
最佳答案
总的来说,使用约束将值放入正确的寄存器比显式移动要好得多:
#include <asm/unistd.h>
asm volatile("syscall"
: // no outputs. Other syscalls need an "=a"(retval) to tell the compiler RAX is modified, whether you actually use the retval or not.
: "D" ((uint64_t)code), "a" ((uint64_t)__NR_exit_group) // 231
: "rcx", "r11" // syscall itself clobbers these. exit can't fail and return; mostly here as an example for other syscalls
, "memory" // make sure any stores, e.g. to mmapped files, are done before this
);
__builtin_unreachable(); // tell the compiler execution doesn't come out the bottom of the asm statement. Maybe have the same effect as a "memory" clobber of making sure not to delay stores which could potentially be to mmapped files or shared memory.
如果有用的话,这可以让编译器提升代码中较早的移动,或者如果可以将值安排为已经在正确的寄存器中,甚至可以完全避免移动...
例如 code
如果此函数不内联,则将在 EDI 中; Linux 系统调用约定被选择为尽可能接近 x86-64 System V 函数调用约定,除了使用 R10 而不是 RCX 因为 syscall
指令本身用保存的覆盖它 - RIP 和带有保存的 RFLAGS 的 R11。
(不过,在这种情况下,不必要地转换 (uint64_t)code
会强制编译器使用 mov %edi, %edi
重做零扩展。电话号码确实需要零扩展到 64 位,这几乎肯定会免费发生,即使您没有手动转换它(因为编译器将使用 mov $231, %eax
), 但明确说明所需的内容并没有坏处。exit_group
系统调用采用 32 位 int
arg,因此内核保证忽略 RDI 中的高垃圾。)
关于c - "unsupported for mov"GCC 内联汇编器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721713/
这个问题在这里已经有了答案: Differences between general purpose registers in 8086: [bx] works, [cx] doesn't? (3
我最近开始探索计算机体系结构领域。在研究指令集体系结构时,我遇到了“ mov”指令,该指令将数据从一个位置复制到另一个位置。我知道某些类型的mov'指令是有条件的,而有些则需要添加偏移量或位移来查找特
我正在研究使用模拟 MSP430 CPU 的 Microcorruption CTF。 我见过几个 mov 指令示例,例如: mov sp, r4 ;将堆栈指针的值移至寄存器4 mov #0xfffc
我不明白 MOV 和 MOV ptr 之间的区别。 例如,在这段 C 代码中: unsigned char x, y; x = 2; 汇编中的第二行是: `MOV x, 2` 但是这个 C 代码的第二
MOV可能是每个人在学习ASM时都会学到的第一条指令。 刚才我遇到了一本书Assembly Language Programming in GNU/Linux for IA32 Architectur
下面两行有什么区别? mov ax, bx mov ax, [bx] 如果bx包含值100h,并且内存地址100h处的值是23,那么第二个是否将23复制到ax? 另外,下面两行有什么区别? mov a
我编写了一个基本的 C 程序,它定义了一个整型变量 x,将其设置为零并返回该变量的值: #include int main(int argc, char **argv) { int x;
我是一个初学者,正在编写汇编程序以使用以下代码打印从 1 到 9 的数字: section .text global _start _start:
mov (%rax),%eax有什么区别和 mov %rax,%eax ?我确定这是一个简单的问题,但我在任何地方都找不到答案。 这是提示我的问题的原始代码: mov -0x8(%rbp),%r
有人可以解释一下这三个指令的功能吗? ORG 1000H MOV AX,CS MOV DS,AX 我知道理论上的代码、数据和额外段是什么,但是: 在这个程序中它们是如何实现的? 为什么整个
在 8086 架构的 16 位 MS-DOS 应用程序中,mov bx,ax 和 mov bh,ah 之间的速度有区别吗? 最佳答案 您没有指定架构,但至少在 8086 中指定, 286 , 386和
我正在反汇编一些代码,我发现: mov eax, cr3 mov cr3, eax 这些线的作用是什么? 这是 x86 低级(BIOS/固件/引导加载程序之前)初始化代码。我们甚至还没有设置缓存。 最
使用 nasm 组装此代码时: BITS 64 mov eax, 0x1 mov rax, 0x1 我得到这个输出: b8 01 00 00 00 b8 01 00 00 00 这是 mov eax,
我试图理解 Intel 语法和 AT&T 语法之间的差异(我使用 GNU as)。 我有两个文件,intel.s: .intel_syntax noprefix val: mov eax, v
我需要一种非常精确的方法来加速音频。 我正在为 OpenDCP(一种用于制作数字电影包的开源工具)准备电影,以便在影院放映。 我的源文件通常是 23.976fps 和 48.000kHz 音频的 qu
通过查看英特尔指令卷,我发现了这一点: 1) 88/r MOV r/m8,r8 2) 8A/r MOV r8,r/m8 当我在 NASM 中写下这样的一行,并使用列表选项将其组装时: mov al
Intel 手册说 mov 有两种变体,涉及内存和 32 位立即操作数: MOV r/m32, imm32 MOV r/m64, imm32 第一个复制四个字节,第二个复制八个字节,采用给定的 32
我已经处理了一天了,最后不得不出来问。我想获取一个无声的 prores mov 文件(但显然确实有时间码轨道)并将其与 6 个单声道 wav 文件无损混合,使 6 个单声道 wav 在最终 mov 中
这是我的代码: section .data digit db 0,10 section .text global _start _start: call _printRAXD
我在问 mov需要计算该地址的指令,即(在 at&t 语法中mov i(r, r, i), reg或 mov reg, i(r, reg, i)必须在端口 1 上执行,因为它们实际上是带有 3 个操作
我是一名优秀的程序员,十分优秀!