我想在 Assembly 中创建一个 4 元素数组:
; Create an array of 4 elements
push BYTE 'a'
push BYTE 'b'
push BYTE 'c'
push BYTE 'd'
我想到数组以相反的顺序存储在 C/C++ 中(第一个元素的内存地址最低,最后一个元素的内存地址最高),所以为了确保我的假设是正确的,我做了以下测试:
#include <stdio.h>
int main()
{
char arr[4] = {'a', 'b', 'c', 'd'};
printf("Address: %d Value: %c\n", arr + 0, *(arr + 0));
printf("Address: %d Value: %c\n", arr + 1, *(arr + 1));
printf("Address: %d Value: %c\n", arr + 2, *(arr + 2));
printf("Address: %d Value: %c\n", arr + 3, *(arr + 3));
return 0;
}
并且输出符合预期:
Address: 3603656 Value: a
Address: 3603657 Value: b
Address: 3603658 Value: c
Address: 3603659 Value: d
这是否意味着我应该像这样在 Assembly 中创建数组:
; Create an array of 4 elements
push BYTE 'd'
push BYTE 'c'
push BYTE 'b'
push BYTE 'a'
并假设它的第一个元素是'a'的地址。或者像以前那样做没有问题,而不是递增 1 以到达下一个元素,我只是递减 1?
编辑: 我的意思是相对于堆栈的相反顺序(向下增长)。
最佳答案
您不是在此处创建数组。 PUSH 和 POP 是针对堆栈执行的,堆栈的通用规则是:后进先出。
如果你想创建数组,那么你可以简单地使用一系列mov
命令:
mov <mem>,<const> // x86 instruction
或者使用数据定义指令
Array DW 'a', 'b', 'c', 'd'
关于c++ - 在 C/C++ 中数组是否以相反的顺序存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779784/