TQ2440 Development Board Learning Record (2)---set stack and call C function __ function

Source: Internet
Author: User
Tags code tag

This article is based on the previous TQ2440 Development Board learning Documentary (1)-The simplest independent running assembler evolved. 0 arm Stack (stack) Basics

ARM supports a full minus type stack, meaning that the stack pointer sp (that is, R13) always points to the data unit that just entered the stack. The corresponding to the full is dissatisfaction, this type of stack pointer sp points to the next blank cell at the top of the stack. minus means that the stack at the bottom of the memory address is large, the top address of the stack, when pressed into the data, the stack to address the direction of small expansion.

At present we only have 4KB of memory available, the range is: 0X00000000-0X00000FFF, the following figure:

To make the most of this 4KB of RAM, we put the stack
The pointer SP initially points to 0x00001000. This setting has the following considerations: Must be 4 bytes aligned, this is arm's request. That is, the last two-digit bits must be 0. Although the maximum available memory address is 0X00000FFF, sp=0x00001000 can still be set here. Because the arm stack is "full minus", the 0x00001000 is not used for subsequent use.

C Language of the implementation of functions, heavily dependent on the stack, although fewer parameters, the direct use of register arguments, but the call function before its return address must be stored on the stack on the call function returned to use. 1 Project Source code

From the beginning of this article, the project source code is no longer fully listed in the text, the need for friends, please go to Csdn's git library to download. This time the source code tag for v0.1.

Set stack code on one line:

/* Set STATCK, must is aligned by 4 bytes/
    Ldr sp, =0x00001000

Calling the C function is also very simple

b Main

Note that BL is not used here, which means that the following code will never be executed.

What needs to be explained is that s3c2440 has several running modes, different modes have their own stack pointer registers, which need to be set separately. At the moment we're only working in the default SVC mode, and we're just setting up the stack in that mode.

With the stack, C language functions can be implemented smoothly, with the C language, who will use the assembly. So the back is written in C language of a LED water lamp program.

void Main (void)
{
    led_init ();

    while (1) {
        int i=0;
        For (I=1 i<=4; i++) {
            Led_off (I-1 < 1? 4:i-1);
            LED_ON (i);
            Delay (10000);}}

2 Compiling Instructions

See Makefile file for details. 3 Operating Instructions

Take part in the TQ2440 Development Board Study (1)-The simplest description of an independent running assembler.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.