Bit 11 of jalr sign-extened for call (auipc + jalr)?

(daiw) #1

In a book , I read : call offset = auipc x1, offset[31:12] + jalr x1, x1, offset[11:0]
if the offset[11] is 1, jalr makes the sign-extension of bit 11,
should the offset[31:12] in auipc be encoded with destination-offset[31:12] - 0xFFFFF ?

(Jim Wilson) #2

The high part value needs to be biased up by 1 when the low part value will be negative. The calculation is more like ((offset+0x800)&~0xFFF).

(Bruce Hoult) #3

I’d make the calculation something like this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
    if (argc != 2) return 1;    
    int offset = atoi(argv[1]);

    int jalr_arg = (offset << 20) >> 20;
    int auipc_arg = (offset - jalr_arg) >> 12;
    printf("Offset = %d 0x%08x\n", offset, offset);
    printf("auipc ra, %d # 0x%05x\n", auipc_arg, auipc_arg & 0xfffff);
    printf("jalr ra, %d(ra) # 0x%03x\n", jalr_arg,  jalr_arg & 0xfff);
    return 0;

(daiw) #4

jimw, brue, thanks for your confirmation.
It is interesting that there is such this trick to encode offset. :slight_smile: