NASM Preprocessor. NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two.

Slides:



Advertisements
Similar presentations
THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.
Advertisements

COP 3402 Systems Programming
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Macro Processor.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
NASM Preprocessor. NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Position Independent Code self sufficiency of combining program.
NASM Preprocessor. NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two.
Chapter 4 Macro Processors
Practical Session 8 Computer Architecture and Assembly Language.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
1 Chapter 4 Macro Processors Professor Gwan-Hwan Hwang Dept. Computer Science and Information Engineering National Taiwan Normal University 9/17/2009.
Joseph L. Lindo Assembly Programming Sir Joseph Lindo University of the Cordilleras.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
An introduction to systems programming
4. Macro Processors1 Chapter IV: Macro Processors Overview: r To study the design and implementation of macro processors. r A macro represents a commonly.
Week 1 Algorithmization and Programming Languages.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
NASM Preprocessor. NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two.
Assembly Code Optimization Techniques for the AMD64 Athlon and Opteron Architectures David Phillips Robert Duckles Cse 520 Spring 2007 Term Project Presentation.
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CSC 221 Computer Organization and Assembly Language
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
Practical Session 6. NASM Preprocessor NASM contains a powerful macro processor, which supports conditional assembly multi-level file inclusion two forms.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Programming Practical2. Initialising Variables (Recap) Initialising variables are done in the.data section Initialising variables are done in.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Practical Session 6 Computer Architecture and Assembly Language.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Practical Session 3.
CSC 221 Computer Organization and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
High-Level Language Interface
MACRO Processors CSCI/CMPE 3334 David Egle.
Computer Architecture and Assembly Language
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Computer Architecture and Assembly Language
Practical Session 4.
Miscellaneous Topics.
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
UNIT – IV MACRO PROCESSORS
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Procedures & Macros Introduction Syntax Difference.
Presentation transcript:

NASM Preprocessor

NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two forms of macro (single-line and multi-line), and a `context stack' mechanism for extra macro power.  Preprocessor directives all begin with a % sign.  The preprocessor collapses all lines which end with a backslash (\) character into a single line. Thus: %define THIS_VERY_LONG_MACRO_IS_DEFINED_TO \ THIS_VALUE will work like a single-line macro without the backslash- newline sequence.

Single-line macros  %define – defines single-line macro (c-style). %define ctrl 0x1F & %define param(a, b) ((a)+(a)*(b)) mov byte [param(2,ebx)], ctrl 'D' expand to mov byte [(2)+(2)*(ebx)], 0x1F & 'D'  When the expansion of a single-line macro contains tokens which invoke another macro, the expansion is performed at invocation time, not at definition time. %define a(x) 1+b(x) %define b(x) 2*x mov ax,a(8) will evaluate in the expected way to mov ax,1+2*8

Single-line macros (cont)  Macros defined with %define are case sensitive. You can use %idefine to define all the case variants of a macro at once.  There is a mechanism which detects when a macro call has occurred as a result of a previous expansion of the same macro, to guard against circular references and infinite loops.  You can overload single-line macros: %define foo(x) 1+x %define foo(x,y) 1+x*y T he preprocessor will be able to handle both types of macro call, by counting the parameters you pass.

Single-line macros (cont)  %undef– undefines defined single-line macro %define foo goo %undef foo mox ax, foo - will expand to the instruction mov eax, foo, since after %undef the macro foo is no longer defined.  To have a reference to an embedded single-line macro resolved at the time that it is embedded, as opposed to when the calling macro is expanded, you need a different mechanism to the one offered by %define. The solution is to use %xdefine, or it's case- insensitive counterpart %xidefine.

Single-line macros (cont)  %define isTrue 1 %xdefine isTrue 1 %define isFalse isTrue %xdefine isFalse isTrue %define isTrue 0 %xdefine isTrue 0 val1: db isFalse ; val1 = ? val1: db isFalse ; val1=? %define isTrue 1 %xdefine isTrue 1 val2: db isFalse ; val2 = ? val2: db isFalse; val2=?

Single-line macros (cont)  %define isTrue 1 %xdefine isTrue 1 %define isFalse isTrue %xdefine isFalse isTrue %define isTrue 0 %xdefine isTrue 0 val1: db isFalse ; val1 = 0 val1: db isFalse ; val1=1 %define isTrue 1 %xdefine isTrue 1 val2: db isFalse ; val2 = 1 val2: db isFalse; val2=1  In the left case, val1 is equal to 0, and val2 is equal to 1. This is because, when a single-line macro is defined using %define, it is expanded only when it is called. As isFalse expands to isTrue, the expansion will be the current value of isTrue. The first time it is called that is 0, and the second time it is 1.  In the left case, each time that isFalse is called, it expands to 1, as that is what the embedded macro isTrue expanded to at the time that isFalse was defined.

multiple-line macros means: the first parameter of the macro this macro gets one parameter  Works with %macro … %endmacro mechanism. %macro prologue 1 push ebp mov ebp,esp sub esp,%1 %endmacro my_func: prologue 12 my_func: push ebp mov ebp,esp sub esp,12  With a macro taking more than one parameter, subsequent parameters would be referred to as %2, %3 and so on.

multiple-line macros (cont)  Multi-line macros, like single-line macros, are case-sensitive, unless you define them using the alternative directive %imacro.  If you need to pass a comma as part of a parameter to a multi- line macro, you can do that by enclosing the entire parameter in braces. %macro silly 2 %2: db %1 %endmacro silly 'a', letter_a ; letter_a: db 'a' silly 'ab', string_ab ; string_ab: db 'ab' silly {13,10}, crlf ; crlf: db 13,10

multiple-line macros (cont)  As with single-line macros, multi-line macros can be overloaded by defining the same macro name several times with different amounts of parameters.  Reserved words can also be overloaded: %macro push 2 push %1 push %2 %endmacro

multiple-line macros (cont)  Macros with a minimum amount of parameters can be defined: %macro name 2+ The mark %2 will be replaced with second parameter and whatever follows it.  Macros with min. to max. amount of parameters: %macro name 0-1 “default” Here “default” will replace %1 in case non is given when invoked.

multiple-line macros – local labels  Defining a macro with an internal label: %macro retz 0 jnz %skip ret %skip: %endmacro In every ‘retz’ invocation, the preprocessor will create some unique label of the