nasm组件中的printf float 64位 [英] printf float in nasm assembly 64-bit

查看:95
本文介绍了nasm组件中的printf float 64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用printf打印一个浮点数

I want to print a float value with printf

global main
extern printf

section .data
   string: db `%f\n`, 0

section .bss
   rs: resq 1

[...]

   movq xmm0, [rs]
   mov rdi, string
   mov rax, 0
   call printf

rs包含浮动值1.6

rs contains the floating value 1.6

(gdb) x/fg &rs
0x600ad8 <rs>:  1.6000000000000001

但是程序可以打印

[username@localhost folder]$ ./programname
0.000000

我可以让谁获得打印1.6的程序?我在做什么错了?

who can I get the program to print 1.6? what am I doing wrong?

推荐答案

我怀疑问题与您将代码rax设置为0的代码有关,而问题必须是1,因为您传递了浮点参数(有关详细信息,请参见此处).基本上rax应该包含在xmmN寄存器中传递的变量参数的数量.

I suspect the problem has something to do with your code setting rax to 0 whereas it must be 1 because you pass a floating point argument (see here for details). Basically rax should contain the number of variable arguments passed in xmmN registers.

printf中的崩溃似乎是由堆栈错配引起的,因为该程序在movaps指令(它期望内存操作数在16个字节的边界上对齐)处崩溃:

The crash in printf seems to be caused by stack miaslignment as the program crashes at a movaps instruction (which expects the memory operand to be aligned on 16-byte boundary):

=> 0x7ffff7a65f84 <__printf+36>:    movaps %xmm0,0x50(%rsp)
   0x7ffff7a65f89 <__printf+41>:    movaps %xmm1,0x60(%rsp)
   0x7ffff7a65f8e <__printf+46>:    movaps %xmm2,0x70(%rsp)
   0x7ffff7a65f93 <__printf+51>:    movaps %xmm3,0x80(%rsp)
   0x7ffff7a65f9b <__printf+59>:    movaps %xmm4,0x90(%rsp)
   0x7ffff7a65fa3 <__printf+67>:    movaps %xmm5,0xa0(%rsp)
   0x7ffff7a65fab <__printf+75>:    movaps %xmm6,0xb0(%rsp)
   0x7ffff7a65fb3 <__printf+83>:    movaps %xmm7,0xc0(%rsp)

输入main时,堆栈不是16字节对齐的,但是如果您对此进行了修复,则程序可以正常工作.下面是我的测试程序(请注意开头的sub rsp, 8):

When entering main the stack is not 16-byte aligned but if you fix this the program works fine. Below is my test program (notice the sub rsp, 8 in the beginning):

global main
extern printf

section .data
    string db `%f\n`, 0
    rs dq 1.6

section .text

main:
    sub rsp, 8
    movq xmm0, qword [rs]
    mov rdi, string
    mov rax, 1
    call printf
    add rsp, 8
    mov eax, 0x60
    xor edi, edi
    syscall

这篇关于nasm组件中的printf float 64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆