简体   繁体   中英

Assembly 64-bit invalid effective address

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.

I believe an address can only use general-purpose registers, such as rax , plus rip . You would need to move the value out to one before "dereferencing" it. I have to warn, however, it has been quite a while since I did assembly.

EDIT

Here is the relevant part from the Intel manual:

3.7.5.1 Specifying an Offset in 64-Bit Mode

The offset part of a memory address in 64-bit mode can be specified directly as a static value or through an address computation made up of one or more of the following components:

  • Displacement — An 8-bit, 16-bit, or 32-bit value.
  • Base — The value in a 64-bit general-purpose register.
  • Index — The value in a 64-bit general-purpose register.
  • Scale factor — A value of 2, 4, or 8 that is multiplied by the index value.

The base and index value can be specified in one of sixteen available general-purpose registers in most cases. See Chapter 2, “Instruction Format,” in the Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 2A.

The following unique combination of address components is also available.

  • RIP + Displacement ⎯ In 64-bit mode, RIP-relative addressing uses a signed 32-bit displacement to calculate the effective address of the next instruction by sign-extend the 32-bit value and add to the 64-bit value in RIP.

This is apart from the calling convention problem mentioned by @Jester.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM