打印汇编浮点数 [英] Printing floating point numbers in assembler
问题描述
我试图从assemler调用printf函数打印浮点值。它正常工作与字符串和整数值,但无法打印浮动。这里是工作code的例子:
全球主要
EXTERN的printf段.data
消息:DB字符串是:%D%X%的,10,0
end_message:DB..字符串结尾,0.text段
主要:
MOV EAX,为0xFF
MOV EDI,消息
movsxd RSI,EAX
MOV RDX,为0xFF
MOV RCX,end_message
XOR RAX,RAX
调用printf
RET
字符串是:255 FF ..字符串的结尾
块引用>那么,这些参数通过寄存器传输: EDI 的包含格式化字符串, RSI 的和地址的 RDX 的包含相同数量的打印在十进制和十六进制的风格,的 RCX 的包含字符串的结束, RAX 的包含0,因为我们没有一个浮动进行打印。
这code工作正常,但同时试图打印浮动有新的变化:全球主要EXTERN的printf段.data
VAL:DQ 123.456
味精:DB的结果是:%FL,10,0.text段
主要:
MOV RDI,味精
MOVSD XMM0,[VAL]
MOV EAX,1
调用printf MOV RAX,0
RET这code剪断可以编译但返回正在执行分段错误。看来,这个问题是在的 XMM0 的,但试图改变的 MOVSD XMM0,[VAL] 为 MOVSD XMM0,VAL 给出了<错误值/ p>
错误:OP code和操作数的组合无效
块引用>信息。
编译器NASM在openSUSE 12.3运行更新。我试图让一个C程序产生.S组装。这给出了一个非常奇怪的解决方案:
主:
.LFB2:
.cfi_startproc
pushq%RBP
.cfi_def_cfa_offset 16
.cfi_offset 6,-16
MOVQ%RSP,RBP%
.cfi_def_cfa_register 6
SUBQ $ 32%,可吸入悬浮粒子
MOVL%EDI,-4(RBP%)
MOVQ%RSI,-16(RBP%)
MOVQ VAL(%RIP),RAX%
MOVQ%RAX,-24(RBP%)
MOVSD -24(RBP%),%XMM0
MOVL $ .LC0,EDI%
MOVL $ 1,%eax中
调用printf
MOVL $ 0,%EAX
离开
.cfi_def_cfa 7,8
RET是否可以写一个简单的printf的例子吗?
解决方案为您汇编的问题:
你需要你的主程序开始前对齐堆栈。插入
子RSP,8
在主力右:
然后退役前再添加它:
添加RSP,8
I'm trying to print a floating-point value from assemler calling a printf function. It works fine with strings and integer values but fails printing floats. Here is an example of working code:
global main extern printf section .data message: db "String is: %d %x %s", 10, 0 end_message: db ".. end of string", 0 section .text main: mov eax, 0xff mov edi, message movsxd rsi, eax mov rdx, 0xff mov rcx, end_message xor rax, rax call printf ret
String is: 255 ff .. end of string
So, the parameters are passed through registers: edi contains address of a formatting string, rsi and rdx contain the same number to print in decimal and hex styles, rcx contains end of a string, rax contains 0 as we do not have a float to print. This code works fine but something changes while trying to print float:
global main extern printf section .data val: dq 123.456 msg: db "Result is: %fl",10, 0 section .text main: mov rdi,msg movsd xmm0,[val] mov eax,1 call printf mov rax, 0 ret
This code snipped can be compiled but returns segmentation fault being executed. It seems that the problem is in wrong value of xmm0 but trying to change movsd xmm0,[val] to movsd xmm0,val gives an
error: invalid combination of opcode and operands
message. The compiler is NASM running on openSuSe 12.3
Update. I tried to make a c program and produce a .S assembly. It gives a very weird solution:
main: .LFB2: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $32, %rsp movl %edi, -4(%rbp) movq %rsi, -16(%rbp) movq val(%rip), %rax movq %rax, -24(%rbp) movsd -24(%rbp), %xmm0 movl $.LC0, %edi movl $1, %eax call printf movl $0, %eax leave .cfi_def_cfa 7, 8 ret
Is it possible to write a simple printf example?
解决方案for your assembler problem: you need to align the stack before your main program starts.
insert
sub rsp, 8
right after main:
then add it again before ret:
add rsp, 8
这篇关于打印汇编浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!