cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 Discovery Kit - Call assembly file/function in C code

mehrab94
Associate
Posted on August 23, 2016 at 17:42

Hello, 

I looking for some example code for call one function in a assembly file from/in C code. Can someone help me with it? Sorry, I'm beginner in this branch...

Thank you very much. 

#c #programming #assembly
3 REPLIES 3
Posted on August 23, 2016 at 19:06

This is for an F4 in Keil, but is illustrative, it is not complicated.

;*****************************************************************************
; In C code
; Prototype as
; extern void SystemReset(void);
; Call as
; SystemReset()
SystemReset PROC
EXPORT SystemReset
ldr r1, =0xE000ED0C ; NVIC Application Interrupt and Controller
ldr r0, =0x05FA0004 ; Magic
str r0, [r1, #0] ; Reset
b .
bx lr
ENDP ; SystemReset
;*****************************************************************************
; In C code
; Prototype as
; extern void KickWatchdog(void);
; Call as
; KickWatchdog()
KickWatchdog PROC
EXPORT KickWatchdog
ldr r0, =0x40003000 ; IWDG
movw r1, #0xAAAA
str r1, [r0, #0] ; IWDG_KR Reload / Kick
bx lr
ENDP ; KickWatchdog sourcer32@gmail.com
;*****************************************************************************
; In C code
; Prototype as
; extern void InitWatchdog(void);
; Call as
; InitWatchdog()
InitWatchdog PROC
EXPORT InitWatchdog
ldr r0, =0x40003000 ; IWDG
movw r1, #0x5555
str r1, [r0, #0] ; IWDG_KR Enable Access
movw r1, #6 ; /256
str r1, [r0, #4] ; IWD_PR Prescaler Register
movw r1, #0xFFF ; Reload value, reset default, writing for clarity, 768s
str r1, [r0, #8] ; IWD_RLR Reload Register
movw r1, #0xAAAA
str r1, [r0, #0] ; IWDG_KR Reload
movw r1, #0xCCCC
str r1, [r0, #0] ; IWDG_KR Enable
bx lr
ENDP ; InitWatchdog
;*****************************************************************************

Assembler threads

/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Assembly%20Code%20Example&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=8665

;*****************************************************************************
; In C code
; Prototype as
; extern int foo(int, int, int);
; Call as
; foo(x, y, z)
foo PROC
EXPORT foo
; Calling foo(x, y, z); Will enter foo with R0=x, R1=y, R2=z
; returns R0
ldr r0, =12345 ; return(12345)
bx lr
ENDP ; foo sourcer32@gmail.com
;*****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mehrab94
Associate
Posted on August 30, 2016 at 20:56

I thank you and appreciate for your immediate response.

That helped me a lot.

Thank you very much for your time.