# ARM Assembly Code

Here's an assembly program given to me, and I'm not sure how the program goes through.

 AREA 1, CODE, READONLY
ENTRY
MOV r1,#(1:SHL:30)
LDR r2, = 0xABCDEF12
STR r2,[r1]
LDRH r3,[r1]
LDRSH r4,[r1,#2]!
LDRSB r2, [r1,#-1]!
stop B     stop
END


Here's my take on it:

First line, r1= 0x40000000 since left shift 30 times, which means r1= 2^30.

In the second line, r2= 0xABCDEF12 since it is just simply loaded.

Now here comes the part that I don't understand.

STR r2,[r1]

From what I understand, I will store the content of r2 to the content of r1. In this case, I will assume that the address: 0x4000000 now contains the memory of 0xABCDEF12?

Then for the next line, I will load half-word data into r3 with the contents of the memory location at address 0x4000000. This means r3= 0x0000EF12 since only half-word data is loaded.

LDRSH, now I will need to add 2 to r1, then load signed half-word data into r4 with contents of the memory location at address 0x40000002? I don't have the data at address 0x40000002, so how do I figure out what does r4 contain?

Thanks!

• Assume your memory stores 8-bit bytes. If you put the 32-bit 0xABCDEF12 at address 0x4000000, then the byte at 0x4000000 is 0x12, the byte at 0x4000001 is 0xEF, etc. So now what bytes do expect will be at 0x4000002 and 0x4000003? What do you get if you read those bytes as a 16-bit value? Commented Aug 10, 2021 at 2:44
• @brhans why will will the byte at 0x4000001 be 0xEF? not sure about that
– Meep
Commented Aug 10, 2021 at 2:55
• Because if your memory is 8-bit byte addressable, then that 32-bit word 0xABCDEF12 you write must be readable in 8-bit chunks. 0x4000000 is the lowest address, so on a little-endian ARM machine you'd expect to find the least-significant byte of your 32-bit word there. 0x4000001 is the next byte address up, so you should expect to find the next higher byte from your original 32-bit word there, etc... Commented Aug 10, 2021 at 3:01
• @brhans so 0x4000002 is CD and 0x40000003 is AB. So r4 = 0xFFFF00CD? (Since signed and load half word?)
– Meep
Commented Aug 10, 2021 at 3:04
• Does the answer below solve your query? Commented Aug 11, 2021 at 4:26

In your case, the memory space is byte-addressable and the word-size is 32 bits. Which means, a single byte is stored/addressable by a memory location.

The instructions LDR and STR assume word-aligned addresses: $$\0\text{x}0\$$, $$\0\text{x}4\$$ .... Suppose you are loading from location $$\0\text{x}0\$$ using LDR, it means you are loading a word, i.e., $$\4\$$ bytes, from locations $$\0\text{x}0\$$, $$\0\text{x}1\$$, $$\0\text{x}2\$$, $$\0\text{x}3\$$. If this is a little-endian memory space, then the least-significant byte will be at $$\0\text{x}0\$$, and the most significant byte at $$\0\text{x}3\$$.

I don't have the data at address 0x40000002, so how do I figure out what does r4 contain?

The word 0xABCDEF12 is stored as bytes across addresses 0x40000000-0x40000003. The address 0x40000000 is word-aligned with bytes stored at:

0x4000000 : 0x12
0x4000001 : 0xEF
0x4000002 : 0xCD
0x4000003 : 0xAB


So the data/byte at 0x40000002 is 0xCD.

LDRSH, now I will need to add 2 to r1, then load signed half-word data into r4 with contents of the memory location at address 0x40000002?

Yeah, that's right. The instruction LDRSH, is used to load signed half-words, and it assumes half-word-aligned addresses: $$\0\text{x}0\$$, $$\0\text{x}2\$$ ... The address 0x40000002 is half-word-aligned.

The instruction loads half-word from 0x40000002 into r4 i.e., $$\2\$$ bytes at {0x40000003, 0x40000002} $$\\equiv\$$ 0xABCD is loaded into r4.

Since LDSRH loads signed half-words, the data 0xABCD will be sign-extended to word-size before loading into r4.

$$0\text{xABCD} \equiv 0\text{x}\color{blue}{1}010\text{_}1011\text{_}1100\text{_}1101$$

The most significant bit or sign-bit is $$\\color{blue}{1}\$$ here, so the data will be sign-extended as: $$0\text{x}\color{blue}{1111\text{_}1111\text{_}1111\text{_}1111\text{_}1}010\text{_}1011\text{_}1100\text{_}1101$$

Therefore, 0xFFFFABCD will be the data loaded into r4.