UNIT-4
ALEART:- Dear Valued Audience, I want to extend my sincerest apologies for the recent increase in advertisements on our platform. Due to some technical issues, the ad code was inadvertently placed in a way that led to an excessive number of ads being displayed. I understand how frustrating this must have been, and I am truly sorry for any inconvenience it caused. We are working diligently to resolve the issue and restore a better browsing experience for you. Thank you for your patience and understanding. Warm regards, VMSTUDYPOINT (TRY TO CLOSE ADS IN CORNER )
Q.1] What is stack? How stack is implemented in memory? Explain stack related instruction.
Ans:
The Stack: Sometimes it is necessary to save the contents
of a register during execution so that it can be used for the purposes.For this
purpose microprocessor has a area of memory where the contents of specified
registers or memory location can be saved temporary and this area of memory is
called the stack.
Implementation of stack in memory : The
stack is a block of memory that may be used for storing the contents of the CPU
registers temporarily. The stack is a block of memory locations, which is
accessed using the SP and SS registers.In other words, it is a top-town data structure whose
elements are accessed using a pointer that is implemented using the SP and SS
registers.
As we go on storing the data words into
the stack,the pointer goes on decrementing and on the other hand,the pointer
goes on incrementing as we go on retrieving the word data.
The process of storing the data
in the stack is called “Pushing into” the stack and the reverse process is
known as “Popping off” the stack for each such process,the stack pointer (SP)
is decremented or incremented by two.
The stack is essentially
last-in-first-out (LIFO) data segment.This means that the data,which is pushed
into the stack last,will be on top of stack and will be popped off stack first.
Stack Related Instructions:
1)
PUSH(Push Word Onto Stack)
Syntax: PUSH
Source
Purpose: It is
often necessary to save the contents of a register so that it can be used for
other purposes. This instruction pushes the contents of the specified
register/memory location onto the special area of memory called stack.
2) POP(Pop
Data off Stack)
Syntax: POP Destination
Purpose: The POP instruction is used to perform the reserve working
of PUSH instruction. Again Sp(Stack Pointer) plays main role. The 16-bit data
from stack where SP is currently pointing copy to the destination and SP is automatically
incremented by 2.
3) PUSHF(Push
16-bit flag onto stack)
Syntax:
PUSHF
Purpose: Some time it is necessary to save the contents of the flag
register during execution.Usually this is done whenever the processor is
interrupted.PUSHF instruction pushes the lower 16-bit of the flag register onto
the stack and SP is decremented by two.
4) POPF(Pop
16-bit flags off stack)
Syntax:
POPF
Purpose: This
instruction is reserve of PUSHF
instruction,popping 16-bits off the stack and storing them in the flag register.The SP will incremented by 2 (POPF) after the execution.
Q.2] Explain subroutine.What is the difference
between Subroutine and Macro?
Ans:
Subroutine: A subroutine
is a special segment of program that can be called for execution from any point
in a program.
1)In computers, a subroutine is a
sequence of program instructions that perform a specific task, packaged as a
unit. This unit can then be used in programs wherever that particular task have
to be performed.
2)A subroutine is often coded so that it can be started
(called) several times and from several places during one execution of the
program, including from other subroutines, and then branch back (return) to the
next instruction after the call, once the subroutine’s task is done. It is
implemented by using Call and Return instructions.
3)The subroutine is written to
provide a function that must be performed at various points in the main
program.Instead of including this piece
of code in the main program each time the function needed, it is put into the
program just once as a subroutine. An assembly language subroutine is also
reffered to as a procedure.
4)The instructions provided to
transfer control from the main program to a subroutine and return control back
to the main program are called subroutine-handling instructions.
The different types of subroutine instructions are:
• CALL and RET Instructions: There are two basic instructions in the
instruction set of 8086 for subroutine handling: the call(CALL) and return
(RET) instructions together they provide the mechanism for calling a subroutine
into operation and returning control back to the main program at its completion
.
1) Just like the JMP instruction,CALL
allows implementation of two types of operations: the intrasegment call and the
intersegment call.
2)Every subroutine must end by
executing an instruction that returns control to the main program.This is the
return (RET) instruction.
Difference
Between Subroutine and Macro:
Subroutine |
Macro |
Subroutine are called by
CALL instruction and control is transfer to subroutine where it is called. |
In case of macro the
complete code of instruction is inserted at each place where the macro. |
Subroutine is called
the return address i.e. CS:IP is pushed into the stack. |
Macro does not utilize
the surface of the stack. |
The executable code in
the case of subroutine is not lengthy as compared to macro. |
The executable cod ein
the case of macro becomes lengthy as compare to subroutine. |
Subroutine requires more
time for execution as compared to macro. |
Macro
requires less time for execution as compared to subroutine. |
The program using
subroutine requires less memory space for execution as compare to macro. |
The
program using macro requires more memory space for execution. |
Q.3]
What is macro? Explain nested macro with example.
Ans:
Macro: A macro is a group of instructions that
perform one task,just as a procedure performs a task.The difference is that a
procedure is accessed via a CALL instruction,while a macro is inserted in the
program at the point of usage as a new sequence of instructions.Creating a
macro is very similar to creating a new opcode that can be used in the
program.Macro sequencences execute faster than procedures because there is no
CALL and RET instructions to execute.The macro instructions are placed in your
program by the assembler at the point where they are invoked.
The code that is to be repeated is called
the prototype code,and the prototype code along with the statements for
referencing and terminating it is called the macro definition. Included in the
first statement of a macro definition is the macro’s name.The propcedure for
using a macro is to give macro definition and then cause the macro to be
inserted at various points within a program by placing a statement that
includes the macro’s name at these points.These statements are known as macro
calls.When a macro call is encountered by the assembler,the assembler replaces
the call with the macro’s code.This replacement action is reffered to as macro
expansion.
Nested Macro: It is possible for a macro call to appear within a macro definition.This is referred to as macro nesting and has the limitation that all macros included in the definition of a given macro must be defined before the given macro is called.
Example:
Write a program to compute (diff1-diff2)2.
Solution: DIF MACROVAR1,VAR2
MOV AX,VAR1
SUBAX,VAR2
ENDM
The first macro finds the
difference.
:The second macro calls the first
and computes the difference square.
DIFSQRMACRO
OPR1, OPR2,RESULT
PUSH DX
PUSH AX
DIF OPR1,OPR2
IMUL AX
MOV RESULT,AX
POP AX
POP DX
ENDM
; The main program starts which
calls the nested macro.
:
DIFSQR
VAR1, VAR2,RES
Q.4]Write a procedure named SQUARE that squares the content of BL and places the result in BX.Assume that this procedure is called from the main program ,which is located in the same code segment.(7m)
Solution:
SQUARE PROC
NEAR
PUSH
AX
;save
the register to be used
Mov AL,
BL
;Place the number in AL
IMUL BL
;Multiply with itself
MOV BX, AX
;Save the result
POP AX ;Restore the register used
RET ;Restore from
procedure
SQUARE ENDP
Q.5] Write a procedure named CUBE that cubes the contents of BL and place the result in BX. Asssume that this procedure is called from the main program which is located in the same code segment.
Solution:
CUBE PROC NEAR
PUSH AX
MOV AL,BL
IMUL BL
IMUL BL
MOV BX,AX
POP AX
RET
CUBE ENDP
OR
Explain recursion with example (7or 8m).
ANS: Sometimes an algorithm is defined in terms of itself .That is, the algorithm is nested within itself in such a way that its computational procedure calls upon itself. Such algorithms are said to be recursive.Recursive algorithms may be implemented by having a procedure call itself,but care must be taken to assure that each successive call does not destroy the parameters and results generated by the previous call and to make sure that the procedure does not modify itself.This means that each call must store its set of parameters,registers,and all temporary results in a different place in memory.To guarantee that separate areas of memory are used a stack is normally employed.By pushing the data onto the stack as shown in fig.
It can be
conveniently retrieved in reverse order as the process emerges from its nesting
.The data stored by an application of the procedure is called a frame,and the
8086’s BP register can be used to permit ready access to the individual terms
within a frame.Normally, a frame contains the saved register contents ,the
parameter addresses,and a temporary storage area. Properly storing the
information on the stack provides for an orderly exit. To prevent indefinite
nesting of a recursive algorithm,clearly the algorithm must include a
conditional branch that will eventually allow the nest to be exited.
A trivial example of a recursive
procedure is one for evaluating factorials.Computing a factorial can be done
using a recursive procedure FACT as follows:
BEGIN
FACT(N, RESULT)
SAVE
registers on stack
IF N = 0 THEN
RESULT =
1
ELSE
PUSH address of RESULT
onto stack
PUSH N-1
onto stack
CALL FACT
(N-1, RESULT)
RESULT =
N * RESULT
ENDIF
RESTORE
registers from stack
DELETE parameters from stack
RETURN
The parameters N and RESULT
represent the factorial to be found and the result,respectively.
Q.7] Write a program to exchange the ccontents of AX register with BX register without using MOV and XCHG instruction.
Solution:
SUB CX,
CX ;To clear the
CX register
OR CX,
AX ;Copy contents
of AX into CX
SUB AX,
AX ;To clear AX
register
OR AX,
BX ; Copy contents
of BX into AX
SUB BX,
BX ;To clear BX
register
OR BX,
CX ; Copy contents
of CX into BX
Q.8]Explain the operation of input/output instructions of 8086.
OR
Explain
in detail the input-output instructions of 8086.
Ans: Input/output operations are performed by the 8086
microprocessor that employ isolated I/O using special input and output
instructions together with the I/O port addressing modes.
These instructions,in(IN) and out(OUT),are listed in the
table:
Mnemonics |
Meaning |
Format |
Operation |
IN |
Input direct |
IN Acc,Port |
*Acc←Port |
Input indirect |
IN Acc, DX |
Acc←[DX] |
|
OUT |
Output direct |
OUT Port, Acc |
Port←Acc
|
Output indirect |
OUT DX, Acc |
[DX] ←Acc |
*Acc=AL OR AX
(Input/Output
instructions)
Their mnemonics and formats are
provided together with a brief description of their operations. Note that there
are two different forms of IN and OUT instructions: the direct I/O instructions and variable I/O instructions.Either of these two types of
in structions can be used to transfer a byte or word of data.All data transfers
take place between an I/O devices and the MPU’s accumulator register. For this
reason ,this method of performing I/O is known as accumulator I/O. Byte
transfers involve the AL register, and
word transfers the AX register in fact, specifying AL as the source or
destination register in an I/O instruction indicatses that it corresponds to a
byte transfer.That is byte-wide or word-word input/output is selected by
specifying the accumulator (Acc) in the instruction as AL or AX, respectively.
In a direct I/O instruction, the
address of the I/O port is specified as part of the instruction.Eight bits are
provided for this direct address. For this reason, its value is limited to the
address range from 00H to FFH(first 256+ ports). This range is referred to as
page 0 in the I/O address space.
1. IN (Input Byte or Word from
Port)
Syntax: IN Accumulator,Port
Purpose: Processor has a separate
I/O space from its memory space, among them 65536 (0 to 65535) I/O ports are
available for the programmer to use.These ports can be access directly ,using
the port address,for 65536 port address range is 0000H to FFFFH. The input port
is actually a hardware device connected to the processor’s data bus.Using IN
instruction we can access port.The 8086 allows two different forms of the IN
instruction.
A full 16-bit port address must
be loaded into DX register and instructions like IN AL,DX or IN AX,DX may be
used to read the input port.If the port number
is in between 00H to FFH then another form of IN instruction
may be used as IN AL,86H or IN AX,80H.
Example: If we want to read input port 80H then we
may give instruction like
IN AL, 80H or MOV DX,80H
IN AL,DX
2.OUT(Output Byte or Word to
Port)
Syntax: OUT PAort,Accumulator
Purpose: This instruction is
reserve instruction of IN instruction. With OUT we can send 8-bit or 126-bit
data to an output port. The port address may be loaded into DX (ifv more than
8-bit address) for us ewith OUT DX,AL or OUT DX,AX or specified within the
instruction, as OUT 80H,AL or OUT,
80H,AX.
Example: OUT DX,AL if DX= 2000H
and AL=3EH then port address stored in DX
register is output on the address bus,along w2ith 3E from AL on the data
bus,and 8-bit data ----- -from AL on
the data bus,and 8-bit data from AL will store to 2000H port address.
Q.9] Write a sequence of instructions that inputs the byte of data from input ports at I/O addresses A00016 and B00016 adds these values together,and saves the sum in memory location IO_SUM.
Solution:
MOV DX, A000H ; Set port address
IN AL, Dx ;receive data from
port
MOV BL, AL ;Copy in BL
MOV DX, B000H ;Set port address
IN AL, DX ;receive data from
port
ADD AL, BL ; add data
MOV [IO_SUM],
AL ; store result
Q.10] Explain the I/O address space of 8086.
Ans:
The 8086 has separate memory and input/output address space .The I/O
address space is the place where I/O interfaces, such as pointer and monitor
port,are implemented.The I/O address range is from 0000H to FFFFH.This is 65536
bytes or 64KB
addresses;therefore,unlike memory,I/O addresses are only 16
bits long.Each of these addresses corresponds to one byte or port.The part of
the map from address 0000H through 00fh is referred to as page 0.Certain of the
8086’s I/O instructions can perform only input or output datatransfer operation
to I/O devices located in this part of the I/O address space .Other I/O
instructions can input or output data for devices located anywhere in the I/O
address space.I/O data transfers can be byte wide or word wide.Notice that the
eight locations from address 00f8h through 00ffh are specified as reserved by
microprocessor and should not be used.