Home > Mobile >  Does offset in ARM Assembly counts from the start or the end?
Does offset in ARM Assembly counts from the start or the end?

Time:12-15

i am very new to ARM assembly and i want to understand how the actual machine code looks like when you're storing using STUR. To give you an example here's 64 bits

0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
0000 

Now when i call

STUR X0, [X29,#var_8] ; Store the value of X0 (0xB) into the stack at offset 0x8

Does it turn into?

0000 0000 1011 0000 0000
0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
0000 

Or

0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
0000 0000 0000 1011 0000
0000

CodePudding user response:

Memory is byte addressable.  So, the best way to depict memory is by labeling with addresses and showing byte values.  Usually that is done in hex, but binary works, too, I suppose.

 Address  Value
10010000   00
10010001   44
10010002   33
10010003   55
...

Sometimes multiple bytes are shown on one line with only one address number:

 Address  Value
10010000   00 44 33 55
10010004   ...
...

Addresses are important in understanding how memory works.



0000 0000 1011 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

in hex and grouped as bytes is 00 B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00

This value, interpreted in little endian as a 64-bit number is 45056 (decimal).


0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1011 0000 0000

in hex and grouped as bytes is 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0B 00

The above value, interpreted in little endian as a 64-bit number is 2816 (decimal).


So, the answer to your question is neither, the value stored will be

B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

because that is the sequence of bytes in little endian whose value is 0xB, aka the 0x000000000000000B that was in x0.


To be clear, ARM processors could also be run as big-endian, but that doesn't seem to be very common.


The offset is very important as it changes the address where the value in x0 is written.

The offset is added to the base register (here x29) to form what is called the effective address, which determines the address where the first byte will be written.  Multi-byte items in memory are referred to by the lowest address among the bytes — it is this one address that is communicated to the memory system (along with control signals that indicate the size and direction of the memory transfer, here 16 bytes/64-bits and write to memory).

The value written to memory will still be 16 bytes and so the bytes of memory at 16 different, consecutive addresses are updated.  The bytes will appear in memory order according to little endian byte ordering.

That value can be read from memory using a 16 byte/64 bit read, and that will obtain the value 0x00..000B in a CPU register.  The read of that memory location does not have to match in the offset, only the effective address needs to be the same location to refer to the same item, and any addressing mode that will generate that same effective address will work to read that value

For example, if the stack pointer moves from pushing more things onto the stack — the stack pointer moves downward toward smaller values when pushing, in order to accommodate the additional items (things already on the stack stay where they are in terms of addresses) — then when using the stack pointer as base, the offset would need to increase to refer to the same effective address as written before additional items were pushed onto the stack; the amount of increase would need to exactly match the size of items pushed, to keep the effective address the same.

  • Related