0

Code:

dest EQU 0x40000000

AREA name, CODE, READONLY

ENTRY

MOV r0, #2  
LDR r1, =dest   
STR r0, [r1]    
stop B stop

END

This code writes the value of 2 to memory location 0x40000000. When I change this to 0x20000000, 2 fails to get written there. Same thing with 0x3FFFFFFF. When I set the memory location to 0x40003FFF, 2 gets printed onto that location, but when I change the address to 0x40004000, 2 fails to get printed there. Same thing for any address locations higher, like 0x50000000. So according to these outputs, it seems like STR only writes values onto a finite range of memory between 0x40000000 and 0x40003FFF.

Does anyone know why this is the case? Or is there something wrong with my code? I am using Keil uVision5, NXP LPC2140.

phuclv
  • 37,963
  • 15
  • 156
  • 475
John Park
  • 335
  • 5
  • 23
  • You read the documentation yes? what part did you not understand? – old_timer Mar 25 '19 at 01:03
  • @old_timer Documentation? You mean Keil's reference guide for STR? Yes. You mean what happens in memory during my debugging stage? Yes. – John Park Mar 25 '19 at 01:10
  • If yes then what do you understand? I'm not quite familiar with ARM but AFAIK the immediate is 12 bits long, plus the offset can be shifted 3 bits. That means the maximum offset is 15 bits and you can address in the range [-2^14, 2^14-1] around the base http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/BABFGBDD.html – phuclv Mar 25 '19 at 01:39
  • @phuclv Thanks for the edit! How did you edit my code like that? – John Park Mar 25 '19 at 01:40
  • @phuclv Hmmm...I see. So there seems to be an offset restriction of LSL #14 since 16,384 addresses lie between addresses 0x40000000 and 0x40003FFF . Thanks for pointing me in this direction. – John Park Mar 25 '19 at 01:45
  • I simply press edit then Ctrl+K. You should click the `?` button on the right of the editor to learn [how to format](https://stackoverflow.com/help/formatting). Yes of course there are always restrictions because you can't store a big or infinite number of immediate bits inside a 32-bit instruction – phuclv Mar 25 '19 at 01:47
  • 2
    @phuclv Hmm, I think you've thrown a red herring here. The OP changes the 'dest EQU', so the R1 contains the address he wants to write into and there is no need for immediates or shifts or anything of that sort. (See http://www.keil.com/support/man/docs/armasm/armasm_dom1361289875065.htm) for LDR pseudo-instruction. I believe that write attempts fail when he attempts to write into read-only/non-existent memory or something along these lines. It's hard to comment on this without knowing the environment (I asked about this in his previous question). – tum_ Mar 25 '19 at 10:19
  • 2
    The "NXP LPC2140" seems to have only 16kB of SRAM (Static Random-Access-Memory) which can hold values and can be modified. Seems like it is mapped into address space at 0x40000000..0x40003FFF ... you should be able to find this "mapping" info in data sheets for your platform, or in examples coming with it. (16kiB of memory means 16384 bytes... that's probably less than this web page has, so you have to be careful when designing code for it, to use the memory effectively). – Ped7g Mar 25 '19 at 11:07
  • 1
    @JohnPark the first thing you do when you start to work with a microcontroller is download the datasheet. These days and in this case and depending on vendor you also get a programmers reference manual which can vary by name, users manual, reference manual. And from those you see that they purchased an ARM core for this chip so you go to arm and get the architectural reference manual and the technical reference manual. as well as any tools manuals, THEN you can start work. In those manuals you see the memory map as well as the other things you did wrong. – old_timer Mar 25 '19 at 13:40
  • @JohnPark THOSE manuals, so when you read those manuals what part did you not understand about the address space as well as addressing alignment? – old_timer Mar 25 '19 at 13:40

1 Answers1

2

I couldn't find a datasheet for "LPC2140", but I found a datasheet for what appears to be a family of devices instead, and that the specific one you have could be LPC2142/2144. The datasheet, section 6.4, shows that SRAM is mapped to 0x40000000-0x40003FFF (assuming from what you've said that you have the 16 kB SRAM variant). That's the only address space you should be treating as general purpose RAM. Everything outside that range according to the datasheet looks scary and you should avoid it unless you fully know what you're doing.

One thing you should also be cognizant of is unaligned access. STR writes a full word at once (4 bytes) and so the address should be aligned on a word boundary. 0x40003FFF is not aligned to a 4-byte boundary; you should have been writing to 0x40003FFC instead. If you just wanted to write a single byte to 0x40003FFF, you should have used STRB instead.

Jeff
  • 7,504
  • 3
  • 25
  • 34