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