程序集 64 位无效有效地址
Assembly 64-bit invalid effective address
我正在创建一个可以从 64 位 asm 中的 C 代码调用的函数
这是 C 等效函数:
/*
* 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);
}
这是汇编代码:
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
我使用nasm编译:nasm -f elf64 distanza.asm
问题是当我尝试使用 xmm0 和 xmm1:
处的地址获取 x[i] 和 y[i] 的值时
movss xmm2, [xmm0]
subss xmm2, [xmm1]
无法编译:有效地址无效。
如何使用存储在 xmm0 中的地址来获取内存中的值?我必须使用 xmm0,因为它是存储参数 float *x 的寄存器。
我相信一个地址只能使用通用寄存器,比如rax
,加上rip
。您需要在 "dereferencing" 之前将值移出 1。然而,我必须警告,我已经有一段时间没有组装了。
编辑
以下是英特尔手册中的相关部分:
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.
这与@Jester 提到的调用约定问题不同。
float*
是一个指针,它肯定不在 xmm0
.
中
; float dist(float *x, float *y, int length)
; in xmm0 there is *x, in xmm1 float *y, in rdi there is length
实际上,rdi
是*x
,rsi
是*y
,rdx
是length
。阅读 abi documentation or the overview at wikipedia.
另外 pusha/popa
在 64 位模式下不存在。
我正在创建一个可以从 64 位 asm 中的 C 代码调用的函数 这是 C 等效函数:
/*
* 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);
}
这是汇编代码:
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
我使用nasm编译:nasm -f elf64 distanza.asm
问题是当我尝试使用 xmm0 和 xmm1:
movss xmm2, [xmm0]
subss xmm2, [xmm1]
无法编译:有效地址无效。 如何使用存储在 xmm0 中的地址来获取内存中的值?我必须使用 xmm0,因为它是存储参数 float *x 的寄存器。
我相信一个地址只能使用通用寄存器,比如rax
,加上rip
。您需要在 "dereferencing" 之前将值移出 1。然而,我必须警告,我已经有一段时间没有组装了。
编辑
以下是英特尔手册中的相关部分:
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.
这与@Jester 提到的调用约定问题不同。
float*
是一个指针,它肯定不在 xmm0
.
; float dist(float *x, float *y, int length)
; in xmm0 there is *x, in xmm1 float *y, in rdi there is length
实际上,rdi
是*x
,rsi
是*y
,rdx
是length
。阅读 abi documentation or the overview at wikipedia.
另外 pusha/popa
在 64 位模式下不存在。