I'm creating a function do be called from C code in asm 64 bit this is the C equivalent function:
/*
* x and y are two arrays of floats, this function calculates the
* distance beetween those two objects
*/
float dist(float *x, float *y, int length)
{
float c = 0;
int i;
for (i = 0; i < length; ++i)
{
c += (x[i] - y[i]) * (x[i] - y[i]);
}
return sqrt(c);
}
This is the assembly code:
section .text
global distanza64
distanza64:
push rbp ; save base pointer
mov rbp, rsp
pushaq ; save general registers
; C function
; float dist(float *x, float *y, int length)
; in xmm0 there is *x, in xmm1 float *y, in rdi there is length
loop:
cmp rdi, 0 ; cycle counter
je end_loop
movss xmm2, [xmm0] ; x[i]
subss xmm2, [xmm1] ; x[i] = x[i] - y[i] i.e (a-b)
mulss xmm2, xmm2 ; x[i] = x[i] * x[i] i.e (a-b)*(a-b)
addss xmm3, xmm2 ; c += x[i] i.e c = (a-b)*(a-b)
addsd xmm0, 8 ; vgo to next address 8*8 = 64-bit
addsd xmm1, 8 ; same as above
dec rdi ; length--
end_loop:
sqrtss xmm3, xmm3 ; c = sqrt(c)
movss xmm0, xmm3 ; in xmm0 there is the final value
popaq
mov rsp, rbp
pop rbp
ret
I compile using nasm: nasm -f elf64 distanza.asm
The problem is when I try to get the values of x[i] and y[i] using the address at xmm0 and xmm1:
movss xmm2, [xmm0]
subss xmm2, [xmm1]
It won't compile: invalid effective address. How can I use the addresses stored in xmm0 to get the values in memory? I must use xmm0 because it's the register where the paramater float *x, is stored.
float*
is a pointer and it most certainly isn't in xmm0
.
; float dist(float *x, float *y, int length)
; in xmm0 there is *x, in xmm1 float *y, in rdi there is length
Actually, rdi
is *x
, rsi
is *y
and rdx
is length
. Read the abi documentation or the overview at wikipedia.
Also pusha/popa
do not exist in 64 bit mode.