windows linux跨平台,跨平台程序集((x64 x86)&&(Windows Linux)

本文档展示了作者尝试编写一个能够在Windows和Linux的x86/x64平台上运行的XOR函数,利用汇编语言直接操作[RE]AX和[RE]DX寄存器。代码中包含了条件编译以适配不同位宽的整数类型,并进行了静态类型检查。作者询问是否有更优雅或高效的方法来编写这样的跨平台汇编代码,以及在当前实现中是否存在错误。
摘要由CSDN通过智能技术生成

我正试图编写一些代码,以便进一步了解程序集和jit编译器之类的东西。到目前为止,我已经能够想出一个xor函数,理论上,它应该在windows和linux环境下的x86或x64机器上工作。

假设我正确地理解了事情,

[RE]AX

寄存器用于保存整数返回值,而

[RE]DX

是用于在函数之间传递整数的可用寄存器之一。我选择不严格遵循abi并使用

斧头

因为它节省了

MOV

不影响结果的指令。

是否有更好(更优雅或更高效)的方法来发出跨平台程序集,或者我在开发这个程序时犯了什么错误?

#include

#include

template

static auto Xor(TInput const highPart, TInput const lowPart) {

constexpr bool is16Bit = (std::is_same::value || std::is_same::value);

constexpr bool is32Bit = (std::is_same::value || std::is_same::value);

static_assert(is16Bit || is32Bit, "type must be a member of the type family: [u]int{16, 32}_t");

if constexpr (is16Bit) {

uint16_t result;

#if (defined(__linux__) || defined(__unix__) || defined(_WIN32))

asm volatile ("xorw %%dx, %%ax;" : "=a" (result) : "a" (highPart), "d" (lowPart));

#else

#error "Unsupported platform detected."

#endif

return result;

}

else if constexpr (is32Bit) {

uint32_t result;

#if (defined(__linux__) || defined(__unix__) || defined(_WIN32))

asm volatile ("xorl %%edx, %%eax;" : "=a" (result) : "a" (highPart), "d" (lowPart));

#else

#error "Unsupported platform detected."

#endif

return result;

}

}

#define HIGH_PART 4;

#define LOW_PART 8;

int main() {

int16_t const a = HIGH_PART;

int16_t const b = LOW_PART;

int16_t const c = Xor(a, b);

uint32_t const x = HIGH_PART;

uint32_t const y = LOW_PART;

uint32_t const z = Xor(x, y);

std::cout << c << "\n";

std::cout << z << "\n";

getchar();

return 0;

}

下面是一个如何改进的例子;通过“提升”

result

变量和

if defined(...)

支票高于

constexpr

检查我们可以使事情更一般。

template

static auto Xor(T const highPart, T const lowPart) {

constexpr bool is16Bit = (std::is_same::value || std::is_same::value);

constexpr bool is32Bit = (std::is_same::value || std::is_same::value);

static_assert(is16Bit || is32Bit, "type must be a member of the type family: [u]int{16, 32}_t");

#if !(defined(__linux__) || defined(__unix__) || defined(_WIN32))

#error "Unsupported platform detected."

#endif

T result;

if constexpr (is16Bit) {

asm volatile ("xorw %%dx, %%ax;" : "=a" (result) : "a" (highPart), "d" (lowPart));

}

else if constexpr (is32Bit) {

asm volatile ("xorl %%edx, %%eax;" : "=a" (result) : "a" (highPart), "d" (lowPart));

}

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值