Linux 下 AT&T汇编 - Hello World

来源:互联网 发布:横滨玛丽知乎 编辑:程序博客网 时间:2024/06/05 00:50

注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!


大约一年前第三次尝试自己写一个玩具操作系统内核玩,寒假时写到内存分页,没有搞定,开学后实验室各种事,之后是找实习、实习、找工作、忙实验,一直没有空把它搞定,深以为憾。现在虽然实验还没搞好,但终究按捺不住准备再次尝试BabyOS。

当然为了毕业,现在还得把主要时间用在做实验上,假期才能有较多集中时间真正来做BabyOS。然而假期将近,边角的一些时间,可以学习一下基础知识。主要包括AT&T汇编、保护模式、8259A、8253\8254、CMOS、通过读写端口号操作硬盘、内存分段、分页、中断异常、系统调用等。

前几次都是以Skelix为基础,并参考Linux0.12(赵炯博士),Tinix/Orange‘s(于渊兄)来写的,虽然也自己实现了一些东西,但终究是别人的代码居多,这次准备尽可能的用自己的代码,当然写这个BabyOS 纯属娱乐。

这次依然准备用AT&T汇编 + C + 内联汇编作为编程语言,所以首先准备花一点时间重新学一下AT&T汇编,当然也是Just For Fun,若在学习过程中感觉愉快,不妨多学点,若感到无味,则只求了解基本语法,因为实际上用不到多少汇编的知识,了解基本语法,遇到什么再学什么足矣。

先来个Hello World 吧~

.datamsg:.ascii "Hello world, hello AT&T asm!\n"len = . - msg.text.global _start_start:movl$len,%edx# 显示的字符数movl$msg,%ecx# 缓冲区指针movl$1,%ebx# 文件描述符movl$4,%eax# 系统调用号,_writeint$0x80# 系统调用movl$0,%ebx# 传给_exit的参数movl$1,%eax# 系统调用号,_exitint$0x80# 系统调用

编译、链接、运行:

liury@liury-laptop:~/program/asm/helloworld$ ls
helloworld.s  helloworld.s~  linux系统调用号
liury@liury-laptop:~/program/asm/helloworld$ as -o helloworld.o helloworld.s
liury@liury-laptop:~/program/asm/helloworld$ ld -o helloworld helloworld.o
liury@liury-laptop:~/program/asm/helloworld$ ls
helloworld  helloworld.o  helloworld.s  helloworld.s~  linux系统调用号
liury@liury-laptop:~/program/asm/helloworld$ ./helloworld 
Hello world, hello AT&T asm!

写个简单的makefile 吧:

all: helloworldhelloworld.o : helloworld.sas -o $@ $<helloworld : helloworld.old -o $@ $<
测试一下:
liury@liury-laptop:~/program/asm/helloworld$ rm helloworld
liury@liury-laptop:~/program/asm/helloworld$ ls
helloworld.o  helloworld.s  helloworld.s~  linux系统调用号  makefile
liury@liury-laptop:~/program/asm/helloworld$ rm helloworld.o 
liury@liury-laptop:~/program/asm/helloworld$ ls
helloworld.s  helloworld.s~  linux系统调用号  makefile
liury@liury-laptop:~/program/asm/helloworld$ make
as -o helloworld.o helloworld.s
ld -o helloworld helloworld.o
liury@liury-laptop:~/program/asm/helloworld$ ls
helloworld  helloworld.o  helloworld.s  helloworld.s~  linux系统调用号  makefile
liury@liury-laptop:~/program/asm/helloworld$ ./helloworld 
Hello world, hello AT&T asm!

简单注释:

int $0x80是一条AT&T语法的中断指令,用于Linux的系统调用。Linux write系统调用原型:ssize_t write(int fd, const void* buf, size_t count);writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.代码中的movl $1, %ebx 就是文件描述符fd,1表示STDOUT(标准输出),即输出到控制台上movl $msg, %ecx 就是buf,即缓冲区指针movl $len, %edx 就是count,即写到文件fd的字节数。
另一个简单例子CPUID:
# cpuid.s Sample program to extract the processor Vendor ID.section .dataoutput:.ascii "The processor Vendor ID is 'XXXXXXXXXXXX'\n".section .text.global _start_start:movl$0,%eax# The CPUID output option(the Vendor ID string)cpuidmovl$output,%edimovl%ebx, 28(%edi)movl%edx,32(%edi)movl%ecx,36(%edi)movl$42,%edx# 显示的字符数movl$output,%ecx# 缓冲区指针movl$1,%ebx# 文件描述符movl$4,%eax# 系统调用号,_writeint$0x80# 系统调用movl$0,%ebx# 传给_exit的参数movl$1,%eax# 系统调用号,_exitint$0x80# 系统调用
编译、链接、运行
liury@liury-laptop:~/program/asm/cpuid$ make
as -o cpuid.o cpuid.s
ld -o cpuid cpuid.o
liury@liury-laptop:~/program/asm/cpuid$ ./cpuid 
The processor Vendor ID is 'GenuineIntel'