Calling C library function printf in arm assembly language

Publisher:Xingfu6666Latest update time:2016-07-20 Source: eefocusKeywords:arm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Today, when I was writing an ARM assembly code, I wanted to display string information in the program, but it didn't work. I thought it was because of the implementation of printf in the C library. I finally solved the problem in the afternoon. It turned out that the C library was not initialized in the assembly program.

When I encountered this problem, for the sake of simplicity, I directly used assembly to write a program that called the printf function to display the string hello, as follows:

    area |hello$code|,code,readonly
    code32
    entry
    import _printf 
    import ||Lib
Request
armlib||,weak
    
hello
    adr r0,strhello
    bl _printf

strhello
    dcb "hello!/n/0"
    end

As a result, during debugging, although the _printf function can be called, the string cannot be output in the console. But later I rewrote this section of assembly code into a procedure (removed entry, added registers and lr protection at the entry, and restored the protected registers before returning), and then added a C original program (main), called this procedure in main, and found that the string can be output in the console.
So I checked some information (Google) and finally found the problem. It turned out that the C library was not initialized in the assembly code. So I changed the assembly code to the following:
    area |.text|,code,readonly
    code32
    export main; Export main, main function,
    import _printf
    import __main; C Library Entry
    import ||Lib
Request
armlib||,weak
    
main
    stmfd sp!,{lr}
    adr r0,strhello
    bl _printf
    
    ldmfd sp!,{pc}

strhello
    dcb "hello!/n/0"
  
    end
The debugging is successful, and the string information can be successfully output to the console.

This program has a main function main, so the main symbol needs to be exported. In addition, the label __main (import __main) needs to be introduced to represent the beginning of C library initialization; because the _printf function is called in the program and the ARM C library is used, it is necessary to import |Lib$Request$armlib|. The weak at the end indicates that if the label of this line cannot be found, no connection error will be reported.
The meaning of |.text|: It indicates the code segment generated by the C compiler, or the code segment used to be associated with the C library in some way. Here it refers to the latter.

References:
1. ARM assembly optimization, http://blog.csdn.net/guanchanghui/archive/2007/06/27/1669513.aspx
2. RealView Compiler Tool 2.0 Assembler Guide, http://infocenter.arm.com/help/index.jsp
Keywords:arm Reference address:Calling C library function printf in arm assembly language

Previous article:arm-linux connection and connection script
Next article:ARM Procedure Call Standard

Recommended ReadingLatest update time:2024-11-16 07:56

[JZ2440] Install arm-linux-gcc cross-compiler
1. Acquisition of relevant information   Personally, when I was learning the mini2440 development board, I followed teacher Wei Dongshan’s videos. The videos and information can be obtained from the Baiwen.com forum.   Baiwen.com forum link: http://www.100ask.net/bbs/forum.php   After downloading the information
[Microcontroller]
Intel's foundry business and Arm announce a collaboration involving multi-generation cutting-edge system chip design
This cooperation will bring chip designers a powerful combination of Arm cores and Intel's Ami-era process technology. Santa Clara, California and Cambridge, UK, April 12, 2023 – Intel Foundry Services (IFS) and Arm today announced the signing of a multi-generation cutting-edge system-on-chip design. The agreement a
[Embedded]
Getting the ARM instruction set right (Part 1) --- Data storage and loading instructions
ARM instructions belong to Reduced Scalability Computer Instructions (RISC), which are numerous and difficult to remember. However, the development of things is continuous, especially natural science, which is a kind of accumulation. Therefore, their development must be based on certain rules! After mastering and bein
[Microcontroller]
GigaDevice and Arm Technology deepen technical cooperation to jointly create opportunities for Arm MCU innovation
Beijing, China (August 15, 2024) - GigaDevice Technology Group Co., Ltd. (hereinafter referred to as "GigaDevice"), an industry-leading semiconductor device supplier, and Arm Technology (China) Co., Ltd. (hereinafter referred to as "Arm Technology") jointly announced that the two parties will further strengthen
[Embedded]
GigaDevice and Arm Technology deepen technical cooperation to jointly create opportunities for Arm MCU innovation
PC in ARM and PC in AXD
R15 (PC) always points to the instruction being "fetched", not the instruction being "executed" or "decoded". Generally speaking, people usually agree to use the "instruction being executed as the reference point", which is called the first instruction at the moment, so PC always points to the third instruction. W
[Microcontroller]
ARM chip development (S5PV210 chip) - timer, watchdog, RTC
1. Counter The counter increases its count value by one at fixed intervals, so we can calculate the time based on the count value: elapsed time = count value x counting time interval. 2. Timer 2.1、Timer Introduction The timer has a timing function, similar to the countdown function of our mobile phone. For example,
[Microcontroller]
ARM chip development (S5PV210 chip) - timer, watchdog, RTC
Japanese chip manufacturer Socionext announced that it will join forces with TSMC to develop 2nm ARM processors
According to news on October 27, Socionext is the only listed company in Japan responsible for customizing SoC chips. This company claims to "only develop and produce chips after receiving orders." Its operating model is essentially different from Nvidia and AMD. However, the company has now announced that it is teami
[Semiconductor design/manufacturing]
Japanese chip manufacturer Socionext announced that it will join forces with TSMC to develop 2nm ARM processors
ARM AVI video playback based on 2440 freambuffer drawing
AVI file format diagram tidy: AVI is a container that can hold data in any format. Video encoding: The format can be H264, MPEG-4, MJPEG. Since MJPEG is relatively simple, only MJPEG avi videos are supported. Audio encoding: PCM16, mp3 Because PCM encoding, the file is too large, so onl
[Microcontroller]
ARM AVI video playback based on 2440 freambuffer drawing
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号