ARM learning notes 5 (C/C++ and ARM assembly mixed programming)

Publisher:huanxinLatest update time:2024-10-17 Source: cnblogsKeywords:arm  c++  assembly Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Common methods:
1 Embed assembly instructions in C/C++ programs
Syntax format:
__asm
​​{
assembly language program
}
2 Access global variables defined in C/C++ in assembly programs
Sample code is as follows:
test.c
#include
int gVar_1=12;
extern asmDouble(void)
int main(void){
printf("original value of gVar_1 is %d",gVar_1);
admDouble();
printf("modified value of gVar_1 is %d",gVar_1);
return 0;
}

test.s
AREA asmfile,CODE,READONLY
EXPORT asmDouble; declare global reference label
IMPORT gVar_1; reference
asmDouble
ldr r0,=gVar_1
ldr r1,[r0]
mov r2,#2
mul r3,r1,r2
str r3,[r0]
mov pc,lr
END
3 Calling assembly functions in c/c++ programs
The sample code is as follows:


test1.s
AREA asmfile,COCE,READONLY
EXPORT asm_strcpy; declare global reference label
asm_strcpy; function name
loop:
ldrb r4,[r0],#1
cmp r4,#0
beq over
strb r4,[r1],#1
b loop
over:
mov pc,lr; used for function return
END




test1.c
#include
extern void asm_strcpy(const char *src,char *dest);
int main(){
const char *s ="hello world";
char d[32];
asm_strcpy(s,d);
printf("source:%s",s);
printf("destination: %s",d);
return 0;
}
Does the above program have the flavor of jni?

4 Calling C/C++ functions in assembly programs
The sample code is as follows:
test2.c
int cFun(int a,int b,int c){
return a+b+c;
}

test2.s
EXPORT asmfile
AREA asmfile,CODE,READONLY
IMPORT cFun; reference function
ENTRY; specify application entry
mov r0,#11
mov r1,#22
mov r2,#33
BL cFun; return
END

Keywords:arm  c++  assembly Reference address:ARM learning notes 5 (C/C++ and ARM assembly mixed programming)

Previous article:ARM study notes 4 (arm pseudo-instructions)
Next article:ARM study notes six (coprocessor)

Recommended ReadingLatest update time:2024-11-23 07:54

16C84 MCU RB port interrupt program
Interrupts - Part II, Interrupt on RB Change Introduction This discussion deals with the interrupt on RB change feature. It assumes the reader is familiar with the general discussion dealing with interrupts. The PIC16C84 and most other PICs provide a feature where the program may be configured such tha
[Microcontroller]
Enflame Technology Completes RMB 1.8 Billion Series C Financing
Suiyuan Technology announced that it has completed a C round of financing of 1.8 billion yuan, led by CITIC Industry Fund, CICC Capital's fund, and Primavera Capital, and followed by Tencent, Wu Yuefeng Capital, Redpoint Ventures China Fund and many other new and old shareholders.   It is understood that in the nea
[Internet of Things]
Enflame Technology Completes RMB 1.8 Billion Series C Financing
I2C communication timing problem of 89 and 12 microcontrollers
I accidentally discovered this problem while using ADC today and would like to share it with all my friends. I2CDelay of 89: #define I2CDelay() {_nop_();_nop_();_nop_();_nop_();} I2CDelay of 12: void Delay_us(unsigned char us) {     do {         _nop_();         _nop_();         _nop_();         _nop_();      
[Microcontroller]
Design of infrared communication interface circuit controlled by 89C51 single chip microcomputer
In communication systems, non-electrical signals are often used to transmit control signals and data to achieve remote control or telemetry functions. Infrared communication is a commonly used communication method with the characteristics of simple control, convenient implementation, and high transmission reliabilit
[Microcontroller]
STM8L151C8 study notes 5: low power consumption
1. Introduction to low power mode According to the data sheet of STM8L15X, we know that the chip has 5 low power modes, as follows: 1. Wait mode: The CPU clock stops, and the selected peripherals can continue to run. Internal or external interrupts or reset signals can exit the wait mode (WFE or WFI mode). 2. Low
[Microcontroller]
STM8L151C8 study notes 5: low power consumption
Field Oriented Control of Brushless Motors Using an ARM7 Processor
Any improvement in the efficiency of motor drives will save a lot of energy, which is part of the reason for the growing interest in advanced motor control algorithms. Three-phase brushless motors mainly refer to AC induction asynchronous motors and permanent magnet synchronous motors. These motors are known for their
[Microcontroller]
Field Oriented Control of Brushless Motors Using an ARM7 Processor
ARM development board system transplantation-----u-boot compilation
This article and the other two sister articles are all intended to explain how to tailor a Linux system suitable for running on the mini2440 development board to record your own learning results. This article first introduces the software components of the embedded system, and then introduces the steps to compile a ub
[Microcontroller]
ARM development board system transplantation-----u-boot compilation
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号