您的位置:首页 > 运维架构 > Linux

Linux 下 AT&T汇编 - Hello World

2012-12-19 01:53 381 查看
注:以下内容为学习笔记,多数是从书本、资料中得来,只为加深印象,及日后参考。然而本人表达能力较差,写的不好。因非翻译、非转载,只好选原创,但多数乃摘抄,实为惭愧。但若能帮助一二访客,幸甚!大约一年前第三次尝试自己写一个玩具操作系统内核玩,寒假时写到内存分页,没有搞定,开学后实验室各种事,之后是找实习、实习、找工作、忙实验,一直没有空把它搞定,深以为憾。现在虽然实验还没搞好,但终究按捺不住准备再次尝试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 吧~
.data
msg:
	.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	# 系统调用号,_write
	int	$0x80		# 系统调用

	movl	$0,	%ebx	# 传给_exit的参数
	movl	$1,	%eax	# 系统调用号,_exit
	int	$0x80		# 系统调用
编译、链接、运行:liury@liury-laptop:~/program/asm/helloworld$ lshelloworld.s helloworld.s~ linux系统调用号liury@liury-laptop:~/program/asm/helloworld$ as -o helloworld.o helloworld.sliury@liury-laptop:~/program/asm/helloworld$ ld -o helloworld helloworld.oliury@liury-laptop:~/program/asm/helloworld$ lshelloworld helloworld.o helloworld.s helloworld.s~ linux系统调用号liury@liury-laptop:~/program/asm/helloworld$ ./helloworld Hello world, hello AT&T asm!写个简单的makefile 吧:
all: helloworld
	
helloworld.o : helloworld.s
	as -o $@ $<

helloworld : helloworld.o
	ld -o $@ $<
测试一下:liury@liury-laptop:~/program/asm/helloworld$ rm helloworldliury@liury-laptop:~/program/asm/helloworld$ lshelloworld.o helloworld.s helloworld.s~ linux系统调用号 makefileliury@liury-laptop:~/program/asm/helloworld$ rm helloworld.o liury@liury-laptop:~/program/asm/helloworld$ lshelloworld.s helloworld.s~ linux系统调用号 makefileliury@liury-laptop:~/program/asm/helloworld$ makeas -o helloworld.o helloworld.sld -o helloworld helloworld.oliury@liury-laptop:~/program/asm/helloworld$ lshelloworld helloworld.o helloworld.s helloworld.s~ linux系统调用号 makefileliury@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 .data
output:
	.ascii "The processor Vendor ID is 'XXXXXXXXXXXX'\n"

.section .text
.global _start

_start:
	movl	$0,	%eax		# The CPUID output option(the Vendor ID string)	
	cpuid
	movl	$output,%edi
	movl	%ebx, 	28(%edi)
	movl	%edx,	32(%edi)
	movl	%ecx,	36(%edi)

	movl	$42,	%edx	# 显示的字符数
	movl	$output,%ecx	# 缓冲区指针
	movl	$1,	%ebx	# 文件描述符
	movl	$4,	%eax	# 系统调用号,_write
	int	$0x80		# 系统调用

	movl	$0,	%ebx	# 传给_exit的参数
	movl	$1,	%eax	# 系统调用号,_exit
	int	$0x80		# 系统调用
编译、链接、运行liury@liury-laptop:~/program/asm/cpuid$ makeas -o cpuid.o cpuid.sld -o cpuid cpuid.oliury@liury-laptop:~/program/asm/cpuid$ ./cpuid The processor Vendor ID is 'GenuineIntel'

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: