Assembler Trouble - LPC1343

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assembler Trouble - LPC1343

873 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Risto on Sun Oct 03 11:22:42 MST 2010
[SIZE=4]Hello there,[/SIZE]
[SIZE=4]I'm having some trouble with LPCxpresso and debugging a few lines of assembler code. (I want to use assembly for some educational puposes to get familar with Cortex M3 Instruction Set)[/SIZE]

[SIZE=4]What I've done:[/SIZE]

[SIZE=4]- Started LPCxpreso IDE and created a new Project "assembly_trouble" for LPC1343[/SIZE]

[SIZE=4]- under folder /src deleted cr_startup_lpc13.c and main.c[/SIZE]

[SIZE=4]- created a new file assembly.asm und /src folder[/SIZE]

[SIZE=4]- filled assembly.asm with the following (experimental) assembler code:[/SIZE]

[SIZE=2].equ STACK_TOP, 0x10002000[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].text[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2].syntax unified[/SIZE]
[SIZE=2].thumb[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].global[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] _start[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].type[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] start, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]%function[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]_start:[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].word[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] STACK_TOP, start[/SIZE][/LEFT]

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]start:[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2]movs r0, #20[/SIZE]
[SIZE=2]movs r1, #40[/SIZE]
[SIZE=2]PUSH {r0}[/SIZE]
[SIZE=2]PUSH {r1}[/SIZE]
[SIZE=2]adds r0, r1[/SIZE]
[SIZE=2]movs r1, r0[/SIZE]
[SIZE=2]POP {r1}[/SIZE]
[SIZE=2]POP {r0}[/SIZE]
[SIZE=2]movs r0, #10[/SIZE]
[SIZE=2]movs r1, #0[/SIZE][/LEFT]

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]loop:[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2]adds r1, r0[/SIZE]
[SIZE=2]subs r0, #1[/SIZE]
[SIZE=2]bne loop[/SIZE][/LEFT]

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]deadloop:[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2]b deadloop[/SIZE]
[SIZE=2].end[/SIZE][/LEFT]


[SIZE=4]- Under "Properties for assembler_trouble" -> C/C++ Build -> Settings -> MCU Linker -> Target I've unchecked "Manage Linker script".[/SIZE]

[SIZE=4]- Build 'assembly_trouble' [Debug][/SIZE]
[SIZE=4]Console Messages:[/SIZE]
[LEFT][SIZE=2]**** Build of configuration Debug for project assembly_trouble ****[/SIZE][/LEFT]

[LEFT][SIZE=2]make all [/SIZE]
[SIZE=2]Building file: ../src/assembly.asm[/SIZE]
[SIZE=2]Invoking: MCU Assembler[/SIZE]
[SIZE=2]arm-none-eabi-gcc -c -x assembler-with-cpp -D__REDLIB__ -DDEBUG -D__CODE_RED -g3 -mcpu=cortex-m3 -mthumb -o"src/assembly.o" "../src/assembly.asm"[/SIZE]
[SIZE=2]Finished building: ../src/assembly.asm[/SIZE][/LEFT]

[LEFT][SIZE=2]Building target: assembly_trouble.axf[/SIZE]
[SIZE=2]Invoking: MCU Linker[/SIZE]
[SIZE=2]arm-none-eabi-gcc -nostdlib -Xlinker -Map=assembly_trouble.map -Xlinker --gc-sections -mcpu=cortex-m3 -mthumb -o"assembly_trouble.axf" ./src/assembly.o [/SIZE]
[SIZE=2]Finished building target: assembly_trouble.axf[/SIZE][/LEFT]

[LEFT][SIZE=2]make --no-print-directory post-build[/SIZE]
[SIZE=2]Performing post-build steps[/SIZE]
[SIZE=2]arm-none-eabi-size assembly_trouble.axf; # arm-none-eabi-objcopy -O binary assembly_trouble.axf assembly_trouble.bin ; checksum -p LPC1343 -d assembly_trouble.bin;[/SIZE]
[SIZE=2]text data bss dec hex filename[/SIZE][/LEFT]
[SIZE=2]36 0 0 36 24 assembly_trouble.axf[/SIZE]


[SIZE=4]- Added Breakpoint to movs r0, #20 right after the start lable[/SIZE]

[SIZE=4]- Unchecked "Stop on startup at:" in Debug Configurations[/SIZE]

[SIZE=4]- Debug 'assembly_trouble' [Debug][/SIZE]

[SIZE=4]Now the Trouble begins when starting to debug. Have a look at Disassembly while debugging:[/SIZE]

[LEFT][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]movs r0, #20[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008008 <start>: movs r0, #10[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]movs r1, #40[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x0000800a <start+2>: movs r1, #0[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]PUSH {r0}[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x0000800c <start+4>: adds r1, r1, r0[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]PUSH {r1}[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x0000800e <start+6>: subs r0, #1[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]adds r0, r1[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008010 <start+8>: bne.n 0x800c <start+4>[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]movs r1, r0[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008012 <start+10>: b.n 0x8012 <start+10>[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]POP {r1}[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008014 <start+12>: ; <UNDEFINED> instruction: 0xffffffff[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]PUSH {r0}[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008016 <start+14>: ; <UNDEFINED> instruction: 0xffffffff[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]movs r0, #10[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x00008018 <start+16>: ; <UNDEFINED> instruction: 0xffffffff[/SIZE]
[SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]movs r1, #0[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]0x0000801a <start+18>: ; <UNDEFINED> instruction: 0xffffffff[/SIZE][/LEFT]

[LEFT][SIZE=4]Code actually executed is not correct. Whats wrong in here ? [/SIZE]
[SIZE=4]I've attached my LPC Xpresso Project as ZIP File[/SIZE][/LEFT]

[LEFT][SIZE=2][SIZE=4]Risto[/SIZE][/SIZE][/LEFT]
0 Kudos
Reply
2 Replies

852 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Sun Oct 03 23:58:55 MST 2010
I think the key issue you have is that you have disabled the managed link script mechanism, but you have not then substituted in your own link script.

This causes the linker to use a built in linker script, which causes your code to be built for address 0x8000 (this default address comes from long ago in ARM history). This address is above the top of the flash in your LPC1343, and thus basically nothing is getting put into the flash - but the debugger does still appear to be loading your debug symbols.

Thus you basically need to provide a simple linker script that places your code at address 0x0:

     SECTIONS
     {
       . = 0x00000;
       .text : { KEEP(*(.text*)) }
       . = 0x10000000;
       .data : { *(.data) }
       .bss : { *(.bss) }
     }
A good starting place for more information on linker scripts would be:

http://support.code-red-tech.com/CodeRedWiki/OwnLinkScripts

Having correctly placved your code to start at address zero, you will then have another issue - your code will then be placed over the exception vectors. This is not a problem itself, except that NXP have defined a special meaning to the exception vector #7. To quote the User Manual:


Quote:

The reserved ARM Cortex-M3 exception vector location 7 (offset 0x0000 001C in the vector table) should contain the 2&#8217;s complement of the check-sum of table entries 0 through 6. This causes the checksum of the first 8 table entries to be 0. The bootloader code checksums the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is transferred to the user code.

When dowloading the code with the debugger, it automatically calculates the checksum and places into the correct location (it must do this, else your code would not get to run). Unfortunately, this is right in the middle of your code, due to where you have placed it!
To correct it, the simplest thing to do would be to reserve the exception handler space in your code so that "start:" is placed after this reserved location.e.g.

_start: 
.word STACK_TOP, start
.word 0,0,0,0,0,0
_start:
Regards,
CodeRedSupport
0 Kudos
Reply

852 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Sun Oct 03 23:57:14 MST 2010
HI,

The issue is that you have placed your code over the exception vectors. This is not a problem itself, except that NXP have defined a special meaning to the exception vector #7. To quote the User Manual:


Quote:

The reserved ARM Cortex-M3 exception vector location 7 (offset 0x0000 001C in the vector table) should contain the 2’s complement of the check-sum of table entries 0 through 6. This causes the checksum of the first 8 table entries to be 0. The bootloader code checksums the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is transferred to the user code

When dowloading the code with the debugger, it automatically calculates the checksum and places into the correct location (it must do this, else your code would not get to run). Unfortunately, this is right in the middle of your code, due to where you have placed it!

To correct it, the simplest thing to do would be to reserve the exception handler space in your code so that "start:" is placed after this reserved location.e.g.
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]_start:[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].word[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] STACK_TOP, start[/SIZE][/LEFT]
 [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055].word[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0,0,0,0,0,0
[/SIZE]
[SIZE=2]
[/SIZE]
0 Kudos
Reply