Introduction to
Structures in MASM
Definition: Group related data items under one name
Purpose: Simplify management, improve readability
Comparison: Similar to C structs in high-level languages
Example structure: PERSON with name, age, salary fields
by Tabeen Bokhat
2023-ce-13
Declaring and
Instantiating
Structures
Declaration Instantiation
Use STRUCT and ENDS Define variables of
directives structure type in data
segment
Memory Layout
Fields allocated sequentially in memory
Accessing Structure
Members
Syntax OFFSET Operator Example
Use structureName.member to Calculate address of structure • Load salary field into EBX
access data instance • Zero-extend age into ECX
Nested
Structures
Definition Use Cases
Structures can contain other Model complex data like
structures as members address inside person
record
Example
PERSON struct includes nested ADDRESS struct
Introduction to Macros
in MASM
Definition Benefits Directives
Parameterized Reduce code MACRO to start,
templates for duplication, improve ENDM to end macro
reusable code maintainability definition
Defining and Invoking
Macros
Parameters Invocation Syntax Example
Supports positional and named Call macro name with arguments in PRINT_NUM macro prints integers
arguments code using format string
Conditional Assembly
Directives
.IF Directive
Start conditional assembly blocks
.ELSEIF / .ELSE
Provide alternative code paths
.ENDIF
End conditional block
Example
Use DEBUG_MODE flag to include debug code only
Advanced Conditional
Assembly
.IFDEF / .IFNDEF .IFB / .IFNB Chaining
Test presence or absence of Test for blank or non-blank Use ELSEIFDEF for cascading
symbols macros or symbols conditions
Defining Repeat
Blocks
.UNTIL Directive
.REPEAT Directive
Terminate repeat block with
Begin repeated code expansion
condition
Warning
Use Cases
Avoid excessive unrolling for
Loop unrolling, code generation
maintainability
Summary and Best
Practices
Structures Macros Repeat Blocks
• Align fields carefully; document • Keep macros simple; prefer • Good for repetitive code
layouts procedures for logic generation
• Use nesting for complex data • Use conditional assembly for • Avoid excessive unrolling to
models debug vs release reduce code size
End of Chapter 10
THANK YOU!