How do ARM's load and store byte and half-word instructions work? What does signed vs. unsigned do?

arm, assembly, instructions

Solution

When talking ARM a "word" is 32 bits, a "halfword" is 16 bits, and a "byte" is 8 bits. If you read the instruction set documentation in the ARM Architectural Reference Manual (just get the one for ARMv5 if you dont know which one to get, infocenter.arm.com) you will see that a ldrb loads the byte into the lower 8 bits of the destination register padding the upper 24 bits to zeros. A ldrsb will sign extend instead of pad with zeros. Same goes for halfword.

if you have code like this:

char a,b,c;
...
c = a+b;
if(c<0)
{
}

And either a or b or both were in memory at the time that you needed to do this addition then you would ideally want to do a sign extended (assuming you have told your compiler that char is signed) load to save instructions sign extending the registers so you can perform the math and have the flags set right for the comparison.

From the ARM ARM.

LDRSB (Load Register Signed Byte) loads a byte from memory, sign-extends it to form a 32-bit word, and writes the result to a general-purpose register.

LDRB (Load Register Byte) loads a byte from memory, zero-extends it to form a 32-bit word, and writes the result to a general-purpose register.

Problem

I'm just starting to learn ARM and I'm having trouble understanding what the load and store instructions do exactly. Load instructions: ``` ldrsb ldrb ldrsh ldrh ldr ``` Store instructions: ``` strb strh str ``` What does it mean to "load halfword" signed or unsigned? Or to "load byte" signed or unsigned? What is the difference between signed and unsigned, and in what particular applications would some of the load/store instructions be practical to use? All in all, I'm looking for an intuitive understanding of what these instructions do, as I'm still confused about how they work and what their purposes are.

Original source