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  
相關文章
相關標籤/搜索