SYSTEM PROGRAMMING HANDOUT#04

5
System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 1 HANDOUT#04 Aim: Implementation of simple macro Theory: A macro is a unit of specification for program generation through expansion. Macro Definition A macro definition is enclosed between a macro header statement and a macro end statement. Macro definitions are typically located at the start of a program. Macro definition consists of: 1. Macro prototype statements 2. One or more model statements 3. Macro preprocessor statements 1. Macro prototype statements. The macro prototype statement declares the name of the macro and the names and kinds of its parameters. The macro prototype statement has the following syntax: <macro name> [<formal parameter specification>[ …..]] where <macro name> appears in the mnemonic field of assembly Statement and <formal parameter specification> is of the form &<parameter name> [<parameter kind>] 2. Model statements. A model statement is a statement from which assembly language statements may be generated during macro expansion. 3. Macro preprocessor statements. A preprocessor statement is used to perform auxiliary functions during macro expansion.

Transcript of SYSTEM PROGRAMMING HANDOUT#04

System Programming Sunita M. Dol, CSE Dept

Walchand Institute of Technology, Solapur Page 1

HANDOUT#04

Aim:

Implementation of simple macro

Theory:

A macro is a unit of specification for program generation through expansion.

Macro Definition

A macro definition is enclosed between a macro header statement and a macro end

statement. Macro definitions are typically located at the start of a program. Macro

definition consists of:

1. Macro prototype statements

2. One or more model statements

3. Macro preprocessor statements

1. Macro prototype statements.

The macro prototype statement declares the name of the macro and the names and

kinds of its parameters. The macro prototype statement has the following syntax:

<macro name> [<formal parameter specification>[ …..]]

where <macro name> appears in the mnemonic field of assembly Statement and

<formal parameter specification> is of the form

&<parameter name> [<parameter kind>]

2. Model statements.

A model statement is a statement from which assembly language statements may

be generated during macro expansion.

3. Macro preprocessor statements.

A preprocessor statement is used to perform auxiliary functions during macro

expansion.

System Programming Sunita M. Dol, CSE Dept

Walchand Institute of Technology, Solapur Page 2

Macro call

A macro is called by writing the macro name in the mnemonic field of an

assembly statement. The macro call has syntax

<macro name> [<actual parameter specification>[,….]]

where actual parameter resembles an operand specification in assembly statement.

Example

The definition of macro INCR is given below

MACRO

INCR &MEM_VAL, &INCR_VAL, &REG

MOVER &REG, &MEM_VAL

ADD &REG, &INCR-VAL

MOVEM &REG, &MEM_VAL

MEND

Macro Expansion

A macro leads to macro expansion. During macro expansion, the use of a macro

name with a set of actual parameters i.e. the macro call statement is replaced by a

sequence of assembly code. To differentiate between the original statement of a

program and the statements resulting from macro expansion, each expanded

statement is marked with a ‘+’ preceding its label field.

There are two kinds of expansions

1. Lexical expansion.

2. Semantic expansion

1. Lexical expansion.

Lexical expansion implies replacement of a character string by another character

string during program generation. It replaces the occurrences of formal parameter

by corresponding actual parameters.

Example

Consider the following macro definition

MACRO

INCR &MEM_VAL, &INCR_VAL, &REG

MOVER &REG, &MEM_VAL

ADD &REG, &INCR-VAL

MOVEM &REG, &MEM_VAL

System Programming Sunita M. Dol, CSE Dept

Walchand Institute of Technology, Solapur Page 3

MEND

Consider macro call statement on INCR

INCR A, B, AREG

The values of formal parameters are:

Formal parameter value

MEM_VAL A

INCR_VAL B

REG AREG

Lexical expansion of the model statement leads to the code

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

2. Semantic expansion

Semantic expansion implies generation of instructions tailored to the requirements

of a specific usage e.g. generation of type specific instruction for manipulation of

byte and word operands. It is characterized by the fact that different use of macro

can lead to codes which differ in the number, sequence and opcodes of

instructions.

Example

Consider the macro definition

MACRO

CLEAR &X, &N

LCL &M

&M SET 0

MOVER AREG, =’0’

.MORE MOVEM AREG, &X+&M

&M SET &M+1

AIF (&M NE N) .MORE

MEND

Consider the macro call CLEAR B, 3

System Programming Sunita M. Dol, CSE Dept

Walchand Institute of Technology, Solapur Page 4

This macro call leads to generation of statements:

+ MOVER AREG, =’0’

+ MOVEM AREG, B

+ MOVEM AREG, B+1

+ MOVEM AREG, B+2

Input:

#include<stdio.h>

#define xyz 5

#define asd 10

#define pf printf

#define sf scanf

void main(void)

{

pf("Enter a no:");

sf("%d",&a);

b=asd+xyz;

pf("xyz=%d",b);

}

Output:

The old program

#include<stdio.h>

#define xyz 5

#define asd 10

#define pf printf

#define sf scanf

void main(void)

{

System Programming Sunita M. Dol, CSE Dept

Walchand Institute of Technology, Solapur Page 5

pf("Enter a no:");

sf("%d",&a);

b=asd+xyz;

pf("xyz=%d",b);

}

Macro Expansion

xyz 5

asd 10

pf printf

sf scanf

The new program

#include<stdio.h>

void main(void)

{

printf("Enter a no:");

scanf("%d",&a);

b=10+5;

printf("xyz=%d",b);

}

Conclusion:

A macro is a unit of specification for program generation through expansion. A

simple macro is implemented in C-language