简体   繁体   English

来自C ASM的NodeJS按位运算符

[英]NodeJS bitwise operator from C ASM

This is related to my previews question . 这与我的预览问题有关

A complete ASM of the function in C is here . C中函数的完整ASM就在这里

My problem lies on: 我的问题在于:

00408091  |>  F6D3          ||NOT BL
00408093  |.  FEC3          ||INC BL
00408095  |.  02D3          ||ADD DL,BL

On javascript when I use the NOT bitwise operator on 0x35 it returns -36 instead of the expected 0xCA . 在javascript上,当我在0x35上使用NOT位运算符时,它返回-36而不是预期的0xCA Why is that? 这是为什么?

  // cmp al, dl
  if (b <= a) {
    a -= b;
  } else {
    // problem lies here
    console.log(~b);
    a += b;
    a++;
  }

My nodejs code currently is: 我的nodejs代码目前是:

for (var i = 0; i < hashed.length; i++) {
  var a = hashed[i];
  var b = seqX[i];
  var c = seqX[i+1];

  var stepIn = i+1;
  var stepOver = stepIn-1;

  // cmp BL, DL -- 0x63, 0x4e -- 0xf9, 0xc5 -- 0x75. 0x7a
  for (var j = internalRounds - 1; j > 0; j--, stepIn--, stepOver--) {
    if (seqX[(i*2)+1] <= a) {
      a -= seqX[(i*2)+1];
    } else {
      a += seqX[(i*2)+1];
      a++
    }
    // xor dl, bl -- 0x1c, 0xc0
    a ^= seqY[stepIn];
  }

  // cmp al, dl
  if (b <= a) {
    a -= b;
  } else {
    // problem lies here
    console.log(~b);
    a += b;
    a++;
  }

  // xor al, dl --- 0xd4, 0xb8
  a ^= seqY[i];

  // xor al, cl
  a ^= 0x6e;

  console.log(a.toString(16)); // I expect this to be 2.
  console.log('--------------------------');
}

The following sequence of instructions: 以下顺序说明:

00408091  |>  F6D3          ||NOT BL
00408093  |.  FEC3          ||INC BL
00408095  |.  02D3          ||ADD DL,BL

Is treating the bytes in BL and DL as signed values and essentially subtracting BL from DL . BLDL的字节视为有符号值并基本上从DL减去BL The first two take the 2's complement negation of BL as a byte, then the third adds the negated BL to DL . 前两个将BL的2的补码否定作为一个字节,然后第三个将否定的BL添加到DL Unless there are subsequent instructions that deal a certain way with the processor status flags, I'm not sure why it doesn't just say, SUB DL,BL . 除非有后续指令以某种方式处理处理器状态标志,否则我不确定它为什么不只是说, SUB DL,BL

So I would think this group of instructions would just translate into: 所以我认为这组指令只会转化为:

b -= a;

Rather than literally translating each instruction. 而不是字面翻译每个指令。

This depends somewhat on the context of the whole asm program, how BL and DL are treated. 这在某种程度上取决于整个asm程序的上下文,如何处理BLDL If they are treated as byte values consistentlyt, then the above should work. 如果它们被一致地视为字节值,则上述应该起作用。

That because javascript always operates with 32-bit signed integers. 因为javascript总是以32位有符号整数运行。 So you have to truncate result to 8 bits. 所以你必须将结果截断为8位。

(~0x35) & 0xFF // results 0xCA

It looks like you have two problems. 看起来你有两个问题。 The first is that you are doing ~35 , not 0x35 (the first is decimal 35, the second hex 35). 第一个是你正在做~35 ,而不是0x35 (第一个是十进制35,第二个是十六进制35)。

The second is that the bitwise-not in javascript casts the argument to a 32-bit signed integer, whereas it appears that you expect it to be treated as an unsigned char (8-bits). 第二个是bitwise-not in javascript将参数转换为32位有符号整数,而看起来你希望它被视为unsigned char(8位)。 You can fix this by taking the result of the not and doing a bitwise-and with 0xFF . 您可以通过获取not的结果并按位并使用0xFF来解决此问题。 So: 所以:

(~0x35) & 0xFF
> 202
((~0x35) & 0xFF).toString(16)
> "ca"

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

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