首页 文章

x86程序集链接列表的结尾

提问于
浏览
1

设's assume I have a linked list, where each node holds an integer. I am able to traverse the linked list and get the value of each node; however, I'我不知道如何检测到我已到达链表的末尾 . 我知道最后一个节点持有 null ,所以我尝试了 cmp [ebx+ecx*4],0 ,其中基本上ebx是指向函数第一个参数的堆栈指针,ecx是每个节点的增量1 . 问题是链表中的节点也可以保存值0. 0和 null 如何区分?

更新:

所以我尝试过类似的东西,但它最终成了无穷无尽的循环 .

List:
    add eax, [ebx] //add the value at the address of ebx to eax
    lea ebx, [ebx+4] //update the address of ebx to the next field
    cmp ebx, 0 //check if this address is null
    jne List //if so, then stop

是否正确,如果我使用 mov ebx, [ebx+4] ,它将是不正确的,因为ebx现在持有第二个元素的值而不是地址?

2 回答

  • 2

    那应该是一个mov,而不是一个lea:

    mov     ebx,[ebx+4]
    

    使用masm结构的示例代码 . 有一个h2inc.exe程序将.h文件转换为masm .inc文件 . 它包含在masm和ml中,来自microsoft,但在Visual Studio 2003之后停止包含 . 如果感兴趣,你应该能够找到h2inc的副本 .

    .model FLAT
    node    struct
    value   dd      ?
    next    dd      ?
    node    ends
    
            .data
    list0   node    {1, list1}
    list1   node    {2, list2}
    list2   node    {3, list3}
    list3   node    {4, 0}
    
            .code
    _main   proc    near
    ;       ...
            push    ebx
            xor     eax,eax
            lea     ebx,list0
            jmp     for1   ;jmp used to start off with null ptr check
    for0:   add     eax,node.value[ebx]
            mov     ebx,node.next[ebx]
    for1:   or      ebx,ebx
            jnz     for0
            pop     ebx
    ;       ...
    
  • 1

    1)ListNode包含值字段和链接字段(指针),这些是 different 字段 . 为了测试元素是否是最后一个元素,您应该检查 link 变量 .

    struct ListNode {
       int val;
       ListNode* pNext;  // link to the next element
    };
    //...
    if (pNext == nullptr) { // found last element
    

    如果您在程序集中写入,则相同 - 只检查pNext字段的值是否为0.在程序集中,您没有类型,因此您无法区分0表示int,0表示空指针 . 如果访问下一个节点不是通过链接字段,那么它实际上不是链接列表 .

    2)在具有类型系统的语言中,例如C 98/03,'null pointer'和0完全相同,但C 11引入了nullptr值,其类型为 std::nullptr_t ,不同于整数类型 .

相关问题