Structure Chapter 10: Structures t and Macros Structure 관련된변수들의그룹으로이루어진자료구조 template, pattern field structure를구성하는변수 (cf) C언어의 struct 프로그램의 structure 접근 entire structure 또는 individual fields Structure는 procedure의관련이있는인수들을한꺼번에전달할때에편리함 example: file fl directory information Structure 사용하기 structure 정의 structure 변수선언 structure 변수참조 2 Structure 정의 Structure 정의 Employee STRUCT IdNum BYTE "000000000" LastName BYTE 30 DUP(0) Years WORD 0 SalaryHistory DWORD 0,0,0,0 Employee ENDS structure 이름 default 초기값 field 선언은변수선언과같은방식 9 30 2 4 4 4 4 "000000000" (null) 0 0 0 0 0 Idnum Lastname Years SalaryHistory Structure 변수선언 Structure 변수선언 structure 를정의한다음에 structure 변수를선언 COORD STRUCT X WORD? Y WORD? COORD ENDS point1 COORD <5,10> point2 COORD <> worker Employee <> structure 변수이름 structure 정의 structure 이름을 type 으로사용 <> 안에초기값지정 비어있으면 default 초기값지정 3 4
Structure 변수선언 초기값, 배열 Structure 초기값지정 emp Employee <,,,2 DUP(20000)> 3 개의 field 를건너뜀 (default 초기값사용 ) Array of Structure 배열 field 의원소들의일부또는전부를초기화하기위해 DUP 를사용 NumPoints = 3 AllPoints COORD NumPoints DUP(<0,0>) RD_Dept Employee 20 DUP(<>) accounting Employee 10 DUP(<,,,4 DUP(20000) >) Structure 변수참조 변수의 field 참조 ( 형식예 ) worker.salaryhistory ;dword 배열 field mov dx,worker.years mov worker.salaryhistory,20000 y, ; first salary mov worker.salaryhistory+4,30000 ; second salary mov edx,offset worker.lastname mov esi,offset worker mov ax,[esi].years ; invalid (ambiguous) mov ax,(employee PTR [esi]).years 5 6 Structure 변수참조 structure 변수에대한크기참조 TYPE Employee ;TYPE: 자료형의메모리크기 (57) SIZEOF Employee ; SIZEOF=TYPE*LENGTHOF (57*1=57) SIZEOF worker ; 변수 worker의메모리크기 (57) TYPE Employee.SalaryHistory ; 4 (dword 자료형 ) SIZEOF Employee.SalaryHistory ; 4*4=16 (dword 형배열, 4 원소 ) Employee STRUCT IdNum BYTE "000000000" ; 9 LastName BYTE 30 DUP(0) ; 30 Years WORD 0 ; 2 SalaryHistory DWORD 0,0,0,0 ; 16 Employee ENDS ; (57) 예 : 구조체배열참조하는 Loop 모든점을 x, y 좌표를함께증가시키면서저장 (1,1) 1) (2,2) 2) (3,3) 3) NumPoints = 3 AllPoints COORD NumPoints DUP(<0,0>) mov edi,0 ; array index mov ecx,numpoints ; loop counter mov ax,1 ; starting X, Y values L1: mov (COORD PTR AllPoints[edi]).X,ax mov (COORD PTR AllPoints[edi]).Y,ax add edi,type COORD inc ax Loop L1 7 8
Nested Structures Nested structure structure 필드를포함한 structure Nested structure 변수의초기화 nested braces (or brackets) 을사용 Rectangle STRUCT UpperLeft COORD <> LowerRight COORD <> Rectangle ENDS rect1 Rectangle { {10,10}, {50,20} } rect2 Rectangle < <10,10>, <50,20> > Nested structure 변수의접근 ( 예 ) rect1.upperleft.x COORD STRUCT X WORD? Y WORD? COORD ENDS Union union 메모리를공유하는 field 들로구성된자료구조 메모리크기는가장긴 field의크기와같음 union 정의와변수선언및접근 Integer UNION D DWORD 0 W WORD 0 B BYTE 0 Integer ENDS val1 Integer <12345678h> val2 Integer <100h> val3 Integer <> mov val3.b, al mov ax,val3.w add val3.d, eax 9 10 Macros Parameter 가있는 Macro Macro 이름이부여된 assembly lanauge 문장들의 block macro procedure 라고도부름 macro 를사용 ( 호출 ) 할때마다정의된문장의복사본이삽입됨 필요하면 parameter를사용할수있음 (cf) C언어의 #define Macro 정의및호출 mnewline MACRO call Crlf mnewline ; define the macro ; invoke the macro mputchar macro 인수의문자를화면에출력 Definition: Invocation: Expansion: mputchar MACRO char push eax mov al,char call WriteChar pop eax mputchar 'A' 1 push eax 1 mov al, 'A' 1 call WriteChar 1 pop eax LST 파일 call Clf Crlf 로대치됨 11 12
mwritestr Macro mwritestr MACRO buffer mov edx,offset buffer call WriteString str1 BYTE "Welcome!",0 mwritestr str1 Macro Examples mreadstr - reads string from standard input mdumpmem - dumps a range of memory mwrite write string into standard output 1 1 mov edx,offset str1 1 call WriteString 1 잘못된 argument 사용은 expand 될때에확인됨 13 14 mreadstr Macro mdumpmem Macro mreadstr MACRO varname push ecx mov edx,offset varname mov ecx,(sizeof varname) - 1 call ReadString pop p ecx firstname BYTE 30 DUP(?) mreadstr firstname mdumpmem MACRO address, itemcount, componentsize push ebx push ecx push esi mov esi,address mov ecx,itemcount mov ebx,componentsize call DumpMem pop esi pop ecx pop p ebx mdumpmem OFFSET array, 8, 4 15 16
mwrite Macro data 포함 mwrite MACRO text LOCAL string string BYTE text,0 mov edx,offset string call Writestring macro comment ;; data segment ;; define local string ;; code segment LOCAL directive 를사용하여 macro 내에서사용하는변수정의 다른곳에서같은 macro를호출할때에같은이름의변수와구별할수있음 nesting level Nested Macros Nested macro: 다른 macro 를포함하여정의된 macro mwriteln MACRO text mwrite text call Crlf 정의 mwriteln "My Sample Macro Program" 호출 2 2??0002 BYTE "My Sample Macro Program",0 2 2 2 mov edx,offset??0002 2 call Writestring 2 1 call Crlf 17 18