当前位置:Linux教程 - Linux - 和luster一起学习在linux下使用汇编语言(5)

和luster一起学习在linux下使用汇编语言(5)

5. 我们开始写hello world吧


好吧,我们已经铺垫了很多东西了,而且看上去用汇编写程序似乎是一个非常恐怖的事情了。不过既然我们感兴趣,还是应该开始我们的“hello world”程序。
下面的代码中,我们准备采取直接使用内核中的系统调用的方法,这是调用系统内核服务的最快的方法,我们的代码不链接到其他函数库,也不使用ELF解释器,而是直接和内核通讯。

我们分别使用nasm和gas两种汇编器来编译我们的程序,这样我们可以看到Intel和AT&T两种语法格式了。

使用的工具

当然首先我们需要汇编编译器nasm和gas。然后我们需要链接器-ld,因为汇编编译器是生成的只是object代码。一般的发行包的binutils里面包括了gas和ld这两个实用工具。而对于大多数的发行包(例如,Debian,SuSe,Mandrake)都有nasm。


Hello, world!


Linux是一个32位的,运行在保护模式下的操作系统,使用的是flat memory 模式,使用ELF格式的二进制代码。

一个程序可以划分为下面几个部分: .text,.data,.bss。.text是一些只读的代码,.data是可读可写的数据区,.bss则是可读可写的没有初始化的数据区。当然可以有其他一些标准的部分,也可以使用户自己定义的sections,但是我们这里不关心。一个程序至少有.text部分。


下面就是我们的第一个程序“hello,world”。我们给出两个版本,分别是nasm和gas两种。


NASM (hello.asm)
--------------

section .data ;section declarationmsg db ""Hello, world!"",0xa ;our dear stringlen equ $ - msg ;length of our dear stringsection .text ;section declaration ;we must export the entry point to the ELF linker or global _start ;loader. They conventionally recognize _start as their ;entry point. Use ld -e foo to override the default._start:;write our string to stdout mov edx,len ;third argument: message length mov ecx,msg ;second argument: pointer to message to write mov ebx,1 ;first argument: file handle (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel;and exit mov ebx,0 ;first syscall argument: exit code mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel


GAS (hello.S)
--------------

.data # section declarationmsg: .string ""Hello, world! "" # our dear string len = . - msg # length of our dear string.text # section declaration # we must export the entry point to the ELF linker or .global _start # loader. They conventionally recognize _start as their # entry point. Use ld -e foo to override the default._start:# write our string to stdout movl $len,%edx # third argument: message length movl $msg,%ecx # second argument: pointer to message to write movl $1,%ebx # first argument: file handle (stdout) movl $4,%eax # system call number (sys_write) int $0x80 # call kernel# and exit movl $0,%ebx # first argument: exit code movl $1,%eax # system call number (sys_exit) int $0x80 # call kernel



建立可运行的程序


要生成一个可执行的代码,首先就是用源代码编译生产一个object文件。
对于nasm,下面的语法:

$ nasm -f elf hello.asm

而对于gas,而用下面的语法:

$ as -o hello.o hello.S

这样就得到了hello.o这个object文件了。

然后我们就要使用这个object文件来生成可执行代码。这里使用链接器链接:

$ ld -s -o hello hello.o

这样我们就获得了我们的可以执行的代码“hello,world”。

我们的学习就告一段落了。更多的信息可以去参考:
http://linuxassembly.org/list

by Luster([email protected])