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

Slides:



Advertisements
Similar presentations
COP 3402 Systems Programming
Advertisements

Macro Processor.
Macro Processors (MP) Macro: Macro Processors (MP): Examples:
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
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.
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.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Fundamentals of C and C++ Programming Control Structures and Functions.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
NASM Preprocessor. NASM preprocessor  NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two.
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Practical Session 6. NASM Preprocessor NASM contains a powerful macro processor, which supports conditional assembly multi-level file inclusion two forms.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
ECE 526 – Network Processing Systems Design Microengine Programming Chapter 23: D. E. Comer.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Practical Session 6 Computer Architecture and Assembly Language.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Practical Session 3.
13 C Preprocessor.
Information and Computer Sciences University of Hawaii, Manoa
Computer Architecture and Assembly Language
INC 161 , CPE 100 Computer Programming
Computer Architecture and Assembly Language
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Introduction to Compilers Tim Teitelbaum
Computer Organization & Assembly Language
Pre-processor Directives
MACRO Processors CSCI/CMPE 3334 David Egle.
Computer Architecture and Assembly Language
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Computer Architecture and Assembly Language
C Preprocessor(CPP).
Practical Session 4.
Chapter 4 –Requirements for coding in Assembly Language
Computer Architecture and System Programming Laboratory
Conditional Compilation
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)  Individual tokens in single line macros can be concatenated, to produce longer tokens for later processing. This can be useful if there are several similar macros that perform similar functions. For example, consider the definitions: msg1: db "Message number 1",10,0 msg2: db "Second message",10,0 msg3: db "3rd",10,0 Using the following macro with concatenation %+ %define msg(x) msg %+ x We can now perform: push dword msg(2) call printf add esp, 4 For printing the second message

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)  %assign – used to define single-line macros which take no parameters and have a numeric value.  The value can be specified in the form of an expression, and it will be evaluated once, when the %assign directive is processed.  Like %define, macros defined using %assign can be re-defined later, so you can do things like: %assign i i+1

multiple-line macros means: the first parameter of the macro this macro gets only 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 numbers of parameters.

Conditional Assembly  Similarly to the C preprocessor, NASM allows sections of a source file to be assembled only if certain conditions are met.  General syntax: %if ; some code which only appears if is met %elif ; only appears if is not met but is %else ; this appears if neither nor was met %endif  The %else clause is optional, as is the %elif clause. You can have more than one %elif clause as well.

Preprocessor Loops  NASM’s TIMES prefix, though useful, cannot be used to invoke a multi-line macro multiple times. (it is processed after macros have already been expanded).  Alternative form of loop at the preprocessor level: %rep.  %rep and %endrep enclose a chunk of code, which is then replicated as many times as specified by the preprocessor Example: %assign i 0 %rep 64 inc word [table+2*i] %assign i i+1 %endrep

Preprocessor Loops  To break out of a repeat loop part way along, you can use the %exitrep directive to terminate the loop Example: fibonacci: %assign i 0 %assign j 1 %rep 100 %if j > %exitrep %endif dw j %assign k j+i %assign i j %assign j k %endrep fib_number equ ($-fibonacci)/2