Hello World——Linux汇编

这篇博客介绍了如何在Linux环境下用汇编语言编写一个简单的"Hello World"程序,分别展示了32位和64位系统的汇编代码。在32位系统中,使用int $0x80进行系统调用,而在64位系统中,系统调用指令变为syscall。文章还提到了系统调用号和参数寄存器在不同体系结构中的变化。
摘要由CSDN通过智能技术生成

采用AT&T语法。

32位: 

 1 .section .text
 
2
.global _start
 
3

 
4 msg:
 
5     .ascii "Hello World!/n"
 
6 msg_end:
 
7     .equ len, msg_end - msg
 
8     .equ SYS_write, 4

 
9     .equ SYS_exit, 1
10
11 _start:
12
13     mov $SYS_write, %eax    # system call number
14     mov $1, %ebx            # file descriptor (stdout)
15     mov
$msg, %ecx          # message to write
16     mov
$len, %edx          # message length.
17     int $0x80               # system call

18
19     mov $SYS_exit, %eax     # system call number
20     mov $0, %ebx            # exit (0
)
21     int $0x80               # system call

 


64位:

 1 .section .text
 
2
.global _start
 
3

 
4 msg:
 
5     .ascii "Hello World!/n"
 
6 msg_end:
 
7     .equ len, msg_end - msg
 
8     .equ SYS_write, 1

 
9     .equ SYS_exit, 60
10
11 _start:
12
13     mov $SYS_write, %rax    # system call number
14     mov $1, %rdi            # file descriptor (stdout)
15     mov
$msg, %rsi          # message to write
16     mov
$len, %rdx          # message length.
17     syscall                 # previous 'int $0x80' in
i386
18

19     mov $SYS_exit, %rax     # system call number
20     mov $0, %rdi            # exit (0
)
21     syscall                 # previous 'int $0x80' in i386

 

编译命令一样:(假设汇编源文件名为:hello.s)

$ as hello.s -o hello.o

$ ld hello.o -o hello

主要区别:

(1)系统调用号不同了,比如sys_write在i368中是4,x86-64中是1;sys_exit在i386中是1,而x86_64中是60;
(2)系统调用所使用的6个参数寄存器也变了,i386中分别是ebx/ecx/edx/esi/edi/ebp,x86_64中则使用rdi/rsi/rdx/r10/r8/r9,显然不只是“e”改成“r”那么简单;
(3)执行系统调用的指令,i386中使用“int 80”,而x86-64中使用“syscall”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值