概述
干了十来年,感觉个人技术发展受限于基础知识,想用半年的时间把基础知识再巩固一下,当然由于工作的原因这个时间有可能会拉长,但是我会坚持下来。
现在高级语言这么多,编译器已经帮我们做了很多优化,为什么还要学习汇编语言呢,下面是一本参考书给出来的答案:
• the first steps in booting the computer,
• code to handle interrupts,
• low-level locking code for multi-threaded programs,
• code for machines where no compiler exists,
• in situations where the compiler cannot generate code that is optimal (or efficient enough),
• on computers with very limited memory, and
• code that re?uires low-level access to architectural and/or processor features.
出了上面的原因,促使我再次重头学习汇编的还有一下原因:
1、深入研究linux系统需要,尤其是后续想深度优化性能
2、研究语言原理,最近准备学习Go语言。
初次尝试
深入分析之前我们先品尝一下汇编程序,下面这段是网上一段helloworld的程序
.text
.global main
main:
ldr x0, addr_of_keep_x30
str x30, [x0]
ldr x0, addr_of_msg
bl puts
ldr x0, addr_of_keep_x30
ldr x30, [x0]
mov w0, #0
ret
addr_of_msg: .dword msg
addr_of_keep_x30: .dword keep_x30
.data
msg: .asciz "hello world!\n"
keep_x30: .dword 0
编译:
aarch64-linux-gnu-gcc -static -o test test.s
运行:
qemu-aarch64 test
note:
我这边环境是搭建开发环境,主要安装:
sudo apt install gcc-aarch64-linux-gnu
sudo apt install qemu-user
GNU汇编语法
现代的计算机设备主要包括三个部分-CPU 内存和IO设备,这些设备之间最大的区别恐怕就是CPU的体系结构了。每种体系结构都会有自己的汇编指令集,尽管他这些汇编有很大的区别,但是他们之间也会有很多的相似点。GNU汇编(GAS)是一款具备高度可移植性和可重配的汇编语言,他的简单和通用的语法广泛应用各种体系的CPU,我们上节的例子就是GNU汇编的一个例子。
汇编程序的结构
参考
ARM aarch64汇编学习笔记(一):搭建编译环境_Hober-CSDN博客_aarch64 汇编
《ARM 64-Bit Assembly Language》