logoalt Hacker News

Joker_vDlast Wednesday at 3:45 PM1 replyview on HN

Yes, it would:

    add_large_const:
            add     w8, w0, #43, lsl #12
            add     w0, w8, #42
            ret
The same happens e.g. on RISC-V:

    add_large_const:
            lui     a5,43   
            addi    a5,a5,42
            add     a0,a0,a5
            ret
Because there are only 32 bits in an instruction, you can't fit a whole 32-bit immediate into it. Contrast it with x86 which uses variable-length instructions (up to 15 bytes per instruction):

    add_large_const:
            lea     eax, [rdi+176170]    # this instruction takes 6 bytes
            ret
P.S. Some people seem to be really puzzled by LEA instructions. It's intended use is to correspond to C's "dest = &base[offset]" which semantically is just "dest = base + offset", of course — but it allows one to express base and/or offset using available addressing modes.

Replies