所以,我有这个代码:
constexpr unsigned N = 1000;
void f1(char* sum, char* a, char* b) {
for(int i = 0; i < N; ++i) {
sum[i] = a[i] + b[i];
}
}
void f2(char* sum, char* a, char* b) {
char* end = sum + N;
while(sum != end) {
*sum++ = *a++ + *b++;
}
}
Run Code Online (Sandbox Code Playgroud)
我想看看GCC 4.7.2会产生的代码.所以我跑了g++ -march=native -O3 -masm=intel -S a.c++ -std=c++11并获得了以下输出:
.file "a.c++"
.intel_syntax noprefix
.text
.p2align 4,,15
.globl _Z2f1PcS_S_
.type _Z2f1PcS_S_, @function
_Z2f1PcS_S_:
.LFB0:
.cfi_startproc …Run Code Online (Sandbox Code Playgroud) 我想在x86/x86_64上使用禁止的未对齐内存访问来模拟系统.是否有一些调试工具或特殊模式来执行此操作?
我想在使用为SPARC或其他类似CPU设计的软件(C/C++)时,在几台x86/x86_64 PC上运行许多(CPU密集型)测试.但是我对Sparc的访问是有限的.
据我所知,Sparc总是检查内存读写中的对齐是否自然(从任何地址读取一个字节,但只有当地址可被4整除时才允许读取4字节字).
可能是Valgrind还是PIN有这样的模式?还是特殊的编译模式?我正在寻找Linux非商业工具,但Windows工具也允许.
或者可能是EFLAGS中有秘密的CPU标志?
我希望能够%rbp在内联asm中使用基指针寄存器().这样的玩具示例是这样的:
void Foo(int &x)
{
asm volatile ("pushq %%rbp;" // 'prologue'
"movq %%rsp, %%rbp;" // 'prologue'
"subq $12, %%rsp;" // make room
"movl $5, -12(%%rbp);" // some asm instruction
"movq %%rbp, %%rsp;" // 'epilogue'
"popq %%rbp;" // 'epilogue'
: : : );
x = 5;
}
int main()
{
int x;
Foo(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望,因为我使用通常的序幕/结尾函数调用方法来推送和弹出旧的%rbp,这样就可以了.但是,当我尝试在内x联asm之后访问时,它会出现故障.
GCC生成的汇编代码(略微剥离)是:
_Foo:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
# INLINEASM
pushq %rbp; // prologue
movq %rsp, …Run Code Online (Sandbox Code Playgroud) 最近我在玩 freebsd 系统调用,我对 i386 部分没有问题,因为它在这里有很好的记录。但是我找不到 x86_64 的相同文档。
我看到人们在 linux 上使用相同的方式,但他们只使用程序集而不是 c。我想在我的例子中,系统调用实际上改变了一些被高优化级别使用的寄存器,所以它给出了不同的行为。
/* for SYS_* constants */
#include <sys/syscall.h>
/* for types like size_t */
#include <unistd.h>
ssize_t sys_write(int fd, const void *data, size_t size){
register long res __asm__("rax");
register long arg0 __asm__("rdi") = fd;
register long arg1 __asm__("rsi") = (long)data;
register long arg2 __asm__("rdx") = size;
__asm__ __volatile__(
"syscall"
: "=r" (res)
: "0" (SYS_write), "r" (arg0), "r" (arg1), "r" (arg2)
: "rcx", "r11", "memory"
); …Run Code Online (Sandbox Code Playgroud) 我试图在64位Linux上的GCC扩展内联ASM中输出相同的字符串两次.
int main()
{
const char* test = "test\n";
asm(
"movq %[test], %%rdi\n" // Debugger shows rdi = *address of string*
"movq $0, %%rax\n"
"push %%rbp\n"
"push %%rbx\n"
"call printf\n"
"pop %%rbx\n"
"pop %%rbp\n"
"movq %[test], %%rdi\n" // Debugger shows rdi = 0
"movq $0, %%rax\n"
"push %%rbp\n"
"push %%rbx\n"
"call printf\n"
"pop %%rbx\n"
"pop %%rbp\n"
:
: [test] "g" (test)
: "rax", "rbx","rcx", "rdx", "rdi", "rsi", "rsp"
);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,字符串只输出一次.我尝试了很多东西,但我想我错过了关于调用约定的一些注意事项.我甚至不确定clobber列表是否正确,或者我是否需要保存和恢复RBP和RBX.
为什么字符串不输出两次? …
我没有装配经验,但这是我一直在努力的.如果我缺少传递参数和通过程序集中的指针调用函数的任何基本方面,我想输入.
举例来说,如果我应该恢复我想知道ecx,edx,esi,edi.我读到它们是通用寄存器,但我找不到它们是否需要恢复?打电话后我应该做什么样的清理工作?
这是我现在的代码,它确实有效:
#include "stdio.h"
void foo(int a, int b, int c, int d)
{
printf("values = %d and %d and %d and %d\r\n", a, b, c, d);
}
int main()
{
int a=3,b=6,c=9,d=12;
__asm__(
"mov %3, %%ecx;"
"mov %2, %%edx;"
"mov %1, %%esi;"
"mov %0, %%edi;"
"call %4;"
:
: "g"(a), "g"(b), "g"(c), "g"(d), "a"(foo)
);
}
Run Code Online (Sandbox Code Playgroud)