bugra sezer
bugra sezer

Reputation: 195

LDR in Assembly Language diffrent forms?

I have a problem to solve but my solutions all come up wrong so I decided to come and ask it here in order to see where my mistakes are.

LDR r1, [r0, #4]
LDR r2, [r0], #8
LDR r3, [r0]
LDR r4, [r0, #4]!

These are all defined varibles in the memory.

Memory
Address Content
0x100   0x02
0x104   0x05 
0x108   0x0A
0x10C   0x10

This is the main question we need to write the last values.

Register    Initial Value   Last Value
r0          0x100           ?
r1          0x000           ?
r2          0x000           ?
r3          0x000           ?
r4          0x000           ?

With my calculations I was able to find the r1 correctly but after that I fail.For example the r2 come out 0x05 and at the end r0 come out 0x10C how can this be,there is no code here to save the r0 on to the memory why it changes.I guess my dont know what to do with r2 and r4.Anyone has knowlage about ARM help will be really good.

Thanks.

Upvotes: 0

Views: 3920

Answers (1)

Martin Rosenau
Martin Rosenau

Reputation: 18523

The instruction:

LDR rX, [rY, #Z]!

Is more or less equal to:

ADD rY, #Z
LDR rX, [rY]

And

LDR rX, [rY], #Z

More or less equals:

LDR rX, [rY]
ADD rY, #Z

Replacing the instructions your code looks like this:

LDR r1, [r0, #4]   ; Load memory content of 0x104 to r1
LDR r2, [r0]       ; Load memory content of 0x100 to r2
ADD r0, #8         ; Now R0 is 0x108
LDR r3, [r0]       ; Load memory content of 0x108 to r3
ADD r0, #4         ; Now R0 is 0x10C
LDR r4, [r0, #4]   ; Load memory content of 0x10C to r4

This means the registers must have the following values after:

R0 = 0x10C
R1 = 5
R2 = 2
R3 = 0xA
R4 = 0x10

I tried the program using an Arcorn Archimedes emulator and I get exactly these results.

Upvotes: 1

Related Questions