Search code examples
assemblyarmarm7

Why won't this change the values of some of the elements in array1 in memory?


I wrote some assembly code on Keil where if an element in array is < 5, the program increments that element. Trouble is, the ARM code does not change the values of array1 in memory. What changes do I need to make in order to do so?

    ADR r0, array1      ; loads address of 'a' to r0

    MOV r1, #0          ; r1 = index

L0  CMP r1, #8
    BGE stop
    LDR r2, [r0, r1, LSL#2] ; load content of array1[index] to r2
    CMP r2, #5
    ADDLT   r2, r2, #1      ; array1[index]++
    STRLT   r2, [r0, r1, LSL#2] ; store r2 as content of array1[index]
    ADD r1, r1, #1      ; index++
    B   L0

stop B  stop
array1  DCD 1, 7, 4, 9, 2, 3, 8, 6
    END

Solution

  • Below is the memory map for Cortex-M3 DesignStart Eval. Granted, this is Cortex-M rather than ATM7TDMI, but it is good as an example.

    You can see here, FLASH region, various expansion regions, peripherals and different types of RAM. You can compare this to the memory map of the LPC2140 to check the relevant addresses for your device.

    Generally, for program code, FLASH regions are read only (a special control sequence is required to modify FLASH, specific to the actual part). The areas marked as expansion in this diagram have nothing. Accesses to these should fault (from an idealistic point of view), but could also just be ignored. In DesignStart, they are regions for designers to add their own hardware. The only region that your code can reliably use for read/write storage are the RAM regions. ARM-7 does not have the clearly defined memory map of ARMv7-M, but for an LPC2140 device, you can be sure there is RAM between 0x40000000 and 0x40001FFF (as well as FLASH starting from 0x00000000).

    The ARM architecture allows you to use a code region as data (so litterals can be embedded in the code, and accessed just like variables). It does not guarantee that the code memory can be modified.

    Fig 4-1 from ARM100894_0000_00_en

    Fig 4-1 from ARM100894_0000_00_en