简体   繁体   English

如何将无符号24位int转换为带符号的int并将符号扩展为32位?

[英]How to convert an unsigned 24 bit int into a signed int and sign extend to 32 bits?

I'm attemting to encode the branch instruction of the ARM architecture into a printout (converting a 32bit number into assembly). 我正在考虑将ARM体系结构的分支指令编码为打印输出(将32位数转换为汇编)。 For the Branch instruction. 对于分支指令。 I have to extend a signed_immed_24 value to 32 bits and shift left to obtain the value for the assembly code. 我必须将signed_immed_24值扩展为32位并向左移动以获取汇编代码的值。

Currently, the integer is unsigned. 目前,整数是无符号的。 I was wondering if anyone had any useful tips for converting it to a signed integer transforming 9991764 into -6785452. 我想知道是否有人有任何有用的提示将其转换为有符号的整数,将9991764转换为-6785452。 And then shifting logically left by 2 to give the final answer of -27141808 然后逻辑上左移2,得到-27141808的最终答案

The data of the 32 bit is contained within a defined data structure. 32位的数据包含在定义的数据结构中。

/* Branch Instruction format data set */
typedef struct {
    uint32_t in;
    int cnd; char *cndbits; char *cndname;
    int IOO; char *IOObits; char *IOOname;
    int L;   char *Lbits;   char *Lname;
    int im;  char *imbits;  char *imname;
} arm_b;

Where im is the integer value to be converted. 其中im是要转换的整数值。

This is the function that (when working) will print the assembly code. 这是(工作时)将打印汇编代码的功能。

/* prints the assembly language of the branch instruction */
void print_b_ass(arm_b *b_instr) {
    printf("\t B 0x3e61d950 <PC + -27141808 (-6785452 << 2)>\n\n");
}

If I'm understanding what you're asking for: 如果我理解你的要求:

int a = 9991764;
a <<= 8;
a /= 64;
// a = -27141808

(I've changed this to a divide from a right-shift, because technically the standard says that a shift might be logical and not arithmetic. I assume the divide will end up as the correct shift anyway) (我把它改成了右移的区别,因为从技术上来说,标准说换班可能是合乎逻辑的而不是算术。我认为除法最终会成为正确的换档)

Also assumes a 32-bit int. 还假设一个32位的int。

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

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