Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
The Assembly Language Level
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Macro Processor.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 4 Macro Processors
Introduction to C Programming
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter 2 How to Compile and Execute a Simple Program.
Intermediate PHP (2) File Input/Output & User Defined Functions.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Lists in Python.
Fundamentals of C and C++ Programming Control Structures and Functions.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Objective At the conclusion of this chapter you will be able to:
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
JavaScript, Fourth Edition
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Controlling Computers with Programs When you create a computer program you are creating a set of instructions that tell the computer exactly and completely.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSC 4630 Meeting 7 February 7, 2007.
Programming what is C++
Chapter 4 – C Program Control
Variables, Expressions, and IO
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Scripts & Functions Scripts and functions are contained in .m-files
Python Primer 2: Functions and Control Flow
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,
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
T. Jumana Abu Shmais – AOU - Riyadh
String and Lists Dr. José M. Reyes Álamo.
Chapter 7 Assembly Language
Basics.
C Programming Getting started Variables Basic C operators Conditionals
2.6 The if/else Selection Structure
Introduction to Java Applications
Introduction to Computer Science
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.

m4 macro processor a programming language with a quite different feel than C, Java, etc.: textual substitution (replacement) rather than numeric data processing function evaluation rather than variety of control structures recursion rather than iteration (you can build iteration out of recursion)

m4 macro processor define(name,definition) -- macro definition - instructs m4 to replace each occurrence of "name" by its definition (textual substitution, a.k.a. expansion). You can define a macro once and use it several times. Leading unquoted blanks, tabs, and newlines are ignored. $1,$2,... inside "definition" -- macro parameters (based on position in call) for parameterized expansion ($0 = macro name, $* = all arguments $1 and above) name(arg1,arg2) -- macro call with two parameters (arguments) include(file) -- read in text of "file"

m4 macro processor `text' -- delay expansion of text but strip quotes (note open quote and close quote difference) changequote(^,!) -- change quote characters to other characters eval(expression) -- evaluate an arithmetic expression incr(arg) / decr(arg) -- returns the value incremented or decremented [ shortcuts for eval(arg+1), eval(arg-1) ] ifdef(arg1,arg2,arg3) -- if the first parameter is defined, return the second, otherwise return the third ifelse(arg1,arg2,arg3,arg4) -- if the first parameter is the same string as the second, return the third parameter, otherwise return the fourth (with a provision for nested ifelse evaluation using parameters 4,5,6, and 7)

m4 macro processor index(arg1,arg2) -- returns position within the first parameter where the second parameter begins (0- origin), or -1 if the second parameter is not a substring within the first parameter len(arg) -- returns the number of characters in the first parameter (i.e., the string length) substr(arg1,arg2,arg3) -- substring(string,start,length) where start is 0-origin position translit(arg1,arg2,arg3) -- transliterate first string using match characters of second parameter with substitute characters of third parameter divert(2) -- send output to second stream...

m4 macro processor undivert -- print all streams dnl -- delete rest of line, including newline debugmode(V) -- turns on tracing and debugging output

m4 macro processor typical use in HLL define(N,10) for(i = 0; i < N; i++) for(i = 0; i < 10; i++)

m4 macro processor example use of positional parameters define(swap,$2 $1) swap(3,4) 4 3 swap(a,b) b a

m4 macro processor example use of built-in macro define(tr,`translit($1,abcde,ABCDE)') tr(computer) ComputEr

m4 macro processor example of the need to quote macro name for redefinition define(fn,3) fn 3 define(fn,4) fn 3 define(`fn',4) fn 4

m4 macro processor parameter passing examples – spaces, commas are important.use eval() to interpret string as arithmetic expression define(`show',` ') show(a) show( b ) define(`fn',`eval($1*$2)') fn(3) m4: Bad expression in eval: 3* fn(3 4) m4: Bad expression in eval (excess input): 3 4* fn (3,4) m4: Bad expression in eval: * (3,4) fn(3,4) 12 fn(3,4,5) 12

m4 macro processor counting in m4 % cat m4.script `define(loc,1) ' define(loc,1) `loc ' loc `define(loc,2) ' define(loc,2) `loc ' loc `define(`loc',3) ' define(`loc',3) `loc ' loc `loc+1 ' loc+1 `eval(loc+1) ' eval(loc+1) `define(loc,loc+1) ' define(loc,loc+1) `loc ' loc `define(`loc',loc+1) ' define(`loc',loc+1) `loc ' loc `define(`loc',eval(loc+1))' define(`loc',eval(loc+1)) `loc ' loc `define(`loc',eval(loc+1))' define(`loc',eval(loc+1)) `loc ' loc

m4 macro processor counting in m4 % m4 m4.script define(loc,1) loc 1 define(loc,2) loc 1 define(`loc',3) loc 3 loc eval(loc+1) 4 define(loc,loc+1) loc 3 define(`loc',loc+1) loc 3+1 define(`loc',eval(loc+1)) loc 5 define(`loc',eval(loc+1)) loc 6

m4 macro processor multiply-accumulate (mac) for accumulator machine define(mac,`load($2) mul($3) add($1)') mac(a,b,c) load(b) mul(c) add(a)

Macros in ARM The ARM assembler will replace the macro name with its definition. Macros may contain calls to other macros, nested up to 255 levels Two directives define a macro, MACRO and MEND. MACRO {$label} macroname {$parameter1} {,$parameter2}{,$parameter3}.....code... MEND

Macro A macro prototype statement must appear on the first line following the MACRO directive. The prototype tells the assembler the name of the macro, macroname, and its parameters. A label is optional, but is useful if the macro defines internal labels. Any number of parameters can be used; each must begin with $ to distinguish it from ordinary program symbols. Within the macro body, $label, $parameter, and so on, can be used in the same way as any other variables.

ARM Macros The $label parameter is simply treated as another parameter to the macro. The macro itself describes which labels are defined where. The label does not represent the first instruction in the macro expansion. For example, it is useful in a macro that uses several internal labels, such as a loop, to define each internal label as the base label with a different suffix. Sometimes, a value appends a macro parameter or label. Separate the appended value by a dot. After the assembler recognizes the end of the parameter and label, the assembler ignores the dot.

ARM Macros For example: $label.1 $label.loop $label.$count Default values can be set for parameters by following them with an equals sign and the default value. If the default has a leading or trailing space, the whole value should appear in quotes, as shown in the following code example....{$parameter="default value"}

ARM Macros The MEND directive signifies the end of the macro definition. If the macro contains WHILE/WEND loops, or contains conditional assembly, the WHILE/WEND loop must close before execution reaches the MEND directive. You can also terminate macro expansion with the MEXIT directive, used in conjunction with WHILE/WEND or conditional assembly.

ARM Macros multiply-accumulate (addMul) for ARM MACRO $Label_1: addMul $v1, $v2, $v3 $Label_1 add $v1, $v2, add two terms add $v1, $v1, add 6 to the sum lsl $v1, $v1, multyply by 8 In source code example: addMul r0, r1, r1 … The assembler makes the necessary substitutions add r0, r1, r2 add r0, r0, #6 lsl r0, r0, #3 …