#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
$
|