ARM assembly: Lesson 10

今天,我们来看一下基于ARM汇编,如何实现函数的调用。

基础知识

在ARM汇编中,函数的前四个参数存放于 R0~R3寄存器中, 剩余的参数存放于栈中,返回值存放于r0。在栈中存放数值,可以避免在调用过程中,数据的丢失,因为寄存器有限,并且经常被更新。

Function names in C are like labels in assembly.

The PC(program counter register) holds the address of the currently executing instruction. 

 函数实现

函数原型

首先,通常函数的C代码,我们基本理解我们需要干嘛——实现加法。

int add_nums(int num1, int num2)
{
return num1 + num2;
}

int main()
{
    add_nums(1,2};
    return 0;
}

汇编实现

LR (Link Register) 用于存放函数调用后,接下来的指令的地址。

.global _start
_start:
	
	mov r0, #1  //arg1
	mov r1,	#2	//arg2
	
	push {r0, r1}
	bl add_nums
	mov r2, r0
	pop {r0,r1}
	
add_nums:
	add r0,r0,r1
	bx lr
	

将r0, r1 存入栈中。这里可以观察到,sp指向的位置从0变成了一个很大的数字,在内存中查看对应的地址,可以看到,就是1,2!

在此前,我们提到了b可以作为无条件分支的关键词,此外,还有bl(branch with link)。bl除了可以进行分支之外,还可以存储PC的状态,保证在函数调用之后,回到调用之前的状态(PC地址)。

然后,通过step into,我们进入add_nums label,然后,对应的可以看到r0被更新为3. 通过bx指令,我们可以访问lr寄存器中的地址,对应的就是 mov r2, r0.

然后通过pop,我们基于栈中存储的元素更新了r0,r1,r0从3变成了1。可以观察到sp指向的地址变为0,可以理解为栈为空,但是,此前的地址中的数字并没有改变,还是1和2.

Take Away

bx lr指令用于访问存放于Link Register (LR) 的地址。

push 和pop用于将数字存放于栈中,或者从栈中获取。

PUSH {R0, R1}  // Push R0 and R1 onto the stack

2) Who uses ARM? Currently ARM CPU is licensed and produced by more than 100 companies and is the dominant CPU chip in both cell phones and tablets. Given its RISC architecture and powerful 32-bit instructions set, it can be used for both 8-bit and 32-bit embedded products. The ARM corp. has already defined the 64-bit instruction extension and for that reason many Laptop and Server manufactures are planning to introduce ARM-based Laptop and Servers. 3) Who will use our textbook? The primary audience of our textbook on ARM (ARM Assembly Language Programming & Architecture by Mazidi & Naimi) is undergraduate engineering students in Electrical and Computer Engineering departments. It can also be used by practicing engineers who need to move away from 8- and 16-bit legacy chips such as the 8051, AVR, PIC and HCS08/12 family of microcontrollers to ARM. Designers of the x86-based systems wanting to design ARM-based products can also benefit from this textbook. Table of Contents Chapter 1: The History of ARM and Microcontrollers Chapter 2: ARM Architecture and Assembly Language Programming Chapter 3: Arithmetic and Logic Instructions and Programs Chapter 4: Branch, Call, and Looping in ARM Chapter 5: Signed Numbers and IEEE 754 Floating Point Chapter 6: ARM Memory Map, Memory Access, and Stack Chapter 7: ARM Pipeline and CPU Evolution Appendix A: ARM Cortex-M3 Instruction Description Appendix B: ARM Assembler Directives Appendix C: Macros Appendix D: Flowcharts and Pseudocode Appendix E: Passing Arguments into Functions Appendix F: ASCII Codes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值