【发布时间】:2016-10-20 12:04:36
【问题描述】:
那些在汇编帖子中徘徊的人会注意到,我在试图弄清楚汇编时已经发布了很多,每次都学习新东西。
我一直忙于将 c 代码编译成汇编并运行一个小测试,看看我是否得到相同的结果。我正在翻译的下一段 c 代码有结构,我似乎找不到任何好的、完整的例子来遵循这个。
这是我的 C 代码:
typedef struct item {
int number;
char name[32];
} item;
/**
* Performs a binary sort on the items pointed to by the parameter list.
*
* @param[in/out] list pointer to an array of items
* @param[in] n total number of elements in the array
*/
void binary_sort_c(item *list, int n) {
int i;
int j;
int bottom;
int top;
int middle;
item temp;
if (list != NULL) {
i = 1;
while (i < n) {
temp = list[i];
bottom = 0;
top = i-1;
while (bottom <= top) {
middle = (bottom+top)/2;
if (temp.number < list[middle].number) {
top = middle-1;
} else {
bottom = middle+1;
}
}
j = i-1;
while (j >= bottom) {
list[j+1] = list[j];
j = j-1;
}
list[bottom] = temp;
i = i+1;
}
}
}
现在这是我的问题,我似乎无法弄清楚如何在汇编中使用这个结构。如果通过 c 代码将结构作为参数传递给程序集,我是否需要在程序集中重新制作结构?
我如何在程序集中访问结构中的不同元素?
例如temp.number
如何检查list = NULL 是否在汇编中?
我可以用作模板来工作和理解的小示例代码将是完美的。
【问题讨论】:
-
list只是一个简单的指针参数,因此您可以像往常一样通过与零进行比较来检查 NULL。至于访问成员,你需要知道偏移量。 -
@Jester 那么这足以检查
if (list != NULL)、mov eax, list、cmp eax, 0jne LABEL吗? -
可以,这取决于
list的声明方式。 -
我想知道
mov eax,list到底是什么意思。在 NASM 中,会将list符号的值加载到 eax 中。如果该符号被定义为list dd 0,则 eax 将包含该零的地址,而不是零本身。所以在这种情况下你必须做mov eax,[list] test eax,eax jz detected_null。如果list在 C 中被定义为全局变量item *list = nullptr;,那么mov eax,list又是该指针的加载地址 (&list),而不是nullptr值。所以mov eax,list对我来说没有意义?