UNIT - 3
Macro Processors
Q1) Explain Macros facility?
A1)
Writing a macro is another way of ensuring modular programming in assembly language.
The Syntax for macro definition −
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name of the macro.
The macro is invoked by using the macro name along with the necessary parameters. When you need to use some sequence of instructions many times in a program, you can put those instructions in a macro and use it instead of writing the instructions all the time.
For example, a very common need for programs is to write a string of characters in the screen. For displaying a string of characters, you need the following sequence of instructions −
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
In the above example of displaying a character string, the registers EAX, EBX, ECX and EDX have been used by the INT 80H function call. So, each time you need to display on screen, you need to save these registers on the stack, invoke INT 80H and then restore the original value of the registers from the stack. So, it could be useful to write two macros for saving and restoring data.
We have observed that, some instructions like IMUL, IDIV, INT, etc., need some of the information to be stored in some particular registers and even return values in some specific register(s). If the program was already using those registers for keeping important data, then the existing data from these registers should be saved in the stack and restored after the instruction is executed.
Example
Following example shows defining and using macros −
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
msg2 db 'Welcome to the world of,', 0xA,0xD
len2 equ $- msg2
msg3 db 'Linux assembly programming! '
len3 equ $- msg3
When the above code is compiled and executed, it produces the following result −
Hello, programmers!
Welcome to the world of,
Linux assembly programming!
Q2) Explain Macro definition and call, Macro Expansion?
A2)
Definition: macro
A macro name is that of associate abbreviation, that stands for a few of the connected lines of the code. Macros are helpful for that of the subsequent purposes:
To modify and to scale back that of the quantity of the repetitive cryptography
To reduce errors caused by that of the repetitive cryptography
To make that of associate program a lot of clear.
A macro consists of the name, set of the formal parameters and also the body of code. the utilization of that of the macro name with set of the particular parameters is to switch by a number of the code generated by its body. this can be called macro enlargement.
Macros permit that of a coder to outline that of the pseudo operations, usually operations that are generally the fascinating, aren't enforced as that of the a part of the processor instruction, and may be enforced as that of a sequence of directions. every use of a macro generates new of the program instructions; the macro has that of the result of automating writing of that of the program.
Macros may be outlined that are utilized in several of the programming languages, like C, C++ etc. Example macro therein of the C programming. Macros are ordinarily utilized in that of the C to outline little snippets of code. If the macro has the parameters, they're substituted into that of the macro body throughout expansion; therefore, a C macro will mimic that of a C perform. the standard reason for doing this can be to avoid that of the overhead of a call within the straightforward cases, wherever the code is light-weight enough that call overhead has that of a big impact on performance.
For example,
#define scoop (a, b) a>b? A: b
Defines the macro scoop, taking 2 of the arguments a and b. This macro is also called like several C perform, exploitation identical syntax. Therefore, afterward of the pre-processing
z = max(x, y);
Becomes z = x>y? X:y;
While this use of macros is extremely necessary for that of the C, as an example to outline type-safe generic data-types or that of the debugging tools, it's conjointly slow, rather inefficient, and should cause that of variety of pitfalls.
C macros are capable of that of the mimicking functions, making new syntax at intervals a number of the constraints, moreover as increasing into that of the discretionary text (although the compiling program would require that text to be valid C ASCII text file, as an alternative comments), however they need a number of the constraints as that of a programming construct. Macros that mimic functions, as an example, may be referred to as like real functions, however that of a macro can't be passed to it of the perform employing a function pointer, since that of the macro itself has no address.
In programming languages, like C or that of the programing language, a reputation that defines a collection of the commands that are substituted for that of the macro name where the name seems therein of a program (a method referred to as macro expansion) once the program is compiled or the assembled. Macros are kind of like that of the functions therein they'll take arguments and in that they're calls to it of the lengthier sets of directions. in contrast to functions, macros are replaced by that of the the particular commands they represent once the program is ready for the execution. perform directions are traced into a program just once.
Macro enlargement.
A macro decision results in that of the macro enlargement. throughout macro enlargement, the macro statement is replaced by the sequence of the assembly statements.
In the higher than program a macro decision is shown therein of the center of the figure. that's INITZ. that is named throughout that of the program execution? each macro begins with the MACRO keyword at the start and ends thereupon of the ENDM (end macro).whenever a macro is named the complete of the code is substituted within the program wherever it's called. that the resultant of the macro code is shown on the correct most aspect of the figure. Macro job in high level programming languages
(C programming)
#define max(a,b) a>b?a:b
Main ()
The higher than program was written exploitation C programming statements. Defines the macro scoop, taking 2 arguments a and b. This macro is also referred to as like several C perform, exploitation identical syntax. Therefore, when preprocessing
Becomes z = x>y ? x: y;
After macro enlargement, the entire code would seem like this.
#define max(a,b) a>b?a:b
main()
Q3) Explain Nested Macro calls?
A3)
A macro body may contain that of the more macro definitions. However, these nested macro definitions aren't valid till the insertion macro has been that of the expanded! meaning, the insertion of that of the macro should are referred to as, before the nested macros that may be known as.
Example 1:
A macro, which might be wont to outline that of the macros thereupon of the capricious names, could look as that of the follows:
DEFINE MACRO MACNAME
MACNAME MACRO
DB 'I am the macro &MACNAME;.'
ENDM
ENDM
In order to not overload the instance with "knowhow", the nested macro solely introduces itself kindly thereupon of an acceptable character string within the ROM. The call
DEFINE Obiwan
Would outline the macro
Obiwan MACRO
DB 'I am the macro Obiwan.'
ENDM
And the decision
DEFINE Skywalker
Would outline the subsequent macro:
Skywalker MACRO
DB 'I am the macro Skywalker.'
ENDM
Example 2:
A macro is to insert a variable range of NOPs into the program. For this, a macro with a nested REPT block appears to be best-suited:
REPEAT MACRO NOPS
REPT NOPS
NOP
ENDM
ENDM
The macro decision
REPEAT 4
Results in one thing like that:
Line I Addr Code supply
9+ one N 0004 REPT four
10+ one NOP
11+ one ENDM
12+ a pair of 0000 00 NOP
13+ a pair of 0001 00 NOP
14+ a pair of 0002 00 NOP
15+ a pair of 0003 00 NOP
Q4) Explain Advanced macro facilities?
A4)
Advanced macro facilities are aimed to supporting semantic expansion.
Used for:
Performing conditional expansion of model statements and
In writing expansion time loops.
These facilities can be grouped into following.
1. Facilities for alteration of flow of control during expansion.
2. Expansion time variables.
3. Attributes of parameters.
1. ALTERATION OF FLOW OF CONTROL DURING EXPANSION
Two features are provided to facilitate alteration of flow of control during expansion.
1. Expansion time sequencing symbol
2. Expansion time statements
AIF,
AGO and
ANOP
A sequencing symbol (SS) has the syntax. <Ordinary string>
As SS is defined by putting it in the label field of statement in the macro body.
It is used as an operand in an AIF or AGO statement to designate the destination of an expansion time control transfer.
AIF STATEMENT
An AIF statement has the syntax AIF (<expression>) <sequencing symbol>
Where <expression> is a relational expression involving ordinary strings, formal parameters and their attributes and expansion time variables.
If the relational expression evaluates to true, expansion time control is transferred to the statement containing <sequencing symbol> in its label field.
AN AGO STATEMENT
An AGO statement has the syntax AGO <sequencing symbol>
Unconditionally transfers expansion time control to the statement containing <sequencing symbol> in its label field.
AN ANOP STATEMENT
An ANOP statement is written as <sequencing symbol> ANOP
And simply has the effect of defining the sequencing symbol.
2. EXPANSION TIME VARIABLE
Expansion time variables (EV’s) are variables which can only be used during the expansion of macro calls.
A local EV is created for use only during a particular macro call.
A global EV exists across all macro calls situated in a program and can be used in any macro which has a declaration for it.
Local and global EV’s are created through declaration statements with the following syntax: LCL <EV specification> [, <EV specification>] GBL <EV specification> [, <EV specification>]
<EV specification> has the syntax &<EV name> where <EV name> is an ordinary string.
Values of EV‟s can be manipulated through the pre-processor statement SET.
A SET statement is written as<EV specification> SET <SET-expression>
Where <EV specification> appears in the label field and SET in the mnemonic field.
A SET statement assigns the value of <SET-expression> to the EV specified in <EV specification>.
The value of an EV can be used in any field of a model statement, and in the expression of an AIF statement.
A call on macro CONSTANTS is expanded as follows.
The local EV A is created.
The first SET statement assigns the value „1‟ to it. The first DB statement thus declares a byte constant „1‟.
The second SET statement assigns the value „2‟ to A
And the second DB statement declares a constant „2‟.
3. ATTRIBUTES OF FORMAL PARAMETERS
An attribute is written using the syntax <attribute name>‟ <formal parameter spec>
And represents information about the value of the formal parameter,
I.e. about the corresponding actual parameter.
The type, length and size attributes have the names T,L and S
EXAMPLE
Here expansion time control is transferred to the statement having .NEXT in its label field only if the actual parameter corresponding to the formal parameter A has the length of „1‟.
Q5) Explain Design of macro pre-processor?
A5)
And then it translates it into that of an assembly program which does not contain any of the macro definitions and the calls.
2. DETERMINETHEVALUESOFFORMALPARAMETERS
3. MAINTAINEXPANSIONTIMEVARIABLES
4. ORGANIZEEXPANSIONTIMECONTROLFLOW
5. DETERMINEVALUESOFSEQUENCINGSYMBOLS
6. PERFORMEXPANSIONOFAMODELSTATEMENT
Q6) Write The Sequence Of Instructions To Perform The Operation Beta = Alpha+5 Using Sic/xe Instructions?
A6)
23
LDA ALPHA
ADD Si
STA BETA
... ...
ALPHA RESW 1
BETA RESW 1
Q7) What Is The Use Of Td Instruction In Sic Architecture?
A7)
The test device (TD) instruction tests whether the addressed device is ready to send or receive a byte of data. The condition code is set to indicate the result of this test. Setting of < means the device is ready to send or receive, and = means the device is not ready.
Q8) Define The Basic Functions Of Assembler?
A8)
Q9) What Is Meant By Assembler Directives Give Example?
A9)
These are the statements that are not translated into machine instructions, but they provide instructions to assembler itself.
example S1ART,END.BYTE.WORD.RESW and RESB.
Q10) What Are Forward References?
Q10)
It is a reference to a label that is defined later in a program.
Consider the statement
10 1000 STL RETADR
……
……
8o 1036 RETADR RESW 1
The first instruction contains a forward reference RETADR. If we attempt to translate the program line by line, we will unable to process the statement in line10 because we do not know the address that will be assigned to RETADR .The address is assigned later(in line 80) in the program.