1. 結構體定義
在NASM內部,沒有實際意義上的定義結構體類型的機制,NASM使用宏 STRUC 和 ENDSTRUC來定義一個結構體。STRUC有一個參數,它是結構體的名字。能夠使用「RESB」類僞指令定義結構體的域,而後使用ENDSTRUC來結束定義。app
以下,定義一個名爲「mystruc"的結構體,包含一個long, 一個word, 一個byte和一個字符串。spa
[plain] view plain copy.net
- struc mytype
- mt_long: resd 1
- mt_word: resw 1
- mt_byte: resb 1
- mt_str: resb 32
- endstruc
在上面的代碼中定義了,mt_long 在偏移地址0處,mt_word在4,mt_byte 在6,mt_str在7。blog
若是想要在多個結構體中使用具備一樣名字的成員,能夠把結構體定義成這樣:ip
[cpp] view plain copy字符串
- struc mytype
- .long: resd 1
- .word: resw 1
- .byte: resb 1
- .str: resb 32
- endstruc
2. 結構體聲明
聲明一個結構體使用」ISTRUC「、」AT「 和 「IEND」宏。在程序中聲明一個「mystruc"結構體,能夠像以下代碼同樣:get
使用定義一:flash
[cpp] view plain copyit
- MYSTRUC:
- istruc
- at mt_long, dd 123456
- at mt_word, dw 7890
- at mt_byte, db 'a'
- at mt_str, db 'abcdefg',0
- iend
使用定義二:io
[cpp] view plain copy
- MYSTRUC:
- istruc mytype
- at mytype.long, dd 123456
- at mytype.word, dw 7890
- at mytype.byte, db 'a'
- at mytype.str, db 'abcdefg',0
- iend
3. 示例一
[cpp] view plain copy
-
[cpp] view plain copy
- jmp start
- struc mytype
- .num: resd 1
- .str: resb 32
- endstruc
-
- MYDATA:
- istruc mytype
- at mytype.num, dd 32
- at mytype.str, db 'hello, world', 0
- iend
-
- start:
- mov ax, cs
- mov ds, ax
-
- mov ax, 0b800h
- mov es, ax
-
- xor edi,edi
- xor esi,esi
- mov ah, 0ch
- mov esi, MYDATA + mytype.str
- mov edi, (80 * 10 + 0)*2
- cld
- .1: lodsb
- test al, al
- jmp .2
- mov [es:edi], ax
- add di, 2
- jmp .1
- .2:
-
- mov ax, 4c00h
- int 21h