Chinaunix首页 | 论坛 | 博客
  • 博客访问: 178287
  • 博文数量: 11
  • 博客积分: 478
  • 博客等级: 下士
  • 技术积分: 264
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-20 11:45
文章分类
文章存档

2011年(1)

2009年(10)

我的朋友

分类: LINUX

2009-12-19 14:29:02

AT&T汇编语言是gcc使用的汇编语言,并且在大多数Unix和Linux系统上都使用AT&T汇编语言。他和Intel汇编都是IA-32平台的主要汇编语言,不同的是,Intel汇编主要运用在Windows系统.而AT&T还广泛运用在嵌入式领域,下面是AT&T语言的Hello World!

HelloWorld.s

#AT&T assemble , Hello World

.section .data
string:
    .ascii "Hello World!\n"

.section .text
.globl _start
_start:
    nop                        #cpu do nothing, this insctruction write for debug with gdb

#the follow 5 lines is: write(int file_descriptor, void *buffer, size_t length)
    movl    $4, %eax        #Linux soft interrupt, call number in eax, 4 is for system-call write
    movl    $1, %ebx        #file descriptor in ebx, stdout's file descriptor is 1
    movl    $string, %ecx    #buffer in ecx, this line move string's address to ecx
    movl    $13, %edx        #buffer's size in edx
    int        $0x80            #Linux soft interrupt

#the follow 3 lines is: exit(int status_code)
    movl    $1, %eax        #system call: exit
    movl    $0, %ebx        #status code in ebx
    int        $0x80            #Linux soft interrupt


在Linux下编译和链接此文件的指令为:
编译:
as -o HelloWorld.o HelloWorld.s
链接:
ld -o HelloWorld HelloWorld.o

在shell下运行:
$./HelloWorld
即可看到在shell下输出 Hello World!
在shell下查看退出码:
$echo $?
可以看到退出码为 0


HelloWorld.s源代码 
文件:HelloWorld.tar.gz
大小:0KB
下载:下载

阅读(3878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~