at&t 汇编
.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
你試用dev c++編譯的嗎?
把c原始碼po上來看看說不定你的c原始碼就錯了
kikiatw 发表于 2019-10-9 20:43
你試用dev c++編譯的嗎?
把c原始碼po上來看看說不定你的c原始碼就錯了
我是在linux 64位机器下直接写的at&t汇编。 #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
$ 本帖最后由 kikiatw 于 2019-10-11 09:16 编辑
樓上大神, 我跪了...
大概10幾年前我用C寫CGI, 那時候腦筋好使, 我當時把好幾個檔案編在一起,
都是先用gcc <filename.c> -S
然後再把好幾個 <filename.s>-o filename.cgi
我記得打開filename.s 來看裡面就是組合語言, 類似版主的代碼, 所以我才以為他是用c寫的
後來腦子被門擠了跑去做硬體工程師, 現在回來看, 一切都回不去了. {:5_104:}
页:
[1]