返回首页   进站必读

12.2 汇编基础


12.2 汇编基础

要彻底搞清楚C语言的原理,就必须深入到指令一层去理解。从一个简单汇编程序说起:

#PURPOSE: Simple program that exits and returns a
#	  status code back to the Linux kernel
#
#INPUT:   none
#
#OUTPUT:  returns a status code. This can be viewed
#	  by typing
#
#	  echo $?
#
#	  after running the program
#
#VARIABLES:
#	  %eax holds the system call number
#	  %ebx holds the return status
#
 .section .data

 .section .text
 .globl _start
_start:
 movl $1, %eax	# this is the linux kernel command
		# number (system call) for exiting
		# a program

 movl $4, %ebx	# this is the status number we will
		# return to the operating system.
		# Change this around and it will
		# return different things to
		# echo $?

 int $0x80	# this wakes up the kernel to run
		# the exit command	

把这个程序保存成文件hello.s(汇编程序通常以.s作为文件名后缀),用汇编器as把汇编程序中的 助记符翻译成机器指令,生成目标文件hello.o:

$as hello.s -o hello.o

然后用链接器ld把目标文件hello.o链接成可执行文件hello:

$ld hello.o -o hello
$./hello
$echo $?
4