__declspec(naked) int add(int a, int b)

本文介绍了一个使用__declspec(naked)修饰符定义的加法函数的实现细节,展示了如何通过直接操控寄存器来完成两个整数相加的过程。特别地,文章分析了函数不使用标准调用约定时,参数传递及栈帧管理的具体方式。
__declspec(naked) int add(int a, int b)
{
 _asm
  {
   mov eax, dword ptr [esp + 0x4]
   add eax, dword ptr [esp + 0x8]
   ret
  }
}

函数 __declspec(naked) int add(int a, int b) 生成的汇编代码

:00401BCC 8B442404  mov eax, dword ptr [esp+04]
:00401BD0 03442408  add eax, dword ptr [esp+08]
:00401BD4 C3        ret

因为函数开头没有

push ebp    
mov ebp, esp

ebp没有入栈,所以函数参数地址就是 esp + 0x4 和 esp + 0x8 了。自然函数结束也不需要 pop ebp 了。
 

Build started: Project: 11 *** Using Compiler 'V6.18', folder: 'D:\kell\core\ARM\ARMCLANG\Bin' Build target 'Target 1' start/core_cm3.c(445): error: non-ASM statement in naked function is not supported uint32_t result=0; ^ start/core_cm3.c(442): note: attribute is here uint32_t __get_PSP(void) __attribute__( ( naked ) ); ^ start/core_cm3.c(465): error: parameter references not allowed in naked functions "BX lr \n\t" : : "r" (topOfProcStack) ); ^ start/core_cm3.c(461): note: attribute is here void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); ^ start/core_cm3.c(479): error: non-ASM statement in naked function is not supported uint32_t result=0; ^ start/core_cm3.c(476): note: attribute is here uint32_t __get_MSP(void) __attribute__( ( naked ) ); ^ start/core_cm3.c(499): error: parameter references not allowed in naked functions "BX lr \n\t" : : "r" (topOfMainStack) ); ^ start/core_cm3.c(495): note: attribute is here void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); ^ start/core_cm3.c(509): warning: no previous prototype for function '__get_BASEPRI' [-Wmissing-prototypes] uint32_t __get_BASEPRI(void) ^ start/core_cm3.c(509): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __get_BASEPRI(void) ^ static start/core_cm3.c(524): warning: no previous prototype for function '__set_BASEPRI' [-Wmissing-prototypes] void __set_BASEPRI(uint32_t value) ^ start/core_cm3.c(524): note: declare 'static' if the function is not intended to be used outside of this translation unit void __set_BASEPRI(uint32_t value) ^ static start/core_cm3.c(536): warning: no previous prototype for function '__get_PRIMASK' [-Wmissing-prototypes] uint32_t __get_PRIMASK(void) ^ start/core_cm3.c(536): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __get_PRIMASK(void) ^ static start/core_cm3.c(551): warning: no previous prototype for function '__set_PRIMASK' [-Wmissing-prototypes] void __set_PRIMASK(uint32_t priMask) ^ start/core_cm3.c(551): note: declare 'static' if the function is not intended to be used outside of this translation unit void __set_PRIMASK(uint32_t priMask) ^ static start/core_cm3.c(563): warning: no previous prototype for function '__get_FAULTMASK' [-Wmissing-prototypes] uint32_t __get_FAULTMASK(void) ^ start/core_cm3.c(563): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __get_FAULTMASK(void) ^ static start/core_cm3.c(578): warning: no previous prototype for function '__set_FAULTMASK' [-Wmissing-prototypes] void __set_FAULTMASK(uint32_t faultMask) ^ start/core_cm3.c(578): note: declare 'static' if the function is not intended to be used outside of this translation unit void __set_FAULTMASK(uint32_t faultMask) ^ static start/core_cm3.c(590): warning: no previous prototype for function '__get_CONTROL' [-Wmissing-prototypes] uint32_t __get_CONTROL(void) ^ start/core_cm3.c(590): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __get_CONTROL(void) ^ static start/core_cm3.c(605): warning: no previous prototype for function '__set_CONTROL' [-Wmissing-prototypes] void __set_CONTROL(uint32_t control) ^ start/core_cm3.c(605): note: declare 'static' if the function is not intended to be used outside of this translation unit void __set_CONTROL(uint32_t control) ^ static start/core_cm3.c(619): warning: no previous prototype for function '__REV' [-Wmissing-prototypes] uint32_t __REV(uint32_t value) ^ start/core_cm3.c(619): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __REV(uint32_t value) ^ static start/core_cm3.c(635): warning: no previous prototype for function '__REV16' [-Wmissing-prototypes] uint32_t __REV16(uint16_t value) ^ start/core_cm3.c(635): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __REV16(uint16_t value) ^ static start/core_cm3.c(656): warning: implicit conversion changes signedness: 'uint32_t' (aka 'unsigned int') to 'int32_t' (aka 'int') [-Wsign-conversion] return(result); ~~~~~~ ^~~~~~ start/core_cm3.c(651): warning: no previous prototype for function '__REVSH' [-Wmissing-prototypes] int32_t __REVSH(int16_t value) ^ start/core_cm3.c(651): note: declare 'static' if the function is not intended to be used outside of this translation unit int32_t __REVSH(int16_t value) ^ static start/core_cm3.c(667): warning: no previous prototype for function '__RBIT' [-Wmissing-prototypes] uint32_t __RBIT(uint32_t value) ^ start/core_cm3.c(667): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __RBIT(uint32_t value) ^ static start/core_cm3.c(683): warning: no previous prototype for function '__LDREXB' [-Wmissing-prototypes] uint8_t __LDREXB(uint8_t *addr) ^ start/core_cm3.c(683): note: declare 'static' if the function is not intended to be used outside of this translation unit uint8_t __LDREXB(uint8_t *addr) ^ static start/core_cm3.c(699): warning: no previous prototype for function '__LDREXH' [-Wmissing-prototypes] uint16_t __LDREXH(uint16_t *addr) ^ start/core_cm3.c(699): note: declare 'static' if the function is not intended to be used outside of this translation unit uint16_t __LDREXH(uint16_t *addr) ^ static start/core_cm3.c(715): warning: no previous prototype for function '__LDREXW' [-Wmissing-prototypes] uint32_t __LDREXW(uint32_t *addr) ^ start/core_cm3.c(715): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __LDREXW(uint32_t *addr) ^ static start/core_cm3.c(732): warning: no previous prototype for function '__STREXB' [-Wmissing-prototypes] uint32_t __STREXB(uint8_t value, uint8_t *addr) ^ start/core_cm3.c(732): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __STREXB(uint8_t value, uint8_t *addr) ^ static start/core_cm3.c(749): warning: no previous prototype for function '__STREXH' [-Wmissing-prototypes] uint32_t __STREXH(uint16_t value, uint16_t *addr) ^ start/core_cm3.c(749): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __STREXH(uint16_t value, uint16_t *addr) ^ static start/core_cm3.c(766): warning: no previous prototype for function '__STREXW' [-Wmissing-prototypes] uint32_t __STREXW(uint32_t value, uint32_t *addr) ^ start/core_cm3.c(766): note: declare 'static' if the function is not intended to be used outside of this translation unit uint32_t __STREXW(uint32_t value, uint32_t *addr) ^ static 19 warnings and 4 errors generated. compiling core_cm3.c... start/system_stm32f10x.c(65): warning: In file included from... ./start/stm32f10x.h(478): warning: In file included from... ./start/core_cm3.h(1204): error: unknown type name 'inline' static __INLINE void __enable_irq() { __ASM volatile ("cpsie i"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1205): error: unknown type name 'inline' static __INLINE void __disable_irq() { __ASM volatile ("cpsid i"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1207): error: unknown type name 'inline' static __INLINE void __enable_fault_irq() { __ASM volatile ("cpsie f"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1208): error: unknown type name 'inline' static __INLINE void __disable_fault_irq() { __ASM volatile ("cpsid f"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1210): error: unknown type name 'inline' static __INLINE void __NOP() { __ASM volatile ("nop"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1211): error: unknown type name 'inline' static __INLINE void __WFI() { __ASM volatile ("wfi"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1212): error: unknown type name 'inline' static __INLINE void __WFE() { __ASM volatile ("wfe"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1213): error: unknown type name 'inline' static __INLINE void __SEV() { __ASM volatile ("sev"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1214): error: unknown type name 'inline' static __INLINE void __ISB() { __ASM volatile ("isb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1215): error: unknown type name 'inline' static __INLINE void __DSB() { __ASM volatile ("dsb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1216): error: unknown type name 'inline' static __INLINE void __DMB() { __ASM volatile ("dmb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1217): error: unknown type name 'inline' static __INLINE void __CLREX() { __ASM volatile ("clrex"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1468): error: unknown type name 'inline' static __INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1489): error: unknown type name 'inline' static __INLINE uint32_t NVIC_GetPriorityGrouping(void) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1489): error: expected ';' after top level declarator static __INLINE uint32_t NVIC_GetPriorityGrouping(void) ^ ; ./start/core_cm3.h(1756): error: unknown type name 'inline' static __INLINE uint32_t ITM_SendChar (uint32_t ch) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1756): error: expected ';' after top level declarator static __INLINE uint32_t ITM_SendChar (uint32_t ch) ^ ; start/system_stm32f10x.c(162): warning: no previous extern declaration for non-static variable 'SystemCoreClock' [-Wmissing-variable-declarations] uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */ ^ start/system_stm32f10x.c(162): note: declare 'static' if the variable is not intended to be used outside of this translation unit uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */ ^ start/system_stm32f10x.c(167): warning: no previous extern declaration for non-static variable 'AHBPrescTable' [-Wmissing-variable-declarations] __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; ^ start/system_stm32f10x.c(167): note: declare 'static' if the variable is not intended to be used outside of this translation unit __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; ^ 2 warnings and 17 errors generated. compiling system_stm32f10x.c... user/main.c(1): warning: In file included from... ./start\stm32f10x.h(478): warning: In file included from... ./start/core_cm3.h(1204): error: unknown type name 'inline' static __INLINE void __enable_irq() { __ASM volatile ("cpsie i"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1205): error: unknown type name 'inline' static __INLINE void __disable_irq() { __ASM volatile ("cpsid i"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1207): error: unknown type name 'inline' static __INLINE void __enable_fault_irq() { __ASM volatile ("cpsie f"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1208): error: unknown type name 'inline' static __INLINE void __disable_fault_irq() { __ASM volatile ("cpsid f"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1210): error: unknown type name 'inline' static __INLINE void __NOP() { __ASM volatile ("nop"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1211): error: unknown type name 'inline' static __INLINE void __WFI() { __ASM volatile ("wfi"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1212): error: unknown type name 'inline' static __INLINE void __WFE() { __ASM volatile ("wfe"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1213): error: unknown type name 'inline' static __INLINE void __SEV() { __ASM volatile ("sev"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1214): error: unknown type name 'inline' static __INLINE void __ISB() { __ASM volatile ("isb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1215): error: unknown type name 'inline' static __INLINE void __DSB() { __ASM volatile ("dsb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1216): error: unknown type name 'inline' static __INLINE void __DMB() { __ASM volatile ("dmb"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1217): error: unknown type name 'inline' static __INLINE void __CLREX() { __ASM volatile ("clrex"); } ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1468): error: unknown type name 'inline' static __INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1489): error: unknown type name 'inline' static __INLINE uint32_t NVIC_GetPriorityGrouping(void) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1489): error: expected ';' after top level declarator static __INLINE uint32_t NVIC_GetPriorityGrouping(void) ^ ; ./start/core_cm3.h(1756): error: unknown type name 'inline' static __INLINE uint32_t ITM_SendChar (uint32_t ch) ^ ./start/core_cm3.h(751): note: expanded from macro '__INLINE' #define __INLINE inline /*!< inline keyword for GNU Compiler */ ^ ./start/core_cm3.h(1756): error: expected ';' after top level declarator static __INLINE uint32_t ITM_SendChar (uint32_t ch) ^ ; 17 errors generated. compiling main.c... 阅读该错误报告并给出修改建议
最新发布
09-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值