Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How 32 bit IR hold load instruction?(RISC style 32bit architechture)

I am bit confused with instruction size and addressable space (I assumed that instruction size should be same as size of address bits. I did not find enough explanation in my book)If I am correct, then in theory if we have a 2^32 addressable units(bytes) of memory in 32 bit architecture (RISC style) how a 4byte sized load instruction hold opcode as well as address?

like image 755
Saravanan Avatar asked Nov 20 '15 15:11

Saravanan


2 Answers

You're assuming that a single instruction can encode a load from an arbitrary absolute address. This is true on x86, even in 64bit mode (but there's a special opcode for loading from a 64bit absolute address with no displacement or index register, and the dest must be rax).

On most RISC architectures, loading from an absolute address is typically done with two mov-immediate instructions to set the upper and lower half of a register, then use that register as the address for a load.

For example,

int a;
int foo(void) { return a; }

compiles to (ARM gcc 4.8.2 on godbolt):

foo():
    movw    r3, #:lower16:.LANCHOR0 @ tmp113,
    movt    r3, #:upper16:.LANCHOR0 @ tmp113,
    ldr r0, [r3]    @, a
    bx  lr  @
a:
    .space  4
like image 80
Peter Cordes Avatar answered Sep 22 '22 16:09

Peter Cordes


For memory access instructions, the instructions typically use an address register with offset. Something of the form : load R1, [R2 + 8]. ARM, x86, MIPS and many others offer this mode. There is often the option to use PC as the address register, to be able to fetch constants that are next to the code.

For jump instructions, in addition to using an address register, you often have an offset jump, that don't use any register but jump X instructions forward or backward. The offset is usually limited in range, and may be shifted by some amount (restricting to addresses multiple of 2, 4 ...), so it can fit in a small immediate operand.
MIPS also use a mix between absolute and relative jump : the j instruction jumps to an absolute address, but in the region of the current instruction. To be precise, the immediate address is missing a few upper bits (so that it can fit), and use the upper bits of the current PC instead.

like image 30
ElderBug Avatar answered Sep 18 '22 16:09

ElderBug