Intel IA-32 Assembly Tutorial

Structures

Structures are a grouping of related variables.

		Person STRUCT						;bytes
			SSN		BYTE "000-00-0000"	;11
			LastName	 BYTE 30 DUP(0)		;30
			FirstName	BYTE 30 DUP(0)		;30
			Phone		BYTE "000-000-0000" ;12
			History	DWORD 0,0,0,0		 ;16
		Person ENDS						;99 bytes

		.data
		person1 Person <"123-45-6789","Smith","John","512-555-1212"> ;initialize with <>
		person2 Person <,"Jones","David">							;skip initializing some elements
		person3 Person {"555-66-7777"}								 ;alternatively, use {}
		person4 Person <>											;used default initializers
	

Arrays of structures are declared like this:

		NumOfEmployees = 10
		Employees Person NumOfEmployees DUP(<>)
	

Referencing structure variables, the following hold true:

		TYPE Person			 ;99 bytes
		SIZEOF Person			 ;99 bytes
		SIZEOF person1			;99 bytes
		
		TYPE Person.History	 ;4 bytes
		LENGTHOF Person.History ;4 elements
		TYPE Person.SSN		 ;1 byte
	

Runtime references:

		.data
		person5 Person <>
		.code
		mov ebx,offset person5.SSN
		mov person5.History,1		;first element of History
		mov person5.History+4,3	;second element of History
		mov person5.History+8,5	;third element of History
		mov esi,offset person5
		inc (Person PTR [esi]).History+8
	

Nested Structures

		Point STRUCT
			x WORD ?
			y WORD ?
		Point ENDS
		
		Rectangle STRUCT
			TopLeft	 Point <>
			BottomRight Point <>
		Rectangle ENDS

		.data
		rect1 Rectangle < <5,5>, <35,25> >
		.code
		mov rect1.TopLeft.x,10
		mov esi,offset rect1.TopLeft
		mov (Point PTR [esi]).y,10
	

Unions

Each field in a structure has an offset relative to the first byte of the structure.

All fields in a union, however, start at the same offset.

The storage size of a union is equal to the length of its longest field.

		Number UNION
			dw DWORD 0
			w	WORD	0
			b	BYTE	0
		Number ENDS		;use ENDS for a UNION
	

A structure can also contain a union.

		Identifier STRUCT
			UNION Number
				dw DWORD 0
				w	WORD	0
				b	BYTE	0
			ENDS
			Text BYTE 8 DUP(?),0
		Identifier ENDS
	

Macros

A macro procedure is a named block of assembly language statements. Wherever the macro is used, a copy of it is inserted into the program.

		.686
		.model flat,stdcall
		.stack 8192
		INCLUDE file_to_include.inc ;macros, structures and prototypes maybe included
		.data
		hello	 BYTE "HELLO!",0
		goodbye BYTE "GOODBYE!",0
		.code
		MessageBoxA PROTO, hwnd:DWORD, message:PTR BYTE, windowTitle:PTR BYTE, options:DWORD
		GetActiveWindow PROTO

		popMsg MACRO message:REQ	;message is a REQuired parameter
			ECHO Expanding popMsg	 ;when assembled, this will print
			call GetActiveWindow
			push 0					;options (0 = none)
			push 0					;windowTitle (0 = error)
			lea esi,message
			push esi				;message (= message)
			push eax				;GetActiveWindow returns in EAX (= hwnd)
			call MessageBoxA
		ENDM

		main PROC
			popMsg hello			;use macro popMsg
			popMsg goodbye			;use macro popMsg
			xor eax,eax
			ret
		main ENDP
	
<< < [Page 12 of 12] > >>