简体   繁体   中英

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). 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.

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. And then shifting logically left by 2 to give the final answer of -27141808

The data of the 32 bit is contained within a defined data structure.

/* 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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