【问题标题】:assembly intel x86 Structures, and receiving structures as an arguement汇编 intel x86 结构,并接收结构作为参数
【发布时间】: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, listcmp 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 又是该指针的加载地址 (&amp;list),而不是 nullptr 值。所以mov eax,list 对我来说没有意义?

标签: c assembly nasm


【解决方案1】:

如何在程序集中访问结构中的不同元素?

您需要知道 C 编译器为该结构生成的确切内存布局(即它是否以及如何对齐/填充元素)。

假设一个打包的结构你应该能够做这样的事情:

STRUC ITEM
.iNumber RESD 1
.cName RESB 32
ENDSTRUC

; Now let's say you have an item* in ebx:

mov dword [ebx + ITEM.iNumber],123

【讨论】:

  • 就像您在这里所做的那样,我可以将给出的参数作为参数并使用它来访问它们吗?喜欢:mov edi, list, mov eax, 1, lea esi, [edi+eax*4] ;, mov ebx, [esi], mov ecx, dword[ebx+ ITEM.iNumer] 这行得通吗? @迈克尔
  • 另外,STRUC ITEM, .iNumber RESD 1, .cName RESB 32, ENDSTRUC 会出现在 binary_sort: push ebp, mov ebp, esp, @Michael 之前还是之后
  • 结构类型定义可以在你第一次使用它的位置之前或之后,没关系。
  • 结构内部是否可以有一个元素指向另一个相同类型的结构的指针?我将如何在 asm 中做到这一点?类似于item *next
  • 那只是一个 DWORD(或 x86-64 上的 QWORD)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多