鱼C论坛

 找回密码
 立即注册
查看: 2938|回复: 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-9 20:43:20 | 显示全部楼层
你試用dev c++編譯的嗎?
把c原始碼po上來看看說不定你的c原始碼就錯了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我是在linux 64位机器下直接写的at&t汇编。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-9 22:45:06 | 显示全部楼层
#include <stdio.h>

int main(void)
{
        float a = 10.0f;
        printf("%f", a);
        return 0;
}
        .file        "test.c"
        .text
        .section        .rodata
.LC1:
        .string        "%f"
        .text
        .globl        main
        .type        main, @function
main:
.LFB0:
        .cfi_startproc
        leal        4(%esp), %ecx
        .cfi_def_cfa 1, 0
        andl        $-16, %esp
        pushl        -4(%ecx)
        pushl        %ebp
        .cfi_escape 0x10,0x5,0x2,0x75,0
        movl        %esp, %ebp
        pushl        %ebx
        pushl        %ecx
        .cfi_escape 0xf,0x3,0x75,0x78,0x6
        .cfi_escape 0x10,0x3,0x2,0x75,0x7c
        subl        $16, %esp
        call        __x86.get_pc_thunk.ax
        addl        $_GLOBAL_OFFSET_TABLE_, %eax
        flds        .LC0@GOTOFF(%eax)
        fstps        -12(%ebp)
        flds        -12(%ebp)
        subl        $4, %esp
        leal        -8(%esp), %esp
        fstpl        (%esp)
        leal        .LC1@GOTOFF(%eax), %edx
        pushl        %edx
        movl        %eax, %ebx
        call        printf@PLT
        addl        $16, %esp
        movl        $0, %eax
        leal        -8(%ebp), %esp
        popl        %ecx
        .cfi_restore 1
        .cfi_def_cfa 1, 0
        popl        %ebx
        .cfi_restore 3
        popl        %ebp
        .cfi_restore 5
        leal        -4(%ecx), %esp
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
.LFE0:
        .size        main, .-main
        .section        .rodata
        .align 4
.LC0:
        .long        1092616192
        .section        .text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
        .globl        __x86.get_pc_thunk.ax
        .hidden        __x86.get_pc_thunk.ax
        .type        __x86.get_pc_thunk.ax, @function
__x86.get_pc_thunk.ax:
.LFB1:
        .cfi_startproc
        movl        (%esp), %eax
        ret
        .cfi_endproc
.LFE1:
        .ident        "GCC: (GNU) 9.2.0"
        .section        .note.GNU-stack,"",@progbits

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

这几条指令重点理解
        flds        .LC0@GOTOFF(%eax)
        fstps        -12(%ebp)
        flds        -12(%ebp)
        subl        $4, %esp
        leal        -8(%esp), %esp
        fstpl        (%esp)
        leal        .LC1@GOTOFF(%eax), %edx
        pushl        %edx
        movl        %eax, %ebx
        call        printf@PLT
        addl        $16, %esp

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

        .code32
        .section .data
output:
        .asciz "This area is %f\n"

        .section .text
        .globl _start
_start:
        nop
        finit
        pushl        $10
        call        area
        addl        $4, %esp
        pushl        %eax
        flds        (%esp)
        leal        -8(%esp), %esp
        fstpl        (%esp)
        pushl        $output
        call        printf
        addl        $16, %esp
        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
$ gcc -m32 -g -Wall -nostartfiles -o main main.s
$ ./main
This area is 314.159271
$ 
想知道小甲鱼最近在做啥?请访问 -> 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寫的
後來腦子被門擠了跑去做硬體工程師, 現在回來看, 一切都回不去了.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-7 20:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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