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'
相关文章推荐
- linux 下at&t语法的汇编之hello world!!
- linux--汇编.S--AT&T汇编语法
- Linux AT&T 汇编语言开发指南
- linux下的C语言开发(AT&T 汇编语言)
- Linux 内核入门学习笔记(一) AT&T汇编基础
- linux下的C语言开发(AT&T 汇编语言)
- Linux环境汇编语言编程初步——AT&T语法
- Linux 汇编语言开发指南(AT&T)
- linux下的C语言开发(AT&T 汇编语言)
- Linux 汇编语言开发指南 Intel 格式--AT&T 格式
- Linux下的AT&T语法(即GNU as 汇编语法)入门(转载)
- Linux 下AT&T汇编语法格式简介02
- Linux下AT&T汇编语法格式简介2
- Linux 0.11内核bootsect.s和loader.s的移植为AT&T汇编
- [转贴]AT&amp;amp;amp;T ASM Syntax,帮大家了解linux代码中的AT&amp;amp;amp;T汇编
- Linux 下AT&T汇编语法格式简介03
- Linux下AT&T汇编语法格式简介1
- Linux AT&T 汇编错误:Error: invalid instruction suffix for `push'
- linux汇编工具之GAS(AT&T 语法)和NASM(Intel 语法)比较
- Linux下AT&T汇编语法格式简介