5 years, 1 month ago.

How to compile assembly language code offline with Mbed CLI?

Hello,

The assembly language example compiles smoothly with online compiler. But when I'm trying to compile the same project imported to local drive with Mbed CLI it reports errors:

Compile [ 99.5%]: my_asm.s
[ERROR] .\my_asm.s: Assembler messages:
.\my_asm.s:1: Error: bad instruction `area asm_func,CODE,READONLY'
.\my_asm.s:3: Error: bad instruction `export my_asm function location so that C compiler can find it and link'
.\my_asm.s:6: Error: bad instruction `export my_asm'
.\my_asm.s:7: Error: bad instruction `my_asm'
.\my_asm.s:9: Error: bad instruction `arm Assembly language function to set LED1 bit to a value passed from C'
.\my_asm.s:10: Error: bad instruction `led1 gets value(passed from C compiler in R0)'
.\my_asm.s:11: Error: bad instruction `led1 is on GPIO port 1 bit 18'
...

How can I get it compiled offline with Mbed CLI?

1 Answer

5 years, 1 month ago.

Dear Zoltan, there are two different assemblers, the ARM assembler for the online and Studio environment and the GNU assembler for the offline use. The syntax is different, therefore I write the assembler code twice and via an #ifdef I define which one to use. (PS: You can also generare assembler from C/C++ code via the -S compiler option)

Regards Helmut

Here is an example:

Assembler

#ifdef MBED_FAULT_HANDLER_DISABLED
#ifdef __ARMCC_VERSION

;********************** COPYRIGHT(c) 2017  HELIOS Software GmbH ****************
;* 
;*
;*
;*******************************************************************************
	MACRO
$L	CreateHandlerStub $Name
$Name._Handler	PROC
		push	{r7,lr}		; +r7 to keep stack 8 bytes aligned
		movs	r2, #0x04
		mov		r1, lr
		tst		r1, r2
		beq		$L.l4
		mrs		r1, psp
		b		$L.l5
$L.l4	mrs     r1, msp
		movs	r2, #0x08
		add	r1, r2
$L.l5	nop					; for reasons unknown, the online assembler fails
		adr	r0, $L.name		; without this nop
		bl	FaultError
$L.name	dcb	"$Name",0
		ALIGN
	ENDP
        EXPORT	$Name._Handler
	MEND

	AREA ||.text||, CODE, READONLY, CODEALIGN

	IMPORT FaultError
	PRESERVE8

NMI_Handler			CreateHandlerStub NMI
HardFault_Handler	CreateHandlerStub HardFault
MemManage_Handler	CreateHandlerStub MemManage
BusFault_Handler	CreateHandlerStub BusFault
UsageFault_Handler	CreateHandlerStub UsageFault

GenerateFault	PROC
	push	{r7,lr}		; +r7 to keep stack 8 bytes aligned
	movs	r0,#0
    subs	r0,#3
	movs	r1,#1
    movs	r2,#2
	movs	r3,#3
    ldr		r0,[r0, #0]
	ENDP
	EXPORT	GenerateFault
	PRESERVE8
	END

#elif defined(__GNUC__)

@
@ We define each fault handler to be a function of its own to have proper stack backtraces
@
	.macro	CreateHandlerStub Name,NameId
	.align
	.global \Name\()_Handler
	.thumb_func
	.type	\Name\()_Handler, %function
\Name\()_Handler:
	push	{r7,lr}		@ +r7 to keep stack 8 bytes aligned
	movs	r2, #0x04
	mov		r1, lr
	tst		r1, r2
	beq		4f
	mrs		r1, psp
	b		5f
4:	mrs		r1, msp
	movs	r2, #0x08
	add		r1, r2
5:
	nop
	adr		r0, 6f
	bl	FaultError
6:	.asciz	"\Name"
	.align
	.size	\Name\()_Handler, . - \Name\()_Handler
	.endm

	.section	.text
	.thumb
	.syntax unified

	CreateHandlerStub NMI
	CreateHandlerStub HardFault
	CreateHandlerStub MemManage
	CreateHandlerStub BusFault
	CreateHandlerStub UsageFault

#endif
#endif

Accepted Answer

Thank you Helmut! That also explains why I have got so many errors :-) I appreciate your tips as well. Now I can start to play with bits, bytes and registers ...

posted by Zoltan Hudak 11 Mar 2019