S3C2440-Bare Metal Edition-04 | ARM-THUMB Subroutine Calling Rules ATPCS

Publisher:SerendipitySoulLatest update time:2021-07-14 Source: eefocusKeywords:S3C2440 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the GPIO experiment, we first wrote an assembly program to operate the register to light up the LED. However, the readability and portability of assembly language are too poor, so we wrote the startup code, set the stack pointer SP, and then called the main function in C language to enter the world of C language. The C language accessed the control register and lit up the LED. The readability and portability of the program were greatly improved. So, have we ever thought about how to call the C language entry function main in assembly language?


In fact, for ARM processors, the rules for subroutine calls are formulated in the ARM instruction set assembler and the THUMB instruction set assembler - the ATPCS rules, which include:

  • Register Usage Rules

  • Data stack usage rules

  • Parameter passing rules

1. Register usage rules

There are 16 registers R0-R15 in the ARM processor. Each register has a usage specified by the ATPCS rules and an alias specified according to its usage, as shown in the following table:

registerAliasesuse
R15pcProgram Counter
R141.Link register (used to store subroutine return address)
R13spData stack pointer (pointing to the top of the stack)
R12ipThe scratch register for the subroutine call
R11v8ARM state local variable register 8
R10v7ARM state local variable register 7
R9v6ARM state local variable register 6
R8v5ARM state local variable register 5
R7v4ARM state local variable register 4
R6v3ARM state local variable register 3
R5v2ARM state local variable register 2
R4v1ARM state local variable register 1
R3a4Parameter/result/scratch register 4
R2a3Parameter/result/scratch register 3
R1a2Parameter/result/scratch register 2
R0a1Parameter/result/scratch register 1

Summarized as follows:

  • Parameters are passed and results are returned between subroutines through registers R0-R3;

  • In the subroutine, local variables are saved through registers R4-R11;

  • Register R12 is used as the scratch register between subroutines;

  • Register R13 is used as a data stack pointer, pointing to the top of the stack;

  • Register R14 is used as a link register to store the return address of the subroutine;

  • Register R15 is used as the program counter;


2. Data stack usage rules

ATPCS stipulates that the data stack is of FD type (Full Descending), that is, the stack pointer points to the top element of the stack and grows in the direction of decreasing memory address. During operation, the data stack is 8-byte aligned, and the stmdb/ldmia batch memory access instructions are used to operate the FD data stack.


The FD type data stack operates specifically as follows:

  • When saving content, first decrement the SP pointer and then save the data;

  • When restoring data, first obtain the data and then increment the SP pointer;

3. Parameter passing rules

  • When calling a function, if there are no more than 4 parameters, they are passed in sequence using R0-R3. If there are more than 4 parameters, the remaining parameters are passed through the data stack.

  • When a function returns a result, it is passed in sequence using R0-R3;

Experiment - Experiment on passing parameters when calling functions in assembly

1. Purpose

Calling functions in assembly language and passing parameters.


2. Experimental content

The main function defines the parameters. If the passed parameter is 1, the first LED is turned on. If the passed parameter is 2, the second LED is turned on.


3. Experimental Code

3.1. Startup Code

@ brief: S3C2440 startup file

@ author: mculover666


.text

.global _start


_start:

@ Disable watchdog

LDR R0,=0x53000000

MOV R1,#0

STR R1,[R0]


@ Set the stack top pointer SP (start from Nand)

LDR SP,=4096


@ Call led_on with parameter 1 to turn on the first LED

LDR R0,=1

BL led_on


@ Pass parameter 100000, call delay, delay

LDR R0,=100000

BL delay


@ Call led_on with parameter 2 to turn on the second LED

LDR R0,=2

BL led_on


@ Program Pause

halt:

B halt


3.2.C code

void delay(volatile int xms)

{

while(xms--);

}


int led_on(int led)

{


if(led == 1)

{

/* Set the GPFCON register to configure the GPF4 pin as output mode*/

*(unsigned int *)0x56000050 &= ~(3<<(2*4));

*(unsigned int *)0x56000050 |= 1<<(2*4);


/* Set the GPF DAT register, GPF4 outputs low level, and lights up the LED */

*(unsigned int *)0x56000054 &= ~(1<<4);

}

else if(led == 2)

{

/* Set the GPFCON register to configure the GPF4 pin as output mode*/

*(unsigned int *)0x56000050 &= ~(3<<(2*5));

*(unsigned int *)0x56000050 |= 1<<(2*5);


/* Set the GPF DAT register, GPF4 outputs low level, and lights up the LED */

*(unsigned int *)0x56000054 &= ~(1<<5);

}


return 0;

}


3.3. Compilation

TARGET = led_blink


CFLAGS = -Wall # Output all warnings


$(TARGET).bin:$(TARGET).elf

arm-linux-objcopy -O binary -S $(TARGET).elf $(TARGET).bin


#Note: The startup file must be linked first

$(TARGET).elf:start.o $(TARGET).o

arm-linux-ld -Ttext 0 start.o $(TARGET).o -o $(TARGET).elf


$(TARGET).o:$(TARGET).c

arm-linux-gcc -c $(TARGET).c $(CFLAGS) -o $(TARGET).o

start.o:start.s

arm-linux-gcc -c start.s $(CFLAGS) -o start.o


clean:

rm -rf *.o *.elf *.bin


download_to_nand:

#Download to nand flash

oflash 0 1 0 0 0 $(TARGET).bin


4. Download and run

The program starts running and the first LED lights up:

After a delay of about 1s, the second LED is also lit:

5. Experimental Summary

Through this experiment, we have mastered the use of ATPCS rules in actual development. When calling the main function, we use the R0 register to pass parameters, which is summarized as follows:

  • The subroutine calling rules in ARM processors are formulated by ATPCS, including register usage rules, data stack usage rules, and parameter passing rules;

  • R0-R3 can pass parameters/results, R4-R11 can store local variables, R13 is the data stack pointer SP, and R14 stores the subroutine return address;

  • The data stack in ATPCS is of FD type, i.e. full-decreasing type;

  • In ATPCS, parameters are passed in sequence using R0-R3. If there are more than 4 parameters, the rest are passed using the data stack.


Keywords:S3C2440 Reference address:S3C2440-Bare Metal Edition-04 | ARM-THUMB Subroutine Calling Rules ATPCS

Previous article:S3C2440-Bare Metal Edition-03 | Use of GPIO (lighting up LEDs, key detection)
Next article:S3C2440-Bare Metal Edition-05 | Detailed Explanation of S3C2440 Clock System (FCLK, PCLK, HCLK)

Recommended ReadingLatest update time:2024-11-16 11:49

Porting u-boot-2010.09 to S3C2440 (VI) - Calculation of SDRAM address and capacity
For the GEC2410 development board, the physical start address of SDRAM is 0x30000000, the end address is 0x34000000, and the size is 64Mbytes. I have a question? Why is the physical start address of SDRAM 0x30000000 and the end address 0x34000000, and the resulting size is 64Mbytes? Because the capacity is display
[Microcontroller]
S3C2440-Bare Metal Edition-03 | Use of GPIO (lighting up LEDs, key detection)
Experiment 1 - Lighting up an LED  1. Look at the schematic to determine how the hardware is connected The schematic diagram shows the hardware circuit of the chip controlling the LED and how the chip pins are connected to the LED. 2. Check the main chip manual to determine how to control the pins Specific: How to
[Microcontroller]
S3C2440-Bare Metal Edition-03 | Use of GPIO (lighting up LEDs, key detection)
S3C2440 Linux driver transplantation - button
Development board: TQ2440 Kernel version: 2.6.32 1. Hardware connection diagram    Four input pins:                             EINT0-----( GPF0  )----INPUT---K4                             EINT2-----( GPF2  )----INPUT---K3                             EINT4-----( GPF4  )----INPUT---K2                             EI
[Microcontroller]
S3C2440 Linux driver transplantation - button
ARM9_S3C2440 Learning (II) Register Organization in ARM State
The register organization in ARM state is shown in Figure 2.3. ARM has 37 32-bit registers, including 31 general registers, 1 current program status register CPSR (current program status register), and 5 backup program status registers SPSR (saved program status register). These 37 registers are not all visible at the
[Microcontroller]
Problems encountered when debugging nandflash bare metal program on s3c2440
According to the previous sdram code, the startup code turns off the watchdog, initializes the storage controller (mainly Norflash of BANK0 and SDRAM of BANK6), sets the stack to the highest address of SDRAM, and takes the data of the text segment directly from Norflash. The code is as follows:
[Microcontroller]
S3C2440 bare metal -------SPI_FLASH programming
1.spi_flash.h   #ifndef _SPI_FLASH_H #define _SPI_FLASH_H   void SPIFlashReadID(int *pMID, int *pDID); void SPIFlashInit(void); void SPIFlashEraseSector(unsigned int addr); void SPIFlashProgram(unsigned int addr, unsigned char *buf, int len); void SPIFlashRead(unsigned int addr, unsigned char *buf, int len);   #endif
[Microcontroller]
S3C2440 interrupt program
During CPU operation, how to know when some unexpected events occur in various peripherals, such as the serial port receives new data, a device is inserted into the USB interface, a key is pressed, etc. There are mainly two methods: Query mode: The program cyclically queries the status of each device and responds ac
[Microcontroller]
Simple Application and Implementation of LCD of S3C2440
        LCD can be divided into: STN, TFT, LTPS, OLED. Each of the other categories has its own advantages and disadvantages. Since FL2440 uses the TFT type, we will talk about this separately. TFT LCD greatly shortens the screen response time, and its response time is less than 80ms. It also improves the screen blur a
[Microcontroller]
Simple Application and Implementation of LCD of S3C2440
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号