UNIT -III
Instruction Set and Assembly Language Programming
of 8086
Instruction formats, Addressing modes,
Instruction Set
Assembler Directives,
Procedures, Macros
Simple Programs involving Logical
Branch and Call Instructions
Sorting Evaluating Arithmetic Expressions
String Manipulations
UNIT-III
The instruction format contains two fields
operation code / opcode
Operand field
OPERATION CODE / OPCODE:
It indicates the type of the operation to be performed by CPU
Example : MOV , ADD …
OPERAND:
The CPU executes the instruction using the information resides in these fields .
There are six general formats of instructions in 8086 instruction set.
The instruction of 8086 vary from 1to 6 bytes length
ONE BYTE INSTRUCTION:
It is only one byte long and may have implied data or register operands.
The least three significant 3 bits of the opcode are used for specifying register
operand if any otherwise all the 8 bits form an opcode and the operands are implied.
REGISTER TO REGISTER
The format is 2 byte long
The first byte of the code specifies the opcode and width
The second byte of the code shows the register operand and R/M field
The Register represented by REG is one of the operands . The R/M field specifies
another register or memory location .ie the other operand
REGISTER TO/FROM MEMORY WITH NO DISPLACEMENT
The format is 2 byte long
This is similar to the register to register format except for the MOD field is shown.
The MOD field shows the mode of addressing
REGISTER TO/FROM MEMORY WITH DISPLACEMENT
The format contains one or two additional bytes for displacement along with 2 bytes
Register to/from memory with no displacement.
IMMEDIATE OPERAND TO REGISTER
The first byte as well as the 3 bits from the second byte which are used for REG field
in case of Register to register format or used for OPCODE.
It also contains one are two bytes of data.
IMMEDIATE OPERAND TO MEMORY WITH 16 BIT DISPLACEMENTS
It requires 5 to 6 bytes for coding
The first two bytes contains the information regarding OPCODE,MOD and R/M fields
The remaining 4 bytes contains 2 bytes of displacement and 2 bytes of data
ADDRESSING MODES OF 8086
According two the flow of instructions may be categorized as
1. Sequential Control flow instructions
2. Control transfer instructions
Sequential control flow instructions are the instructions which after execution
transfer control to the next instruction appearing immediately. The control transfer
instructions transfer control to some predefined address or the address somehow
specified in the instruction after their execution.
What is addressing mode?
The different ways in which a source operand is denoted in an instruction are known
as addressing mode the addressing modes for sequential control flow instructions
are
1. Immediate Addressing Mode
2. Direct Addressing mode
3. Register Addressing mode
4. Register Indirect Addressing mode
5. Indexed Addressing Mode
6. Register Relative addressing mode
7. Based indexed addressing mode
8. Relative based indexed Addressing mode
IMMEDIATE ADDRESSING MODE
The addressing mode in which the data operand is a part of the instruction itself is
known as immediate addressing mode.
Example
MOV DL, 08H
The 8-bit data (08H) given in the instruction is moved to DL
(DL) 08H
MOV AX, 0A9FH
The 16-bit data (0A9FH) given in the instruction is moved to AX register
(AX) 0A9FH
DIRECT ADDRESSING MODE
The addressing mode in which the effective address of the memory location at which
the data operand is stored is given in the instruction. The effective address (Offset) is
just a 16-bit number written directly in the instruction.
Example:MOV BX, [1354H]
MOV BL, [0400H]
The square brackets around the 1354H denote the contents of the memory location.
When executed, this instruction will copy the contents of the memory location into BX
register. This addressing mode is called direct because the displacement of the operand
from the segment base is specified directly in the instruction.
REGISTER ADDRESSING MODE
The instruction will specify the name of the register which holds the data to be operated by the
instruction. All registers except IP may be used in this mode
Example:
MOV CL, DH
The content of 8-bit register DH is moved to another 8-bit register CL
(CL) (DH)
REGISTER INDIRECT ADDRESSING MODE
This addressing mode allows data to be addressed at any memory location through an
offset address held in any of the following registers: BP, BX, DI & SI.
Example
MOV AX, [BX]; suppose the register BX contains 4895H, then the contents
; 4895H are moved to AX
ADD CX, {BX}
INDEXED ADDRESSING MODE
In this addressing mode, the operands offset address is found by adding the contents of
SI or DI register and 8-bit/16-bit displacements. DS and ES are the default segments for
index registers SI and DI respectively. This is the special case of the of register indirect
addressing mode.
Example
MOV BX, [SI+16], ADD AL, [DI+16]
REGISTER RELATIVE ADDRESSING MODE
In register relative Addressing, BX, BP, SI and DI is used to hold the base value for
effective address and a signed 8-bit or unsigned 16-bit displacement will be specified in
the instruction. In case of 8-bit displacement, it is sign extended to 16-bit before adding
to the base value. When BX holds the base value of EA, 20-bit physical address is
calculated from BX and DS.When BP holds the base value of EA, BP and SS is used.
Example:
MOV AX, [BX + 08H] MOV AX, 08H [BX]
BASED INDEXED ADDRESSING MODE
In this addressing mode, the offset address of the operand is computed by summing the
base register to the contents of an Index register. The default segment registers may be
ES or DS
Example:
MOV DX, [BX + SI] MOV DX, [BX][SI]
RELATIVE BASED INDEXED ADDRESSING MODE
In this addressing mode, the operands offset is computed by adding the base register
contents. An Index registers contents and 8 or 16-bit displacement.
Example
MOV AX, [BX+DI+08]
ADD CX, [BX+SI+16]
CONTROL TRANSFER INSTRUCTIONS ADDRESSING MODES /BRANCH ADDRESSING
MODE
The control transfer instructions transfer control to some predefined address or the
address somehow specified in the instruction after their execution
Examples : INT , CALL ,RET and JUMP instructions
The control transfer instruction the addressing modes depend upon whether destination
location is within the same segment or a different one .It also depends on the method of
passing the destination address to the processor
Basically there are two methods for passing control transfer instructions
1. Intersegment addressing mode
2. Intrasegment addressing mode
INTRASEGMENT ADDRESSING MODE
If the destination location is within the same segment the mode is called intrasegment
addressing mode
There are two types
1. Intrasegement direct mode
2. Intrasegment indirect mode
INTRASEGMENT DIRECT MODE:
In this mode the address to which the control is to be transferred lies within the
segment in which the control transfer instruction lies and appears directly in the
instruction as an immediate displacement value .The displacement is computed relative
to the content of the instruction pointer IP.
JMP SHORT LABEL;
is a control transfer instruction following intra segment direct mode. Here, SHORT LABEL
represents a signed displacement.
INTRASEGMENT INDIRECT MODE :
In this mode the displacement to which the control is to be transferred is in the same
segment in which the control transfer instruction lies but it is passed to the instruction
indirectly Here the branch address is found as the content of a register or a memory
location .
Example
JMP [AX]
INTERSEGMENT ADDRESSING MODE
If the destination location is in the different segment the mode is called intersegment
addressing mode
There are two types
1. Intersegment direct mode
2. Intersegment indirect mode
INTERSEGMENT DIRECT MODE:
In this mode the address to which the control is to be transferred is in a different
segment this addressing mode provides a means of branching from one code segment to
another code segment. Here the CS and IP of the destination address are specified
directly in the instruction.
Example
JMP 2000H: 3000H;
INTERSEGMENT INDIRECT MODE :
In this the address to which the control is to be transferred lies in a different segment
and it is passed to the instruction indirectly .Content of memory block containing four
bytes IP(LSB) ,IP(MSB),CS(LSB) and CS(MSB) sequentially The starting address of the
memory block may be referred using any of the addressing mode except immediate
mode .
Example
JMP [5000H];
INSTRUCTION SET OF 8086
The 8086 microprocessor supports 8 types of instructions −
Data Transfer Instructions
Arithmetic Instructions
logical Instructions
String Instructions
Program Execution Transfer Instructions (Branch & Loop Instructions)
Processor Control Instructions
Iteration Control Instructions
Interrupt Instructions
1. DATA TRANSFER INSTRUCTIONS
These instructions are used to transfer the data from the source operand to the
destination operand. Following are the list of instructions under this group −
INSTRUCTION TO TRANSFER A WORD
MOV − Used to copy the byte or word from the provided source to the provided
destination.
PPUSH − Used to put a word at the top of the stack.
POP − Used to get a word from the top of the stack to the provided location.
PUSHA − Used to put all the registers into the stack.
POPA − Used to get words from the stack to all registers.
XCHG − Used to exchange the data from two locations.
XLAT − Used to translate a byte in AL using a table in the memory.
INSTRUCTIONS FOR INPUT AND OUTPUT PORT TRANSFER
IN − Used to read a byte or word from the provided port to the accumulator.
OUT − Used to send out a byte or word from the accumulator to the provided
port.
INSTRUCTIONS TO TRANSFER THE ADDRESS
LEA − Used to load the address of operand into the provided register.
LDS − Used to load DS register and other provided register from the memory
LES − Used to load ES register and other provided register from the memory.
INSTRUCTIONS TO TRANSFER FLAG REGISTERS
LAHF − Used to load AH with the low byte of the flag register.
SAHF − Used to store AH register to low byte of the flag register.
PUSHF − Used to copy the flag register at the top of the stack.
POPF − Used to copy a word at the top of the stack to the flag register.
2. ARITHMETIC INSTRUCTIONS
These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc.
Following is the list of instructions under this group −
INSTRUCTIONS TO PERFORM ADDITION
ADD − Used to add the provided byte to byte/word to word.
ADC − Used to add with carry.
INC − Used to increment the provided byte/word by 1.
AAA − Used to adjust ASCII after addition.
DAA − Used to adjust the decimal after the addition/subtraction operation.
INSTRUCTIONS TO PERFORM SUBTRACTION
SUB − Used to subtract the byte from byte/word from word.
SBB − Used to perform subtraction with borrow.
DEC − Used to decrement the provided byte/word by 1.
NPG − Used to negate each bit of the provided byte/word and add 1/2’s
complement.
CMP − Used to compare 2 provided byte/word.
AAS − Used to adjust ASCII codes after subtraction.
DAS − Used to adjust decimal after subtraction.
INSTRUCTION TO PERFORM MULTIPLICATION
MUL − Used to multiply unsigned byte by byte/word by word.
IMUL − Used to multiply signed byte by byte/word by word.
AAM − Used to adjust ASCII codes after multiplication.
INSTRUCTIONS TO PERFORM DIVISION
DIV − Used to divide the unsigned word by byte or unsigned double word by
word.
IDIV − Used to divide the signed word by byte or signed double word by word.
AAD − Used to adjust ASCII codes after division.
CBW − Used to fill the upper byte of the word with the copies of sign bit of the
lower byte.
CWD − Used to fill the upper word of the double word with the sign bit of the
lower word.
3. LOGICAL INSTRUCTIONS
These instructions are used to perform operations where data bits are involved,
i.e. operations like logical, shift, etc.
Following is the list of instructions under this group −
INSTRUCTIONS TO PERFORM LOGICAL OPERATION
NOT − Used to invert each bit of a byte or word.
AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.
TEST − Used to add operands to update flags, without affecting operands.
INSTRUCTIONS TO PERFORM SHIFT OPERATIONS
SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.
SAR − Used to shift bits of a byte/word towards the right and copy the old MSB
into the new MSB.
INSTRUCTIONS TO PERFORM ROTATE OPERATIONS
ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.
4. STRING INSTRUCTIONS
String is a group of bytes/words and their memory is always allocated in a
sequential order.
Following is the list of instructions under this group −
REP − Used to repeat the given instruction till CX ≠ 0.
REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF
= 1.
MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided
memory location to the I/O port.
SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.
LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.
5. PROGRAM EXECUTION TRANSFER INSTRUCTIONS (BRANCH AND LOOP INSTRUCTIONS)
These instructions are used to transfer/branch the instructions during an execution. It
includes the following instructions −
Instructions to transfer the instruction during an execution without any condition −
CALL − Used to call a procedure and save their return address to the stack.
RET − Used to return from the procedure to the main program.
JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
JAE/JNB − Used to jump if above/not below instruction satisfies.
JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
JC − Used to jump if carry flag CF = 1
JE/JZ − Used to jump if equal/zero flag ZF = 1
JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
JLE/JNG − Used to jump if less than/equal/if not greater than instruction
satisfies.
JNC − Used to jump if no carry flag (CF = 0)
JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
JNO − Used to jump if no overflow flag OF = 0
JNP/JPO − Used to jump if not parity/parity odd PF = 0
JNS − Used to jump if not sign SF = 0
JO − Used to jump if overflow flag OF = 1
JP/JPE − Used to jump if parity/parity even PF = 1
JS − Used to jump if sign flag SF = 1
6. PROCESSOR CONTROL INSTRUCTIONS
These instructions are used to control the processor action by setting/resetting the flag
values.
Following are the instructions under this group −
STC − Used to set carry flag CF to 1
CLC − Used to clear/reset carry flag CF to 0
CMC − Used to put complement at the state of carry flag CF.
STD − Used to set the direction flag DF to 1
CLD − Used to clear/reset the direction flag DF to 0
STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.
7. ITERATION CONTROL INSTRUCTIONS
These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX
=0
LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX =
0
LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 &
CX = 0
JCXZ − Used to jump to the provided address if CX = 0
8. INTERRUPT INSTRUCTIONS
These instructions are used to call the interrupt during program execution.
INT − Used to interrupt the program during execution and calling service
specified.
INTO − Used to interrupt the program during execution if OF = 1
IRET − Used to return from interrupt service to the main program
ASSEMBLER DIRECTIVES
Assembler directives are the Instructions to the Assembler, linker and loader
regarding the program being executed. also called ‘pseudo instructions. Control the
generation of machine codes and organization of the program; but no machine codes
are generated for assembler directives.
They are used to
› specify the start and end of a program
› attach value to variables
› allocate storage locations to input/ output data
› define start and end of segments, procedures, macros etc..
ASSUME
Used to tell the assembler the name of the logical segment it should use for a
specified segment. You must tell the assembler that what to assume for any segment
you use in the program.
Example
ASSUME: CODE
Tells the assembler that the instructions for the program are in segment named CODE.
DB – Defined Byte
Used to declare a byte type variable or to set aside one or more locations of type byte in
memory.
Example
PRICES DB 49H, 98H, 29H:
Declare array of 3 bytes named PRICES and initialize 3 bytes as shown.
DD – Define Double Word
Used to declare a variable of type doubleword or to reserve a memory location which
can be accessed as doubleword.
DQ – Define Quadword
Used to tell the assembler to declare the variable as 4 words of storage in memory.
DT – Define Ten Bytes
Used to tell the assembler to declare the variable which is 10 bytes in length or reserve
10 bytes of storage in memory.
DW – Define Word
Used to tell the assembler to define a variable type as word or reserve word in memory.
DUP: used to initialize several locations and to assign values to location
END – End the Program
To tell the assembler to stop fetching the instruction and end the program execution.
ENDP – it is used to end the procedure.
ENDS – used to end the segment.
EQU – EQUATE
Used to give name to some value or symbol.
EVEN – Align On Even Memory Address
Tells the assembler to increment the location counter to the next even address if
it is not already at an even address.
EXTRN
Used to tell the assembler that the name or labels following the directive
are in some other assembly module.
GLOBAL – Declares Symbols As Public Or Extrn
Used to make the symbol available to other modules.It can be used in place
of EXTRN or PUBLIC keyword.
GROUP – Group related segment
Used to tell the assembler to group the logical segments named
after the directive into one logical segment. This allows the content of all
the segments to be accessed from the same group.
INCLUDE – include source code from file
Used to tell the assembler to insert a block of source code from
the named file into the current source module. This shortens the source
code.
LABEL
Used to give the name to the current value in the location counter. The
LABEL directive must be followed by a term which specifies the type you
want associated with that name.
LENGTH
Used to determine the number of items in some data such as string or array.
NAME
Used to give a specific name to a module when the programs consisting of
several modules.
OFFSET
It is an operator which tells the assembler to determine the offset or
displacement of named data item or procedure from the start of the
segment which contains it.
ORG – Originate
Tells the assembler to set the location counter value.
Example, ORG 7000H sets the location counter value to point to
7000H location in memory.
$ is often used to symbolically represent the value of the location
counter. It is used with ORG to tell the assembler to change the location
according to the current value in the location counter. E.g. ORG $+100.
ARM processor and its Features
A Advanced RISC Machine (ARM) Processor is considered to be the family of a
Central Processing Units that are used in the music players, smartphones, wearables,
tablets and the other consumer electronic devices.
Advanced RISC Machines create a ARM processor architecture hence the name is
ARM.
This needs very few instruction sets and transistors. It is very small in size. This is the
reason that it is a perfect fit for small-size devices.
It has less power consumption along with reduced complexity in its circuits. They can
be applied to various designs such as 32-bit devices and embedded systems. They can
even be upgraded according to user needs.
What is ARM Processor?
An ARM processor is a widely-used computer chip known for its efficiency and
versatility.
Designed by ARM Limited using a streamlined RISC architecture these processors
are licensed to various companies rather than manufactured directly. ARM unique
business model allows tech companies to customize and build processors for diverse
devices, from smartphones and tablets to computers and smart devices.
Their exceptional balance of processing power and energy efficiency has made them
the preferred choice for mobile computing, enabling longer battery life without
compromising performance.
Features of ARM Processor
Multiprocessing Systems
ARM processors are designed to be used in cases of multiprocessing systems where
more than one processor is used to process information. The First AMP processor
introduced by the name of ARMv6K could support 4 CPUs along with its hardware.
Tightly Coupled Memory
The memory of ARM processors is tightly coupled. This has a very fast response
time. It has low latency (quick response) that can also be used in cases of cache
memory being unpredictable.
Memory Management
ARM processor has a management section. This includes Memory Management Unit
and Memory Protection Unit. These management systems become very important in
managing memory efficiently.
Thumb-2 Technology
Thumb-2 Technology was introduced in 2003 and was used to create variable-length
instruction sets. It extends the 16-bit instructions of initial Thumb technology to 32-bit
instructions. It has better performance than previously used Thumb technology.
One-Cycle Execution Time
ARM processor is optimized for each instruction on the CPU. Each instruction is of a
fixed length that allows time for fetching future instructions before executing the
present instructions. ARM has CPI (Clock Per Instruction) of one cycle.
Pipelining
Processing of instructions is done in parallel using pipelines. Instructions are broken
down and decoded in one pipeline stage. The channel advances one step at a time to
increase throughput (rate of processing).
A large number of Registers
A large number of registers are used in ARM processors to prevent large amounts of
memory interactions. Records contain data and addresses. These act as a local
memory store for all operations.
One of the most common electronic architectural designs in the market is Advanced
RISC Machine Architecture, even better than x86, which is very common in the
server market. ARM Architecture is widely used in smartphones, normal phones, and
also in laptops. Though x86 processors have optimized performance ARM Processor
gives cost-effective processors with small size, takes less power to run, and also gives
better battery life.
ARM Processor is not only limited to mobile phones but is also used in Fugaku, the
world’s fastest supercomputer. ARM Processor also gives more feasibility to designs
of hardware designers and also gives control to designer’s supply chains.
Difference between ARM and x86
ARM x86
ARM uses Reduced Instruction Set Computing x86 uses Complex Instruction Set
Architecture (RISC). Architecture (CISC).
x86 works by executing complex
ARM works by executing single instruction per
instructions at once and it requires more
cycle.
than one cycle.
Performance can be optimized by a Software- Performance can be optimized by
based approach. Hardware based approach.
ARM processors require fewer registers, but x86 processors require less memory,
they require more memory. but more registers.
Execution is slower in an x86
Execution is faster in ARM Processes.
Processor.
ARM Processor work by generating multiple
x86 Processors work by executing
instructions from a complex instruction and they
complex statements at a single time.
are executed separately.
ARM x86
ARM processors use the memory which is x86 processors require some extra
already available to them. memory for calculations.
ARM processors are deployed in mobiles which x86 processors are deployed in Servers,
deal with the consumption of power, speed, and Laptops where performance and
size. stability matter.
Advantages of ARM Processor
ARM processors deal with a single processor at a time, which makes it faster and it
also consumes lesser power.
ARM processors work in the case of a multiprogramming system, where more
than one processor is used to process information.
ARM processors are cheaper than other processors, which makes them usable in
mobile phones.
ARM processors are scalable, and this feature helps it in using a variety of
devices.
Disadvantages of ARM Processor
ARM processors are not stable with x86 processors, and due to this, they cannot be
used in Windows Systems.
ARM processors are not capable of very high performance, which limits them to a
variety of applications.
ARM processor execution is a little hard, which requires skilled programmers to
use it.
ARM processor is inefficient in handling Scheduling instructions.
Conclusion
ARM processors have transformed computing with their efficient design. They are
now used in the many devices from smartphones to the servers. Their key strengths
are power efficiency, good performance and adaptability according to the different
needs. In short ARM processors are very important in the tech world helping to create
a new and the advanced products.
Difference between Macro and Procedure
While programming, particularly in languages such as C or assembly, you come
across such terms as macro and procedure (or function). The two are vital that assist in
the creation of good, easy to manage code but are not used in the same manner and are
not the same. You may come across macros and procedures and the differences
between those two tools will help you decide which one should be used in which
situation, making the code more efficient and easily readable. In this article, the reader
will be able to find out what macros and procedures are, their strengths and
weaknesses and the general comparison of the two.
Assembly language is a common intermediate level programming language which is
used for microprocessor programming. This macro and procedure are two concepts in
assembly by which modular programming is implemented. So now let’s understand
how macro and procedure are different from each other.
Macro
Macro is a set of instruction and the programmer can use it anywhere in the program
by using its name.
It is mainly used to achieve modular programming.
So same set of instructions can be used multiple times when ever required by the help
of macro. Wherever macro’s identifier is used, it is replaced by the actual defined
instructions during compilation thereby no calling and return occurs.
Syntax of Macro
%macro macro_name number_of_parameters
<macro body>
%endmacro
Advantages of Macros
Speed: Macros are evaluated during compilation and as such they do not have any
runtime processing which makes them fast.
Reusability: This type enables the code reuse keywords, but do not entitle a
function call overhead.
Simplicity: Macros can help to make the code more readable as compared to the
conventional coding since this reduces on repetition of the same code.
Disadvantages of Macros
Debugging Difficulty: Another disadvantage of macros is that they remain
expanded before the compilation process and that makes the process of debugging
more complicated.
No Type Checking: We can’t perform type checking for macros that may lead to
more problems if used in a wrong way.
Code Bloat: If macros are used inappropriately, the size of the code will be large
because the repeated codes will be written each time the macro is employed.
Procedure
Procedures are also like macro, but they are used for large set of instruction when
macro is useful for small set of instructions
It contains a set of instructions which performs a specific task. It contains three main
parts i.e.,
Procedure name to identify the procedure, procedure body which contains set of
instructions, and RET statement which denotes return statement. Unlike macros,
procedures follow call-return method thereby achieving true modularity.
Syntax of Procedure
procedure_name :
procedure body
……………………..
RET
To call a procedure
CALL procedure_name
After execution of procedure control passes to the calling procedure using RET
statement.
Advantages of Procedures
Modularity: They make writing of code easier since you are able to divide a large
program into small functions that are easy to manage.
Reusability: They may be used in one program or part of one program as well as
in another program or another part of the same Program.
Type Safety: It is crucial when thinking about procedures because these make the
type safe allowing the user to specify input and outputs.
Easier Debugging: Procedures are easily debuggable because if there is an error it
can be contained and found in one function only.
Disadvantages of Procedures
Runtime Overhead: Whenever calling a procedure incurs a runtime overhead due
to the stack that is followed to execute the respective procedure.
Slower than Macros: In certain circumstances, procedures are usually slower than
macros due to the of overhead time required in the function call.
Difference between Macro and Procedure
S.No MACRO PROCEDURE
Procedure contains a set of
Macro definition contains a set of instructions which can be called
instruction to support modular repetitively which can perform a
01. programming. specific task.
It is used for small set of It is used for large set of
instructions mostly less than ten instructions mostly more than ten
02. instructions. instructions.
In case of macro memory In case of procedure memory
03. requirement is high. requirement is less.
CALL and RET CALL and RET
instruction/statements are not instruction/statements are
04. required in macro. required in procedure.
Assembler directive MACRO is Assembler directive PROC is used
used to define macro and to define procedure and assembler
assembler directive ENDM is used directive ENDP is used to indicate
05. to indicate the body is over. the body is over.
Execution time of procedures is
Execution time of macro is less as high as it executes slower than
06. it executes faster than procedure. macro.
Here machine code is created
multiple times as each time Here machine code is created only
machine code is generated when once, it is generated only once
07. macro is called. when the procedure is defined.
In a procedure parameters are
In a macro parameter is passed as passed in registers and memory
08. part of statement that calls macro. locations of stack.
Overhead time does not take place Overhead time takes place during
as there is no calling and calling procedure and returning
09. returning. control to calling program.
ARM7 ARCHITECTURE
WRITE A PROGRAM TO ADD TWO 8 BIT REGISTERS