J.Swersey
J.Swersey

Reputation: 171

LDRB in ARM ASM - general questions

I'm using an ARM architecture and I'm a little unclear on the concept with LDRB. LDRB being LDR with the optional B command, to load the least significant byte of the 32-bit word. Three issues.

  1. What I actually want is the first 8 bits, not the last 8 bits (I'm iterating over an array). Is there an elegant way to do this? I couldn't find a command for it.
  2. What I'm doing right now is SUB r4, r4, #3 to move r4 (my pointer I want to load) back three bytes so that the least significant byte is now the one in the front of the 32-bit word. Is this liable to cause memory issues if all I do is iterate forward?
  3. I'm using LDRB r7, r4; this should load the least significant byte from r4 into r7. By zero extend, it means what'll come out will be something like 00...00010101, or 10101000...00?

Upvotes: 2

Views: 31780

Answers (1)

Notlikethat
Notlikethat

Reputation: 20914

If you want the MSB of a 32-bit word, then get the MSB of a 32-bit word:

LDR R0, [R1]
LSR R0, #24

Anything else is confusing and completely unportable across systems of different endianness - trying to byte address words assuming LE breaks on a BE system. What works on BE-8 breaks if it ever finds itself on an ancient BE-32 system.

In fact, if loading a byte from [word ptr - #3] really gives you the MSB rather than the second-least significant byte of the previous word then I think you are on a BE-32 system* - thus this won't work correctly on anything modern.

* or maybe BE-8 - the trickery of memory endianness vs. bus endianness and word vs. byte addressing is massively confusing and I make no claim to have fully grapsed it.

Upvotes: 6

Related Questions