鱼C论坛

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

at&t 汇编

[复制链接]
发表于 2019-10-9 19:48:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
.code32
# functest3.s - An example of using C style functions
.section .data
output:
   .asciz "This area is %f\n"
precision:
   .byte 0x7f, 0x00
.section .text
.globl _start
_start:
   nop
   finit
pushl $10
   call area
   addl $4, %esp
    pushl %eax
    pushl $output
    call printf
    movl $120, %ebx
movl $1, %eax
   int $0x80

.type area, @function
area:
   pushl %ebp
   movl %esp, %ebp
   subl $4, %esp
   fldpi
   filds 8(%ebp)
   fmul %st(0), %st(0)
   fmulp %st(0), %st(1)
    fstps -4(%ebp)
    movl -4(%ebp), %eax
   movl %ebp, %esp
   popl %ebp
   ret

求问这个为什么输出不对?目标是输出314.1597, 但是实际输出0.0000
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-9 20:43:20 | 显示全部楼层
你試用dev c++編譯的嗎?
把c原始碼po上來看看說不定你的c原始碼就錯了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-9 20:48:24 | 显示全部楼层
kikiatw 发表于 2019-10-9 20:43
你試用dev c++編譯的嗎?
把c原始碼po上來看看說不定你的c原始碼就錯了

我是在linux 64位机器下直接写的at&t汇编。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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
回复 支持 反对

使用道具 举报

发表于 2019-10-11 09:11:46 | 显示全部楼层
本帖最后由 kikiatw 于 2019-10-11 09:16 编辑

樓上大神, 我跪了...
大概10幾年前我用C寫CGI, 那時候腦筋好使, 我當時把好幾個檔案編在一起,
都是先用gcc <filename.c> -S
然後再把好幾個 <filename.s>  -o filename.cgi
我記得打開filename.s 來看裡面就是組合語言, 類似版主的代碼, 所以我才以為他是用c寫的
後來腦子被門擠了跑去做硬體工程師, 現在回來看, 一切都回不去了.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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