Nasm 结构体定义

1. 结构体定义

在NASM内部,没有实际意义上的定义结构体类型的机制,NASM使用宏 STRUC 和 ENDSTRUC来定义一个结构体。STRUC有一个参数,它是结构体的名字。能够使用“RESB”类伪指令定义结构体的域,而后使用ENDSTRUC来结束定义。app

以下,定义一个名为“mystruc"的结构体,包含一个long, 一个word, 一个byte和一个字符串。spa

 

 

 

[plain] view plain copy.net

 

  1. struc mytype  
  2.          mt_long:  resd  1  
  3.          mt_word: resw 1  
  4.          mt_byte:  resb  1  
  5.          mt_str:     resb  32  
  6. endstruc  

 

在上面的代码中定义了,mt_long 在偏移地址0处,mt_word在4,mt_byte 在6,mt_str在7。blog

若是想要在多个结构体中使用具备一样名字的成员,能够把结构体定义成这样:ip

 

[cpp] view plain copy字符串

 

  1. struc mytype  
  2.         .long:  resd    1  
  3.         .word:  resw    1  
  4.         .byte:  resb    1  
  5.         .str:   resb    32  
  6. endstruc   

 

2. 结构体声明

声明一个结构体使用”ISTRUC“、”AT“ 和 “IEND”宏。在程序中声明一个“mystruc"结构体,能够像以下代码同样:get

 

使用定义一:flash

 

[cpp] view plain copyit

 

  1. MYSTRUC:  
  2. istruc  
  3.     at  mt_long,    dd      123456  
  4.     at  mt_word,    dw      7890  
  5.     at  mt_byte,        db      'a'  
  6.     at  mt_str,     db      'abcdefg',0  
  7. iend      

 

使用定义二:io

 

[cpp] view plain copy

 

  1. MYSTRUC:  
  2. istruc mytype  
  3.     at  mytype.long,    dd      123456  
  4.     at  mytype.word,    dw      7890  
  5.     at  mytype.byte,    db      'a'  
  6.     at  mytype.str,     db      'abcdefg',0  
  7. iend  


3.  示例一

 

 

[cpp] view plain copy

 

  1.   

[cpp] view plain copy

 

  1. jmp start  
  2. struc mytype  
  3.     .num:       resd    1  
  4.     .str:       resb    32  
  5. endstruc  
  6.   
  7. MYDATA:  
  8. istruc mytype  
  9.     at  mytype.num,     dd  32  
  10.     at  mytype.str,     db   'hello, world', 0  
  11. iend  
  12.   
  13. start:  
  14.     mov ax, cs  
  15.     mov ds, ax  
  16.   
  17.     mov ax, 0b800h  
  18.     mov es, ax  
  19.       
  20.     xor edi,edi  
  21.     xor esi,esi  
  22.     mov ah, 0ch  
  23.     mov esi, MYDATA + mytype.str  
  24.     mov edi, (80 * 10 + 0)*2  
  25.     cld  
  26. .1: lodsb  
  27.     test al, al  
  28.     jmp .2  
  29.     mov [es:edi], ax  
  30.     add di, 2  
  31.     jmp .1  
  32. .2:  
  33.   
  34.     mov ax, 4c00h  
  35.     int 21h  
相关文章
相关标签/搜索