x86程序集 - 从具有结构的数组中获取结构(x86 assembly - get structure from an array with structures)

我创建了一个带结构的数组,我需要使用索引和指向数组的指针从数组中获取结构。

struct T{ char a, b, c, d, e, f, g; }; T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; } int _tmain(int argc, _TCHAR* argv[]) { T struct1, struct2, struct3, struct4; struct1.a = 1; struct1.b = 2; struct1.c = 3; struct1.d = 4; struct1.e = 5; struct1.f = 6; struct1.g = 7; struct2.a = 8; struct2.b = 9; struct2.c = 10; struct2.d = 11; struct2.e = 12; struct2.f = 13; struct2.g = 14; struct3.a = 15; struct3.b = 16; struct3.c = 17; struct3.d = 18; struct3.e = 19; struct3.f = 20; struct3.g = 21; struct4.a = 22; struct4.b = 23; struct4.c = 24; struct4.d = 25; struct4.e = 26; struct4.f = 27; struct4.g = 28; T pole1[] = { struct1, struct2, struct3, struct4 }; T result = CtiPrvekPole1(pole1, 2); printf("Cti prvek pole1 : %c\n", result.b); }

我应该如何获得该结构? 我使用了8个字节,因为一个结构有7个字节,所以它应该是带有对齐的8个字节。 我对吗?

谢谢。

I created an array with structures and I need to get the structure from the array using an index and a pointer to the array.

struct T{ char a, b, c, d, e, f, g; }; T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; } int _tmain(int argc, _TCHAR* argv[]) { T struct1, struct2, struct3, struct4; struct1.a = 1; struct1.b = 2; struct1.c = 3; struct1.d = 4; struct1.e = 5; struct1.f = 6; struct1.g = 7; struct2.a = 8; struct2.b = 9; struct2.c = 10; struct2.d = 11; struct2.e = 12; struct2.f = 13; struct2.g = 14; struct3.a = 15; struct3.b = 16; struct3.c = 17; struct3.d = 18; struct3.e = 19; struct3.f = 20; struct3.g = 21; struct4.a = 22; struct4.b = 23; struct4.c = 24; struct4.d = 25; struct4.e = 26; struct4.f = 27; struct4.g = 28; T pole1[] = { struct1, struct2, struct3, struct4 }; T result = CtiPrvekPole1(pole1, 2); printf("Cti prvek pole1 : %c\n", result.b); }

How should I get that struct? I used 8 bytes, because one structure has 7 bytes, so it should 8 bytes with aligment. Am I right?

Thanks.

最满意答案

您的想法是正确的,但您的代码不正确。 你有:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; }

所以你将一个地址(一个指针)移动到result占用的前四个字节的内存中。 您需要移动数据。

这样做的C代码是:

T result; result = pole[index]; return result;

将pole[index]的数组中的8个字节复制到result ,然后返回结果。

实际上,您甚至不需要CtiPrvekPole1方法。 你可以写:

T pole1[] = { struct1, struct2, struct3, struct4 }; T result = pole1[2];

如果你真的想用汇编语言来做,那么你必须得到源地址和目标地址并进行复制。 这是一种方法:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov ecx, [eax + ebx * 8]; // ecx = source address lea edx, result // edx = destination address // copy first four bytes mov eax, [ecx] mov [edx], eax // copy next four bytes mov eax, [ecx+4] mov [edx+4], eax } return result; }

Your thinking is correct, but your code is incorrect. You have:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; }

So you're moving an address (a pointer) into the first four bytes of memory that's occupied by result. You need to move the data.

The C code to do that would be:

T result; result = pole[index]; return result;

That copies the 8 bytes from the array at pole[index] into result, and then returns the result.

In fact, you don't even need the CtiPrvekPole1 method. You could just write:

T pole1[] = { struct1, struct2, struct3, struct4 }; T result = pole1[2];

If you really want to do it in assembly language, then you have to get the source and destination addresses and copy. Here's one way to do it:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov ecx, [eax + ebx * 8]; // ecx = source address lea edx, result // edx = destination address // copy first four bytes mov eax, [ecx] mov [edx], eax // copy next four bytes mov eax, [ecx+4] mov [edx+4], eax } return result; }x86程序集 - 从具有结构的数组中获取结构(x86 assembly - get structure from an array with structures)

我创建了一个带结构的数组,我需要使用索引和指向数组的指针从数组中获取结构。

struct T{ char a, b, c, d, e, f, g; }; T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; } int _tmain(int argc, _TCHAR* argv[]) { T struct1, struct2, struct3, struct4; struct1.a = 1; struct1.b = 2; struct1.c = 3; struct1.d = 4; struct1.e = 5; struct1.f = 6; struct1.g = 7; struct2.a = 8; struct2.b = 9; struct2.c = 10; struct2.d = 11; struct2.e = 12; struct2.f = 13; struct2.g = 14; struct3.a = 15; struct3.b = 16; struct3.c = 17; struct3.d = 18; struct3.e = 19; struct3.f = 20; struct3.g = 21; struct4.a = 22; struct4.b = 23; struct4.c = 24; struct4.d = 25; struct4.e = 26; struct4.f = 27; struct4.g = 28; T pole1[] = { struct1, struct2, struct3, struct4 }; T result = CtiPrvekPole1(pole1, 2); printf("Cti prvek pole1 : %c\n", result.b); }

我应该如何获得该结构? 我使用了8个字节,因为一个结构有7个字节,所以它应该是带有对齐的8个字节。 我对吗?

谢谢。

I created an array with structures and I need to get the structure from the array using an index and a pointer to the array.

struct T{ char a, b, c, d, e, f, g; }; T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; } int _tmain(int argc, _TCHAR* argv[]) { T struct1, struct2, struct3, struct4; struct1.a = 1; struct1.b = 2; struct1.c = 3; struct1.d = 4; struct1.e = 5; struct1.f = 6; struct1.g = 7; struct2.a = 8; struct2.b = 9; struct2.c = 10; struct2.d = 11; struct2.e = 12; struct2.f = 13; struct2.g = 14; struct3.a = 15; struct3.b = 16; struct3.c = 17; struct3.d = 18; struct3.e = 19; struct3.f = 20; struct3.g = 21; struct4.a = 22; struct4.b = 23; struct4.c = 24; struct4.d = 25; struct4.e = 26; struct4.f = 27; struct4.g = 28; T pole1[] = { struct1, struct2, struct3, struct4 }; T result = CtiPrvekPole1(pole1, 2); printf("Cti prvek pole1 : %c\n", result.b); }

How should I get that struct? I used 8 bytes, because one structure has 7 bytes, so it should 8 bytes with aligment. Am I right?

Thanks.

最满意答案

您的想法是正确的,但您的代码不正确。 你有:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; }

所以你将一个地址(一个指针)移动到result占用的前四个字节的内存中。 您需要移动数据。

这样做的C代码是:

T result; result = pole[index]; return result;

将pole[index]的数组中的8个字节复制到result ,然后返回结果。

实际上,您甚至不需要CtiPrvekPole1方法。 你可以写:

T pole1[] = { struct1, struct2, struct3, struct4 }; T result = pole1[2];

如果你真的想用汇编语言来做,那么你必须得到源地址和目标地址并进行复制。 这是一种方法:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov ecx, [eax + ebx * 8]; // ecx = source address lea edx, result // edx = destination address // copy first four bytes mov eax, [ecx] mov [edx], eax // copy next four bytes mov eax, [ecx+4] mov [edx+4], eax } return result; }

Your thinking is correct, but your code is incorrect. You have:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov eax, [eax + ebx * 8]; mov result, eax; } return result; }

So you're moving an address (a pointer) into the first four bytes of memory that's occupied by result. You need to move the data.

The C code to do that would be:

T result; result = pole[index]; return result;

That copies the 8 bytes from the array at pole[index] into result, and then returns the result.

In fact, you don't even need the CtiPrvekPole1 method. You could just write:

T pole1[] = { struct1, struct2, struct3, struct4 }; T result = pole1[2];

If you really want to do it in assembly language, then you have to get the source and destination addresses and copy. Here's one way to do it:

T CtiPrvekPole1(T *pole, int index){ T result; _asm{ mov eax, pole; mov ebx, index; mov ecx, [eax + ebx * 8]; // ecx = source address lea edx, result // edx = destination address // copy first four bytes mov eax, [ecx] mov [edx], eax // copy next four bytes mov eax, [ecx+4] mov [edx+4], eax } return result; }