att 汇编 helloworld

  • 博主在 archlinux x86_64 下测试, gcc版本
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.1.0 (GCC) 

使用汇编语言写一个helloworld,这里记录一下遇到的问题和收获, 代码如下

.data  数据section, 非segment
.output: # 定义一个名为.output的变量,类型为string, 内容为helloworld
        .string "hello world
"
.text  # 代码section
.global main # 全局main

main:
# 1st arg
#leaq .output(%RIP), %rdi 
movq $.output, %rdi
movq $0, %rax
CALL printf
RET

汇编由三部分组成, 且指令不区分大小写

  1. label 如.data .text main
  2. directive 如.output .global
  3. instruction 如 movq call

系统调用和C库函数调用

上面的代码是c库调用,方法比较简单,而c库函数最终也会调用到系统函数
调用系统函数write 也可以实现,不过比较复杂

main:
movq $1,	%rdi            # 1号系统调用write
#leaq .output(%RIP), %rsi      
movq $.output,	%rsi            # 起始地址参数
movq $13,	%rdx            # 长度参数
movq $1,	%rax            # 调用可变参数的函数write时,指定浮点参数个数,指针也算
syscall                         # 0x80 改 syscall 了,触发中断
RET

gcc的默认选项--enable-default-pie 导致编译的时候报错

$gcc helloworld.s    
/usr/bin/ld: /tmp/ccbhBYK4.o: relocation R_X86_64_32S against `.data' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status

原因

64位下, 指针.output 的长度是64bit, 在位置依赖的可执行文件里可以使用。 而gcc的pie=true,是位置无关的,使用lea来通过rip求其偏移地址

有2个办法

  1. 增加gcc 选项
$gcc -g -no-pie helloworld.s 
$./a.out 
hello world

  1. 改用lea
#leaq .output(%RIP), %rsi      
movq $.output,	%rsi            # 起始地址参数

调用printf时,一直coredump

修改前的代码

main:
# 1st arg
leaq .output(%RIP), %rdi 
CALL printf
RET

执行结果

$./a.out 
zsh: segmentation fault (core dumped)  ./a.out

原因是 rax需要保存xmm 个数,即浮点类型参数的个数,这里没有浮点数

办法

main:
# 1st arg
leaq .output(%RIP), %rdi 
movq $0, %rsi
CALL printf
RET

传参舒徐

asm传参顺序是从右往左依次传参, 而c中没有规定顺序

printf("msg %d,%s",1,"abc");

对应的asm为

movq "abc"地址, %rdi
movq 1, %rsi
movq "msg %d,%s" rdx 

所以, 类似printf("%d, %d", a+b, a=a+1) 这样的c 语句的执行结果是不确定的
但是根据汇编的角度看,结果就是 a=a+1 先执行

32位和54位函数调用方式区别

在 32 位下, 调用函数时由调用者先将参数压栈,再调用函数, 被调者从栈中获取值,返回值保存在寄存器eax中,再由调用者清理堆栈

在 64位下, 调用函数时

  • 整型参数保存到寄存器,顺序是%rdi, %rsi, %rdx, %rcx, %r8, 和 %r9
  • 浮点参数依次放在寄存器%xmm0-%xmm7中
  • 寄存器不够用时,参数放到栈中
  • 可变参数哈函数(比如printf), 寄存器%eax需记录下浮点参数的个数
  • 被调用的函数可以使用任何寄存器,但它必须保证%rbx, %rbp, %rsp, and %r12-%r15恢复到原来的值(如果它改变了它们的值)。
  • 返回值存储在 %rax中, 包括main函数
原文地址:https://www.cnblogs.com/hustcpp/p/13060615.html