使用 GNU 汇编语法编写 Hello World 程序的三种方法

这篇具有很好参考价值的文章主要介绍了使用 GNU 汇编语法编写 Hello World 程序的三种方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本篇我们使用汇编来写一个经典的 Hello world 程序。

运行环境:

  • OS:Ubuntu 18.04.5 LTS x86-64
  • gcc:version 4.8.5

在用户空间编写汇编程序输出字符串,有三种方法:

  1. 调用C库函数 printf
  2. 使用软中断 int 0x80
  3. 使用 syscall系统调用

下面对三种方法,分别进行说明。

一、调用c库函数

为了更好的理解汇编代码,我们先介绍下 x86-64 架构中函数调用的习惯。

1.1 x86-64架构中函数调用的习惯

1.1.1 参数传递

x86-64中,最多允许 6 个参数通过通用寄存器来传递,多出的参数需要通过栈来传递;传递参数时,参数的顺序与寄存器的关系对应如下:

操作数大小(位) 参数1 参数2 参数3 参数4 参数5 参数6
64 %rdi %rsi %rdx %rcx %r8 %r9
32 %edi %esi %edx %ecx %r8d %r9d
16 %di %si %dx %cx %r8w %r9w
8 %dil %sil %dl %cl %r8b %r9b

当参数大于 6 个时,把超出的参数放到栈上,而参数 7 位于栈顶。

1.1.2 返回值

被调用函数返回时,把返回结果放入 %rax中,供调用函数来获取。

1.1.3 栈对齐

根据 System V AMD64 ABI 文档(下文简称 ABI 文档)说明(第 3.2.2 The Stack Frame 节),在 发起 call 指令之前,栈需要是16字节对齐的。

The end of the input argument area shall be aligned on a 16 (32 or 64, if __m256 or
__m512 is passed on stack) byte boundary. 11 In other words, the stack needs to be 16 (32
or 64) byte aligned immediately before the call instruction is executed.

1.1.4 XMM寄存器

根据 ABI 文档说明(第 3.2.3 Parameter Passing 节),当被调用的函数中有浮点数时,需要使用 %xmm0~%xmm7 共 8 个 SSE 寄存器来传递参数;

If the class is SSE, the next available vector register is used, the registers are taken
in the order from %xmm0 to %xmm7.

另外需要使用 %al 寄存器来指定使用的矢量寄存器的最大数量。

For calls that may call functions that use varargs or stdargs (prototype-less calls or calls
to functions containing ellipsis (. . . ) in the declaration) %al is used as hidden argument
to specify the number of vector registers used. The contents of %al do not need to match
exactly the number of registers, but must be an upper bound on the number of vector
registers used and is in the range 0–8 inclusive.

x86-64函数调用习惯,也可以参考维基百科上的文档,地址在这里:System V AMD64 ABI ;另外,关于 ABI 最新文档,可以从这里获取:x86-64-ABI 。

1.2、打印 Hello world!

代码如下:

.section .data
​
msg:
    .asciz "Hello world!\n"     # 定义了字符串 'Hello world!',由于是使用.asciz 定义的,会自动在字符串后面加上字符 '\0',以满足 C 语言习惯。
​
.section .text
.globl main
main:
    /* 调用 printf() 函数打印 "Hello world!" */
    /*  printf函数原型:int printf(char *fmt, ...) */
    subq $8, %rsp       # 发起 CALL 调用之前,栈必须是对齐到16字节,否则会报 segment fault 错误
    xorq %rax, %rax     # 被调用函数参数中有浮点数时, %al 寄存器中保存的是需要传送到 XMM 寄存器的参数数量
    mov $msg, %rdi      # 字符串地址
    call printf         # 调用C库函数 printf
    
    /* return */
    xorq %rax, %rax     # main函数返回值,rax = 0
    addq $8, %rsp       # 恢复原来的栈地址
    ret                 # 从 main 函数返回

编译并运行:

$ gcc -o helloworld helloworld.s
$ ./helloworld
Hello world!
$ echo $?
0

需要说明的是,我们在程序运行完成后,使用 echo $?来检查函数的返回值,这个返回值就是我们调用 ret指令之前,%rax里保存的值。我们可以把%rax里的值改成改成其它值,比如说 100(movq $100, %rax) 来验证下。

内核资料领取, Linux内核源码学习地址。

1.3 打印包含浮点数的格式化字符串

上面举了个最简单的输出 Hello world 的例子,如果说我们输出的参数里有变量,而且是个浮点数,该如何处理呢?根据函数调用习惯,我们把代码稍微修改一下,让它可以打印出 Hello world!1234.56,并且让函数返回100:

.section .data
​
msg:
    .asciz "Hello world!%.2f\n"
f:
    .double 1234.56
​
.section .text
.globl main
main:
    /* 调用 printf() 函数打印 "Hello world!" */
    /*  printf函数原型:int printf(char *fmt, ...) */
    subq $8, %rsp       # 发起 CALL 调用之前,栈必须是对齐到16字节,否则汇报 segment fault 错误
    movl $1, %eax       # 被调用函数参数中有浮点数时, %al寄存器中保存的是需要传送到XMM寄存器的参数数量,我们传入了1个浮点数,所以为1
    mov $msg, %rdi      # 字符串地址
    movsd f, %xmm0      # 参数为浮点数时,需要使用%xmm系列寄存器来传参
    call printf         # 调用C库函数 printf
    
    /* return */
    movq $100, %rax     # main函数返回值,rax = 100
    addq $8, %rsp       # 恢复原来的栈地址
    ret  

编译并运行:

$ gcc -o helloworld helloworld.s 
$ ./helloworld
Hello world!1234.56
$ echo $?
100

可以看到,运行后输出了浮点数,且返回值为100。

二、应用程序、C库和内核之间的关系

调用 printf() 函数时,应用程序、C库和内核之间的关系如下图所示:

使用 GNU 汇编语法编写 Hello World 程序的三种方法

从图中可以看到,我们调用C库函数printf()时,最终会调用内核的write()系统调用,那么我们就可以绕过C库,直接使用系统调用来输出字符串。

在Linux/x86 系统上,系统调用可以通过多种方式来实现。在32位系统上,可以通过 int 0x80sysenter来实现;在64位系统上,使用syscall来实现。其中 int 0x80是传统的系统调用方式,被称为 legacy system callsysentersyscall是后来添加的指令,被称为 Fast System Call 。

三、软中断 int 0x80

3.1 参数传递

当使用 int 0x80进行系统调用时,参数与寄存器的对应关系如下图所示:

系统调用号 参数1 参数2 参数3 参数4 参数5 参数6
%rax %rbx %rcx %rdx %rsi %rdi %rbp

该对应关系可以从 linux kernel 源码arch/x86/entry/entry_32.S里找到。如果大家不方便下载源码,可以从源码阅读网站查看,各版本的内核源码都有,地址在这里:Linux kernel在线阅读网站。

下面是5.0版本内核文件里的调用参数介绍,文档地址。

/*
 * 32-bit legacy system call entry.
 *
 * 32-bit x86 Linux system calls traditionally used the INT $0x80
 * instruction.  INT $0x80 lands here.
 *
 * This entry point can be used by any 32-bit perform system calls.
 * Instances of INT $0x80 can be found inline in various programs and
 * libraries.  It is also used by the vDSO's __kernel_vsyscall
 * fallback for hardware that doesn't support a faster entry method.
 * Restarted 32-bit system calls also fall back to INT $0x80
 * regardless of what instruction was originally used to do the system
 * call.  (64-bit programs can use INT $0x80 as well, but they can
 * only run on 64-bit kernels and therefore land in
 * entry_INT80_compat.)
 *
 * This is considered a slow path.  It is not used by most libc
 * implementations on modern hardware except during process startup.
 *
 * Arguments:
 * eax  system call number
 * ebx  arg1
 * ecx  arg2
 * edx  arg3
 * esi  arg4
 * edi  arg5
 * ebp  arg6
 */

3.2 系统调用号

在 x86-64 系统上,虽然仍然可以使用 int 0x80 来进行系统调用,但它执行的是32位的系统调用,使用的是32位的系统调用表,且效率低下,不应该再使用;在64位系统上,应该使用syscall系统调用,来使用64位的系统调用表。

32位系统调用表,可以在这里获取。下面列出了32位系统的部分调用及编号,可以看到,write()的系统调用编号为 4 ,exit()系统调用编号为 1。

#
# 32-bit system call numbers and entry vectors
#
# The format is:
# <number> <abi> <name> <entry point> <compat entry point>
#
# The __ia32_sys and __ia32_compat_sys stubs are created on-the-fly for
# sys_*() system calls and compat_sys_*() compat system calls if
# IA32_EMULATION is defined, and expect struct pt_regs *regs as their only
# parameter.
#
# The abi is always "i386" for this file.
#
0   i386    restart_syscall     sys_restart_syscall
1   i386    exit            sys_exit
2   i386    fork            sys_fork
3   i386    read            sys_read
4   i386    write           sys_write
5   i386    open            sys_open            compat_sys_open
6   i386    close           sys_close
7   i386    waitpid         sys_waitpid
8   i386    creat           sys_creat
9   i386    link            sys_link
10  i386    unlink          sys_unlink
11  i386    execve          sys_execve          compat_sys_execve
​
......

3.3 函数原型

write()系统调用,函数原型:

ssize_t write(int fd, const void *buf, size_t count);

exit()系统调用,函数原型:

void _exit(int status);

3.4 汇编代码

.section .data
​
msg:
    .ascii "Hello world!\n"
len = . - msg
​
.section .text
.globl main
main:
    /* write(2) 系统调用, 打印 "Hello world!" */
    /* write(2)原型:ssize_t write(int fd, const void *buf, size_t count); */
    movq $4, %rax       # write()系统调用号,4
    movq $1, %rbx       # 第一个参数,fd
    movq $msg, %rcx     # 第二个参数,buf
    movq $len, %rdx     # 第三个参数,count
    int $0x80
​
    /* exit(2) 系统调用  */
    /* exit()原型:void _exit(int status); */
    movq $1, %rax       # exit()系统调用号,1
    movq $0, %rbx       # 状态码,status
    int $0x80

编译并执行:

$ gcc -o helloworld helloworld.s 
$ ./helloworld
Hello world!
$ echo $?
0

说明:

  • 这里使用了.ascii 来定义一个字符串,而没有使用 .asciz,是因为我们不再需要兼容C的习惯,我们需要自己计算字符串的长度。
  • len = . - msg 里, ”.“表示当前地址。

四、syscall系统调用

4.1 参数传递

当使用 syscall进行系统调用时,参数与寄存器的对应关系如下图所示:

系统调用号 参数1 参数2 参数3 参数4 参数5 参数6
%rax %rdi %rsi %rdx %r10 %r8 %r9

该对应关系可以从 linux kernel 源码 arch/x86/entry/entry_64.S 里找到。下面是 5.0 版本内核文件里的调用参数介绍,文档地址。

/*
 * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
 *
 * This is the only entry point used for 64-bit system calls.  The
 * hardware interface is reasonably well designed and the register to
 * argument mapping Linux uses fits well with the registers that are
 * available when SYSCALL is used.
 *
 * SYSCALL instructions can be found inlined in libc implementations as
 * well as some other programs and libraries.  There are also a handful
 * of SYSCALL instructions in the vDSO used, for example, as a
 * clock_gettimeofday fallback.
 *
 * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
 * then loads new ss, cs, and rip from previously programmed MSRs.
 * rflags gets masked by a value from another MSR (so CLD and CLAC
 * are not needed). SYSCALL does not save anything on the stack
 * and does not change rsp.
 *
 * Registers on entry:
 * rax  system call number
 * rcx  return address
 * r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
 * rdi  arg0
 * rsi  arg1
 * rdx  arg2
 * r10  arg3 (needs to be moved to rcx to conform to C ABI)
 * r8   arg4
 * r9   arg5
 * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
 *
 * Only called from user space.
 *
 * When user can change pt_regs->foo always force IRET. That is because
 * it deals with uncanonical addresses better. SYSRET has trouble
 * with them due to bugs in both AMD and Intel CPUs.
 */

4.2 系统调用号

64位系统调用表,可以在这里获取。下面列出了64位系统的部分调用及编号,可以看到,write()的系统调用编号为 1 ,exit()系统调用编号为 60。

#
# 64-bit system call numbers and entry vectors
#
# The format is:
# <number> <abi> <name> <entry point>
#
# The __x64_sys_*() stubs are created on-the-fly for sys_*() system calls
#
# The abi is "common", "64" or "x32" for this file.
#
0   common  read            sys_read
1   common  write           sys_write
2   common  open            sys_open
3   common  close           sys_close
4   common  stat            sys_newstat
5   common  fstat           sys_newfstat
6   common  lstat           sys_newlstat
7   common  poll            sys_poll
8   common  lseek           sys_lseek
9   common  mmap            sys_mmap
10  common  mprotect        sys_mprotect
​
......
  
55  64  getsockopt      sys_getsockopt
56  common  clone           sys_clone
57  common  fork            sys_fork
58  common  vfork           sys_vfork
59  64  execve          sys_execve
60  common  exit            sys_exit
61  common  wait4           sys_wait4
62  common  kill            sys_kill
63  common  uname           sys_newuname
64  common  semget          sys_semget
65  common  semop           sys_semop
​
......

4.3 函数原型

write()系统调用,函数原型:

ssize_t write(int fd, const void *buf, size_t count);

exit()系统调用,函数原型:

void _exit(int status);

4.4 汇编代码

.section .data
msg:
    .ascii "Hello World!\n"
len = . - msg
​
.section .text
.globl  main
main:
​
    # ssize_t write(int fd, const void *buf, size_t count)
    mov $1, %rdi            # fd
    mov $msg, %rsi          # buffer
    mov $len, %rdx          # count
    mov $1, %rax            # write(2)系统调用号,64位系统为1
    syscall
​
    # exit(status)
    mov $0, %rdi            # status
    mov $60, %rax           # exit(2)系统调用号,64位系统为60
    syscall

编译并运行:

$ gcc -o helloworld helloworld.s 
$ ./helloworld
Hello world!
$ echo $?
0
提示
同样的系统调用函数,在32位系统和64位系统里,其调用号是不一样的,因为使用的是不同的系统调用表。

 文章来源地址https://www.toymoban.com/news/detail-460543.html

到了这里,关于使用 GNU 汇编语法编写 Hello World 程序的三种方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Visual Studio 2022 MASM x64汇编hello world以及调试(Console版 + Windows版)

    本文介绍使用Visual Studio 2022的MASM开发x64汇编程序hello world的环境配置和汇编代码,作为学习CPU指令的起点。分两个版本的hello world, 一个是console版本,另一个是windows版本。 首先安装visual studio community 2022,下载地址 https://visualstudio.microsoft.com/,安装时选择C++开发模块 安装好以

    2024年02月05日
    浏览(80)
  • 【区块链】以太坊Solidity编写一个简单的Hello World合约

    熟悉一门语言得从Hello World! 开始,因为这是最简单的一个输出形式。 我们先在contracts目录下建立一个helloworld.sol文件 进入编辑 保存退出 在migrations下新建一个部署合约的js文件:3_initial_migration.js 名字可以变动 接下来在test中使用js调用智能合约 在另一个窗口打开ganache 运行智

    2024年02月15日
    浏览(49)
  • 编程笔记 Golang基础 007 第一个程序:hello world 使用Goland

    开始在Goland环境中编程go语言代码啦。 打开GoLand软件。 选择 “File”(文件)菜单,然后点击 “New Project”(新建项目)或使用快捷键 Ctrl+Shift+A 并搜索 “New Project”。 在新建项目向导中,选择 “Go” 并点击 “Next” 按钮。 配置项目设置: 为项目选择一个合适的保存位置。

    2024年02月20日
    浏览(33)
  • Linux shell编程学习笔记14:编写和运行第一个shell脚本hello world!

     * 20231020 写这篇博文断断续续花了好几天,为了说明不同shell在执行同一脚本文件时的差别,我分别在csdn提供线上Linux环境 (使用的shell是zsh)和自己的电脑上(使用的shell是bash)做测试。功夫不负有心人,在其中一些实例中可以体现出zsh和bash的对脚本文件支持的差别,收

    2024年02月07日
    浏览(37)
  • HarmonyOS鸿蒙学习基础篇 - 运行第一个程序 Hello World

    下载与安装DevEco Studio      古话说得好,“磨刀不误砍柴工”,对于HarmonyOS应用开发,我们首先得确保工具齐全。这就好比要进行HarmonyOS应用开发,我们需要确保已经安装了DevEco Studio,这是HarmonyOS的一站式集成开发环境(IDE)。      下面我们就以在Windows系统上安装DevEco

    2024年01月23日
    浏览(31)
  • 机器人CPP编程基础-01第一个程序Hello World

    很多课程先讲C/C++或者一些其他编程课,称之为基础课程。然后到本科高年级进行机器人专业课学习,这样时间损失非常大,效率非常低。 C++/单片机/嵌入式/ROS等这些编程基础可以合并到一门课中进行实现,这些素材已经迭代三轮以上,全部公开,需要可以参考,不需要,我

    2024年02月13日
    浏览(37)
  • 创建第一个Servlet程序“hello world“(创建流程+页面出错情况)

    目录 🐲 1. 动态页面之Servlet 🐲 2. 写第一个Servlet的程序:\\\"hello world!\\\" 🦄 2.1 创建项目 🦄 2.2 引入Servlet依赖 🦄 2.3 创建目录结构 🦄 2.4 编写代码  🦄 2.5 打包程序 🦄 2.6 部署程序 🦄 2.7 验证程序 🐲3. 创建Servlet流程简化 🐲4. 工作原理流程分析 🐲5. 访问页面出错 HTTP服务器

    2023年04月11日
    浏览(32)
  • Python程序员Visual Studio Code指南2 Hello World

    Visual Studio Code的Python 扩展提供了对Python语言的支持,包括语法着色、代码补全、过滤、调试、代码导航和代码格式化等功能,以及Jupyter Notebook支持等Python特有的功能。您可以在Visual Studio Code的扩展视图中安装Python扩展。与从扩展市场安装的任何扩展一样,你可以在设置编辑

    2024年02月12日
    浏览(27)
  • 【JavaWeb】使用Servlet实现输出 hello world

    之前讲过如何使用IDEA创建Servlet项目. 因此创建项目这一步就不过多介绍了 有需要的可以看一下➡IDEA专业版和社区版创建Servlet项目 今天介绍如何使用Servlet输出一个\\\"hello world\\\". 示例: 解释一下 @WebServlet(\\\"/hello\\\") 这是什么意思 @WebServlet()会根据某些GET请求会生效, 然后里面写的

    2024年02月09日
    浏览(41)
  • 【30天熟悉Go语言】2 Go开发环境搭建、Hello World程序运行

    Go系列文章: GO开篇:手握Java走进Golang的世界 Go专栏传送链接:https://blog.csdn.net/saintmm/category_12326997.html 1 进入到Go官网(https://golang.org),点击Download按钮; 2 选择操作系统对应的环境版本(图形化安装),进行下载,比如博主的windows: 3 下载完一路安装,博主的安装目录如下

    2024年02月06日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包