鱼C论坛

 找回密码
 立即注册
查看: 4208|回复: 4

at&t 汇编

[复制链接]
发表于 2019-10-9 22:45:06 | 显示全部楼层
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         float a = 10.0f;
  5.         printf("%f", a);
  6.         return 0;
  7. }
复制代码

  1.         .file        "test.c"
  2.         .text
  3.         .section        .rodata
  4. .LC1:
  5.         .string        "%f"
  6.         .text
  7.         .globl        main
  8.         .type        main, @function
  9. main:
  10. .LFB0:
  11.         .cfi_startproc
  12.         leal        4(%esp), %ecx
  13.         .cfi_def_cfa 1, 0
  14.         andl        $-16, %esp
  15.         pushl        -4(%ecx)
  16.         pushl        %ebp
  17.         .cfi_escape 0x10,0x5,0x2,0x75,0
  18.         movl        %esp, %ebp
  19.         pushl        %ebx
  20.         pushl        %ecx
  21.         .cfi_escape 0xf,0x3,0x75,0x78,0x6
  22.         .cfi_escape 0x10,0x3,0x2,0x75,0x7c
  23.         subl        $16, %esp
  24.         call        __x86.get_pc_thunk.ax
  25.         addl        $_GLOBAL_OFFSET_TABLE_, %eax
  26.         flds        .LC0@GOTOFF(%eax)
  27.         fstps        -12(%ebp)
  28.         flds        -12(%ebp)
  29.         subl        $4, %esp
  30.         leal        -8(%esp), %esp
  31.         fstpl        (%esp)
  32.         leal        .LC1@GOTOFF(%eax), %edx
  33.         pushl        %edx
  34.         movl        %eax, %ebx
  35.         call        printf@PLT
  36.         addl        $16, %esp
  37.         movl        $0, %eax
  38.         leal        -8(%ebp), %esp
  39.         popl        %ecx
  40.         .cfi_restore 1
  41.         .cfi_def_cfa 1, 0
  42.         popl        %ebx
  43.         .cfi_restore 3
  44.         popl        %ebp
  45.         .cfi_restore 5
  46.         leal        -4(%ecx), %esp
  47.         .cfi_def_cfa 4, 4
  48.         ret
  49.         .cfi_endproc
  50. .LFE0:
  51.         .size        main, .-main
  52.         .section        .rodata
  53.         .align 4
  54. .LC0:
  55.         .long        1092616192
  56.         .section        .text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
  57.         .globl        __x86.get_pc_thunk.ax
  58.         .hidden        __x86.get_pc_thunk.ax
  59.         .type        __x86.get_pc_thunk.ax, @function
  60. __x86.get_pc_thunk.ax:
  61. .LFB1:
  62.         .cfi_startproc
  63.         movl        (%esp), %eax
  64.         ret
  65.         .cfi_endproc
  66. .LFE1:
  67.         .ident        "GCC: (GNU) 9.2.0"
  68.         .section        .note.GNU-stack,"",@progbits
复制代码


参考这个程序,可以看到printf使用的是%f占位符,指明是float,但是实际传给printf的却是double

这几条指令重点理解
  1.         flds        .LC0@GOTOFF(%eax)
  2.         fstps        -12(%ebp)
  3.         flds        -12(%ebp)
  4.         subl        $4, %esp
  5.         leal        -8(%esp), %esp
  6.         fstpl        (%esp)
  7.         leal        .LC1@GOTOFF(%eax), %edx
  8.         pushl        %edx
  9.         movl        %eax, %ebx
  10.         call        printf@PLT
  11.         addl        $16, %esp
复制代码


知道了问题出在哪里就好办了
把代码改成下面这样就可以了
  1. # functest3.s - An example of using C style functions

  2.         .code32
  3.         .section .data
  4. output:
  5.         .asciz "This area is %f\n"

  6.         .section .text
  7.         .globl _start
  8. _start:
  9.         nop
  10.         finit
  11.         pushl        $10
  12.         call        area
  13.         addl        $4, %esp
  14.         pushl        %eax
  15.         flds        (%esp)
  16.         leal        -8(%esp), %esp
  17.         fstpl        (%esp)
  18.         pushl        $output
  19.         call        printf
  20.         addl        $16, %esp
  21.         movl        $120, %ebx
  22.         movl        $1, %eax
  23.         int        $0x80

  24.         .type area, @function
  25. area:
  26.         pushl        %ebp
  27.         movl        %esp, %ebp
  28.         subl        $4, %esp
  29.         fldpi
  30.         filds        8(%ebp)
  31.         fmul        %st(0), %st(0)
  32.         fmulp        %st(0), %st(1)
  33.         fstps        -4(%ebp)
  34.         movl        -4(%ebp), %eax
  35.         movl        %ebp, %esp
  36.         popl        %ebp
  37.         ret
复制代码

  1. $ gcc -m32 -g -Wall -nostartfiles -o main main.s
  2. $ ./main
  3. This area is 314.159271
  4. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-4 14:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表