繁体   English   中英

如何处理 C 中的负数?

[英]How do I deal with negative numbers in C?

我正在尝试用 C 编写一种非常基本的汇编语言。每条指令有 32 位,前 8 位是操作码,接下来的 24 位包含一个立即值(如果指令带有一个。否则它只是零)。 操作码被定义为简单的数字。 例如PUSHC被定义为1

现在我想测试操作码和立即值是否可以正确分离。

我写了以下定义:

#define SIGN_EXTEND(i) ((i) & 0x00800000 ? (i) | 0xFF000000 : (i))      // Handles negative Immediate Values (i)
#define IMMEDIATE(x) SIGN_EXTEND(x & 0x00FFFFFF)                        // Returns the Immediate Value of an instruction (x)
#define OPCODE(x) (x >> 24)                                             // Returns the Opcode of an instruction (x)
#define OP(o) ((o) << 24)                                               // An instruction with an Opcode (o)
#define OPI(o, i) (OP(o) | IMMEDIATE(i))                                // An instruction with an Opcode (o) and an Immediate Value (i)

在方法void runProg(uint32_t prog[]) {...}中,我按顺序传递一个包含汇编程序指令的数组,以逗号分隔,如下所示:

uint32_t progTest[] = {OPI(PUSHC, 3), OPI(PUSHC, 4}, OP(ADD), OP(HALT)};
runProg(progTest);

这是runProg的作用:

void runProg(uint32_t prog[]) {
    int pc = -1;            // the program counter
    uint32_t instruction;

    do {
        pc++;
        instruction = prog[pc];
        printf("Command %d : 0x%08x -> Opcode [%d] Immediate [%d]\n",
               pc, instruction, OPCODE(instruction), IMMEDIATE(instruction));
    } while (prog[pc] != OP(HALT));

}

所以它以十六进制形式打印出完整的命令,然后是操作码,然后是立即值。 这适用于我定义的所有指令。 上面的测试程序给出了这个 output:

Command 0 : 0x01000003 -> Opcode [1] Immediate [3]
Command 1 : 0x01000004 -> Opcode [1] Immediate [4]
Command 2 : 0x02000000 -> Opcode [2] Immediate [0]
Command 3 : 0x00000000 -> Opcode [0] Immediate [0]

现在问题来了:

命令PUSHC仅适用于正值。 将第一个PUSHC调用的立即值更改为-3会产生以下结果:

Command 0 : 0xfffffffd -> Opcode [255] Immediate [-3]
Command 1 : 0x01000004 -> Opcode [1] Immediate [4]
Command 2 : 0x02000000 -> Opcode [2] Immediate [0]
Command 3 : 0x00000000 -> Opcode [0] Immediate [0]

如您所见,立即值显示正确,但命令缺少操作码。

IMMEDIATE(x)的定义从

#define IMMEDIATE(x) SIGN_EXTEND(x & 0x00FFFFFF)

#define IMMEDIATE(x) (SIGN_EXTEND(x) & 0x00FFFFFF)

产生完全相反的结果,其中 Opcode 被正确分隔,但立即值是错误的:

Command 0 : 0x01fffffd -> Opcode [1] Immediate [16777213]
Command 1 : 0x01000004 -> Opcode [1] Immediate [4]
Command 2 : 0x02000000 -> Opcode [2] Immediate [0]
Command 3 : 0x00000000 -> Opcode [0] Immediate [0]

因此,我比较确定我对SIGN_EXTEND(i)的定义是有缺陷的。 但是,我似乎无法确定。

非常感谢有关如何解决此问题的任何想法!

0x01fffffd是正确的; 8位操作码域包含0x01,24位立即数域包含0xfffffd,即-3的24位二进制补码编码。 编码时,无需符号扩展; IMMEDIATE宏传递一个(大概)32 位二进制补码值,它的工作只是将其减少到 24 位,而不是扩展它。 所以它只需要是#define IMMEDIATE(x) ((x) & 0xffffff) 当你想解释立即字段时,比如打印,那么你需要从 24 位转换为 32 位。为此,你需要一个不同的宏,就像你有不同的宏来编码操作代码( OPI )和解码/提取/解释操作码( OPCODE )。

您可以使用此 function 解释立即字段:

static int InterpretImmediate(unsigned x)
{
    //  Extract the low 24 bits.
    x &= (1u<<24) - 1;

    /*  Flip the sign bit.  If the sign bit is 0, this
        adds 2**23.  If the sign bit is 1, this subtracts 2**23.
    */
    x ^= 1u<<23;

    /*  Convert to int and subtract 2**23.  If the sign bit started as 0, this
        negates the 2**23 we added above.  If it started as 1, this results in
        a total subtraction of 2**24, which produces the two’s complement of a
        24-bit encoding.
    */
    return (int) x - (1u<<23);
}

暂无
暂无

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

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