Assembly x86 Irvine 中的链表
LinkedList in Assembly x86 Irvine
以下是在 Kip Irvine 的汇编语言 x86 书中找到的链表汇编程序。在 main 中,循环遍历列表并显示所有节点值。该程序没有为循环使用固定计数器,而是检查尾节点中的空指针并在找到时停止循环。我的问题如下:
(a) 谁能解释一下 ?它是如何工作的,它是什么意思?
(b) 有人可以解释一下 (ListNode PTR [esi]).NextPtr 吗?这是什么意思?
INCLUDE Irvine32.inc
ListNode STRUCT
NodeData DWORD ?
NextPtr DWORD ?
ListNode ENDS
TotalNodeCount = 15
NULL = 0
Counter = 0
.data
putc macro ptr
push eax
mov al, ptr
call writechar
pop eax
endm
;to use:
;putc 'a'
LinkedList LABEL PTR ListNode
REPEAT TotalNodeCount
Counter = Counter + 1
ListNode <Counter, ($ + Counter * SIZEOF ListNode)>
;struct variables Counter, and ($+Counter*SIZEOF ListNode) being declared
;
ENDM
ListNode <0,0> ; tail node
.code
main PROC
mov esi,OFFSET LinkedList
; Display the integers in the NodeData fields.
NextNode:
; Check for the tail node.
putc 'a'; ->first node, then third node
mov eax,(ListNode PTR [esi]).NextPtr
cmp eax,NULL
je quit
; Display the node data.
putc 'b' ;->fourth node
mov eax,(ListNode PTR [esi]).NodeData
call WriteDec
call Crlf
; Get pointer to next node.
putc 'c' ;->first node
putc 'd' ;->second node
mov esi,(ListNode PTR [esi]).NextPtr
;references a struct using [esi]
jmp NextNode
quit:
exit
main ENDP
END main
Can someone explain (ListNode PTR [esi]).NextPtr
? What does that mean?
表示ESI
寄存器中有一个指向ListNode
结构体开头的指针。它取消引用该指针,并评估 NextPtr
字段。
基本上就像你在 C 中有以下内容:
ListNode* esi;
...
return esi->NextPtr;
Can someone explain < Counter, ($ + Counter * SIZEOF ListNode) >
? How does it work and what does it mean?
不,老实说我不能。好吧,抱歉,结果证明这是一个非常糟糕的答案。 :-)
不过,我可以告诉你我是怎么想出来的。首先我会去 the documentation for MASM and see if I could spot anything that looked relevant. I would find (or, actually, I already know) that $
means the current value of the location counter, and that SIZEOF
is an operator that returns the number of bytes in the specified type.
所以这个 gobbledygook 看起来像是将 Counter
的值乘以 ListNode
结构的大小,然后加上位置计数器的当前值。
但我仍然不知道尖括号是什么意思。所以我会尝试 Google 搜索,比如“尖括号 MASM”。我得到 this question,这不是很有用,因为它没有答案。在 MASM32 帮助文件中,我看到:
Treats as a single literal string. Angle brackets are often used in macro calls and with the FOR directive to ensure that values in a parameter list are treated as a single parameter . . . The assembler removes one set of angle brackets each time it inserts an argument into a macro expansion.
但这对我也没有太大帮助。
从这里去哪里?好吧,假设代码 有效 ,我会 assemble 它并要求 MASM 生成一个列表文件 (/Fl
)。然后我会检查这个列表文件,看看它对生成的代码有什么实际影响。
更新:我的付出得到了回报,我遇到了an old manual for MASM 6.1 online。我无法在 Microsoft 的在线文档中找到它,但在本手册中,它清楚地说明了 pg。 98:
Defining Structure and Union Variables
Once you have declared a structure or union type, you can define variables of that type. For each variable defined, memory is allocated in the current segment in the format declared by the type. The syntax for defining a structure or union variable is:
[[name]] typename < [[initializer [[,initializer]]...]] >
[[name]] typename { [[initializer [[,initializer]]...]] }
[[name]] typename constant DUP ({ [[initializer [[,initializer]]...]] })
The name is the label assigned to the variable. If you do not provide a name, the assembler allocates space for the variable but does not give it a symbolic name. The typename is the name of a previously declared structure or union type.
You can give an initializer for each field. Each initializer must correspond in type with the field defined in the type declaration. For unions, the type of the initializer must be the same as the type for the first field. An initialization list can also use the DUP
operator.
所以看起来这声明了一个ListNode
类型的未命名变量,括号里的东西是ListNode
结构的初始化器,排序像 C 代码:
struct ListNode { ... } = { Counter, ($ + Counter * sizeof(ListNode)) };
这与解释性评论的微弱尝试相符:
; struct variables Counter, and ($+Counter*SIZEOF ListNode) being declared
因为它使用这些值初始化 ListNode
结构的前两个字段 NodeData
和 NextPtr
。
以下是在 Kip Irvine 的汇编语言 x86 书中找到的链表汇编程序。在 main 中,循环遍历列表并显示所有节点值。该程序没有为循环使用固定计数器,而是检查尾节点中的空指针并在找到时停止循环。我的问题如下:
(a) 谁能解释一下
(b) 有人可以解释一下 (ListNode PTR [esi]).NextPtr 吗?这是什么意思?
INCLUDE Irvine32.inc
ListNode STRUCT
NodeData DWORD ?
NextPtr DWORD ?
ListNode ENDS
TotalNodeCount = 15
NULL = 0
Counter = 0
.data
putc macro ptr
push eax
mov al, ptr
call writechar
pop eax
endm
;to use:
;putc 'a'
LinkedList LABEL PTR ListNode
REPEAT TotalNodeCount
Counter = Counter + 1
ListNode <Counter, ($ + Counter * SIZEOF ListNode)>
;struct variables Counter, and ($+Counter*SIZEOF ListNode) being declared
;
ENDM
ListNode <0,0> ; tail node
.code
main PROC
mov esi,OFFSET LinkedList
; Display the integers in the NodeData fields.
NextNode:
; Check for the tail node.
putc 'a'; ->first node, then third node
mov eax,(ListNode PTR [esi]).NextPtr
cmp eax,NULL
je quit
; Display the node data.
putc 'b' ;->fourth node
mov eax,(ListNode PTR [esi]).NodeData
call WriteDec
call Crlf
; Get pointer to next node.
putc 'c' ;->first node
putc 'd' ;->second node
mov esi,(ListNode PTR [esi]).NextPtr
;references a struct using [esi]
jmp NextNode
quit:
exit
main ENDP
END main
Can someone explain
(ListNode PTR [esi]).NextPtr
? What does that mean?
表示ESI
寄存器中有一个指向ListNode
结构体开头的指针。它取消引用该指针,并评估 NextPtr
字段。
基本上就像你在 C 中有以下内容:
ListNode* esi;
...
return esi->NextPtr;
Can someone explain
< Counter, ($ + Counter * SIZEOF ListNode) >
? How does it work and what does it mean?
不,老实说我不能。好吧,抱歉,结果证明这是一个非常糟糕的答案。 :-)
不过,我可以告诉你我是怎么想出来的。首先我会去 the documentation for MASM and see if I could spot anything that looked relevant. I would find (or, actually, I already know) that $
means the current value of the location counter, and that SIZEOF
is an operator that returns the number of bytes in the specified type.
所以这个 gobbledygook 看起来像是将 Counter
的值乘以 ListNode
结构的大小,然后加上位置计数器的当前值。
但我仍然不知道尖括号是什么意思。所以我会尝试 Google 搜索,比如“尖括号 MASM”。我得到 this question,这不是很有用,因为它没有答案。在 MASM32 帮助文件中,我看到:
Treats as a single literal string. Angle brackets are often used in macro calls and with the FOR directive to ensure that values in a parameter list are treated as a single parameter . . . The assembler removes one set of angle brackets each time it inserts an argument into a macro expansion.
但这对我也没有太大帮助。
从这里去哪里?好吧,假设代码 有效 ,我会 assemble 它并要求 MASM 生成一个列表文件 (/Fl
)。然后我会检查这个列表文件,看看它对生成的代码有什么实际影响。
更新:我的付出得到了回报,我遇到了an old manual for MASM 6.1 online。我无法在 Microsoft 的在线文档中找到它,但在本手册中,它清楚地说明了 pg。 98:
Defining Structure and Union Variables
Once you have declared a structure or union type, you can define variables of that type. For each variable defined, memory is allocated in the current segment in the format declared by the type. The syntax for defining a structure or union variable is:
[[name]] typename < [[initializer [[,initializer]]...]] > [[name]] typename { [[initializer [[,initializer]]...]] } [[name]] typename constant DUP ({ [[initializer [[,initializer]]...]] })
The name is the label assigned to the variable. If you do not provide a name, the assembler allocates space for the variable but does not give it a symbolic name. The typename is the name of a previously declared structure or union type.
You can give an initializer for each field. Each initializer must correspond in type with the field defined in the type declaration. For unions, the type of the initializer must be the same as the type for the first field. An initialization list can also use the
DUP
operator.
所以看起来这声明了一个ListNode
类型的未命名变量,括号里的东西是ListNode
结构的初始化器,排序像 C 代码:
struct ListNode { ... } = { Counter, ($ + Counter * sizeof(ListNode)) };
这与解释性评论的微弱尝试相符:
; struct variables Counter, and ($+Counter*SIZEOF ListNode) being declared
因为它使用这些值初始化 ListNode
结构的前两个字段 NodeData
和 NextPtr
。