从概念上理解以下Struct代码如何在x86 Assembly中工作

时间:2017-03-14 04:34:58

标签: assembly struct x86 masm irvine32

以下代码是由xip处理器的Kip Irvine汇编语言提供的结构代码。我只想在概念上理解这段代码是如何工作的。有人可以提供以下代码如何工作的概念演练吗?非常感谢你。

TITLE Intro to STRUCT               (Struct1.asm)

; This program demonstrates the STRUC directive.
; 32-bit version.
; Last update: 8/13/01.

INCLUDE Irvine32.inc

COORD STRUCT
  X WORD ?
  Y WORD ?
COORD ENDS

typEmployee STRUCT
    Idnum    BYTE 9 DUP(0)
    Lastname BYTE 30 DUP(0)
    Years    WORD 0
    SalaryHistory DWORD 10 DUP(0)
typEmployee ENDS

.data

; Create instances of the COORD structure,
; assigning values to both X and Y:
point1 COORD <5,10>; X = 5, Y = 10 
point2 COORD <10,20>; X = 10, Y = 20 

worker typEmployee <>

; override all fields. Either angle brackets
; or curly braces can be used:
person1 typEmployee {"555223333"}; IdNum is 555223333 
person2 typEmployee <"555223333"> ; IdNum is 555223333 

; override only the second field (skips the IdNum field and initializes the LastName.
;one out of four fields have been initialized, aka Lastname while idNum is skipped)
person3 typEmployee <,"Jones">

;The following is structure field , inserted from left to right 
; skip the first three fields, and
; use DUP to initialize the last field:
person4 typEmployee <,,,3 DUP(20000)>
;--> person4 typEmployee<IdNum, Lastname, Years, SalaryHistory> 

; Create an array of COORD objects:
NumPoints = 3
AllPoints COORD NumPoints DUP(<0,0>)

.code
main PROC


; Get the offset of a field within a structure:
    mov edx,OFFSET typEmployee.SalaryHistory

; The following generates an "undefined identifier" error:
;mov edx,OFFSET SalaryHistory is incorrect, you must use mov edx, OFFSET typEmployee.SalaryHistory 



; The TYPE, LENGTH, and SIZE operators can be applied
; to the structure and its fields:
    mov eax,TYPE typEmployee            ; 82
    mov eax,SIZE typEmployee
    mov eax,SIZE worker
    mov eax,SIZEOF worker

    mov eax,TYPE typEmployee.SalaryHistory  ; 4
    mov eax,LENGTH typEmployee.SalaryHistory    ; 10
    mov eax,SIZE typEmployee.SalaryHistory  ; 40

; The TYPE, LENGTH and SIZE operators can be applied
; to instances of the structure:
    mov eax,TYPE worker     ; 82
    mov eax,TYPE worker.Years       ; 2

; Indirect operands require the PTR operator:
    mov esi,offset worker
    mov ax,(typEmployee PTR [esi]).Years

; Loop through the array of points and set their
; X and Y values:
    mov edi,0
    mov ecx,NumPoints
    mov ax,1
L1:
    mov (COORD PTR AllPoints[edi]).X,ax
    mov (COORD PTR AllPoints[edi]).X,ax
    add edi,TYPE COORD
    inc ax
    ;mov eax, edi 
    ;call writedec
    Loop L1

quit:
     exit
main ENDP
END main

1 个答案:

答案 0 :(得分:1)

这是循环,它通过从1到NumPoints的值填充数组AllPoints内元素的X字段值。其他代码主要是垃圾

 ; Loop through the array of points and set their
 ; X and Y values:
     mov edi,0
     mov ecx,NumPoints
     mov ax,1
 L1:
     mov (COORD PTR AllPoints[edi]).X,ax
     mov (COORD PTR AllPoints[edi]).X,ax
     add edi,TYPE COORD
     inc ax
     ;mov eax, edi 
     ;call writedec
    Loop L1