简体   繁体   English

为什么有人会在 C 中将 8 位值与 16 位掩码进行按位 AND 运算?

[英]Why would someone bitwise AND an 8-bit value with a 16-bit mask in C?

I am trying to replicate Javidx9's NES/MOS6502 CPU code in C# as an academic exercise and I am having trouble understanding the logic behind the implementation of the Zero-Page Addressing Mode.我试图在 C# 中复制 Javidx9 的 NES/MOS6502 CPU 代码作为学术练习,但我无法理解零页寻址模式实现背后的逻辑。 Specifically, I am looking at this code :具体来说,我正在查看此代码

// Address Mode: Zero Page
// To save program bytes, zero page addressing allows you to absolutely address
// a location in first 0xFF bytes of address range. Clearly this only requires
// one byte instead of the usual two.
uint8_t olc6502::ZP0()
{
    addr_abs = read(pc);    
    pc++;
    addr_abs &= 0x00FF;
    return 0;
}

I struggle to understand why addr_abs &= 0x00FF;我很难理解为什么addr_abs &= 0x00FF; is there, uint16_t addr_abs is 16 bits but uint8_t read(uint16_t a);在那里, uint16_t addr_abs是 16 位,但uint8_t read(uint16_t a); returns an 8-bit value anyways, so the upper 8 bits (MOS6502 is little-endian) would be 00'd out by default?无论如何都返回一个 8 位值,所以默认情况下高 8 位(MOS6502 是小端)将是 00'd? Am I missing something about how the C compiler/x86 ISA works?我是否遗漏了 C 编译器/x86 ISA 的工作原理?

You're correct addr_abs &= 0x00ff isn't needed.您是对的, addr_abs &= 0x00ff

uint16_t x = n where n is an unsigned 8-bit number (which is the case here). uint16_t x = n其中n是一个无符号的 8 位数字(这里就是这种情况)。 x would have it's upper 8 bits cleared. x会清除它的高 8 位。 As @tadman stated, there might have been a different method used previously to store the value into addr_abs which didn't clear the upper 8 bits.正如@tadman 所说,之前可能使用了一种不同的方法将值存储到addr_abs ,该方法没有清除高 8 位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM