我做了一个nasm程序,计算一定大小的两个向量之间的eucledian距离。 这个nasm函数是从C文件中调用的,它获取函数的结果。我已经测试了,它的工作原理,返回的值是正确的,我可以用任何问题打印出来。 我的问题是当我尝试在nasm文件中打印结果时。 这是nasm代码:
extern printf
%macro print_dist2 1
section .data
.str db %1,0 ; %1 is macro call first actual parameter
section .text
push dword [dist]
push dword .str
push dword fmt2
call printf
add esp, 12 ; pop stack 3 * 4 bytes
%endmacro
section .data
dist: dd 258
fmt2: db "%s, dist: %g",10,0 ; format string for printf
section .bss
section .text
global distanza
x equ 8
y equ 12
dim equ 16
ris equ 20
distanza:
; ------------------------------------------------------------
; Function entrance sequence
; ------------------------------------------------------------
push ebp
mov ebp, esp
push ebx
push esi
push edi
; ------------------------------------------------------------
; read the parameters from the current stack frame
; ------------------------------------------------------------
mov eax, [ebp+x] ; address of x
mov ebx, [ebp+y] ; address of y
mov ecx, [ebp+dim] ; size of the vectors (it's the same)
.
.
. omitted calculations.
.
sqrtss xmm1, xmm1
movss [dist], xmm1 ; move the result to dist
fld dword [dist] ; mov in st0, that will be used by the C file
print_dist2 "distanza"
; ------------------------------------------------------------
; Function exit sequence
; ------------------------------------------------------------
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
这是C档案:
float a[] = {6.1f, 9.5f, 12.6f};
float b[] = {25.1f, 34.1f, 9.6f};
dim = 3;
float dist = distanza(a, b, dim);
printf("d(x,y) = %f\n", dist);
这是输出:
distanza, dist: 5.46877e-315
d(x,y) = 31.227551
C,代码打印正确的值,nasm没有。 如果我更改了nasm代码的格式字符串:
fmt2: db "%s, dist: %f",10,0
我使用%f而不是%g,nasm输出就是这个:
distanza, dist: 0.000000
我不知道我的代码有什么问题,为什么它没有打印正确的值?
答案 0 :(得分:3)
您的dist
是一个32位浮点数,但printf
需要一个双64位浮点数。你必须改变它:
%macro print_dist2 1
section .data
.str db %1,0 ; %1 is macro call first actual parameter
section .text
sub esp, 8 ; Space for a 64-bit double floating point number
fld dword [dist] ; Load a 32-bit single floating point number
fstp qword [esp] ; Store it as 64-bit floating point number
push dword .str
push dword fmt2
call printf
add esp, 16 ; pop stack 4 * 4 bytes
%endmacro