unit 4 assembly language programming
Post on 25-May-2015
404 Views
Preview:
DESCRIPTION
TRANSCRIPT
UNIT 4 – Assembly Language Programming
-By
Prof. K. U. Sharma
The 8086 Stack Segment
• The stack segment register holds the starting address of the stack segment
in the memory. Majorly SS is used for the following purposes namely:
1. To hold the temporary results.
2. To hold the return address of the subroutine.
3. Finally to handle the interrupts.
• Stack segment register with the combination SP i.e; the stack pointer
register gives the stack top address.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
The Stack
• In order to save the contents of a register during the execution, so that it can be
used later for purposes.
• To do so, the microprocessor has a area of memory where the contents of
specified registers or memory location can be saved temporarily, and is called
STACK.
• It is a top-down data structure whose elements are accessed using the pointer
that is implemented using the SP and SS registers.
• As we go on storing the data words onto the stack, the pointer goes on
decrementing.
• The process of storing data in the stack is called “Pushing into” the stack and
the reverse process is known as “Popping off” the sack.
• Stack follows the concept of LIFO.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Stack Related Instructions
1. PUSH (Push Word onto Stack):
Syntax: PUSH Source
It is often used to save the contents of a register so that it can be used for other
purposes.
This inst pushes the contents of the specified register or memory location onto
the Stack.
The higher byte is pushed first and then the lower byte.
Example: 1. PUSH AX if AX = 1234H; SS = 4000H; SP = 1200H
2. PUSH [0200H]
NOTE: Always 16 bit data will be pushed.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
2. POP (POP Data off Stack):
Syntax: POP Destination.
This inst is used to perform the reverse working of PUSH.
SP increments by 2 after execution of this inst.
Examples: 1. POP CX if initially SP = 2000H and SS = 4000H
NOTE: CS and IP registers cannot be Popped.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
3. PUSHF (Push 16 bit flag onto Stack):
Syntax: PUSHF
It is used to save the contents of the flag register onto the stack.
Usually this is done whenever the processor is interrupted.
4. POPF (Pop 16 bit flag off Stack):
Syntax: POPF
Used for popping 16 bits off the stack and storing them in the flag register.
SP is incremented by 2 after executing this inst.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Problem:
1. If the current values in the stack segment register and stack pointer are
C000H and FF00H respectively, what is the address of the current top of
stack? How many words of data are currently held in the stack.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Subroutines:
• A subroutine is a collection of inst that are called from one or many other different
locations within a program.
• The last inst of the subroutine tells the processor how to return and where to return.
Subroutine Related Inst:
• CALL (to call the procedure)
Syntax: CALL Procedure Name
This inst is used to call a subroutine. The subroutine may be in the current code segment (intra
segment) or out side the segment (inter segment). The following operations are performed by
this inst internally.
1. Pushes the address of the inst immediately following onto the stack
2. If it is a near CALL, it pushes only 16-bit address of IP
3. If it is a far CALL, it pushes 32-bit address: first 16 bit for CS and last 16 bit for IP
4. Sets the new CS and IP values to transfer control to the procedure.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
• Two types of CALL operations are possible namely: Intra Segment Call and Inter
Segment Call.
• The allowed operand variations are Near-proc, Far-proc, Regptr16, Memptr16 and
Memptr32.
• Out of which Near-proc, Memptr16 and Regptr16 specify the intra segment calls to the
subroutines.
Near-proc:
• Using a Near-proc operand, a subroutine located in the same code segment can be called.
Example: CALL 5555H
Regptr16:
• The Regptr16 operand provides indirect subroutine addressing by specifying an internal
register as the source of a new value for IP.
Example: CALL BX
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Memptr16:
• The Memptr16 operand provides indirect subroutine addressing by specifying a
memory location as the source of a new value for IP.
Example: CALL [BX]
Far-proc:
• This represents a 32 bit immediate operand that is stored in the four bytes that
follow the op-code of the call inst in the program memory.
Example: CALL 1000:0100
Memptr32:
• The pointer for the subroutine is stored in 4 consecutive bytes in the memory.
Example: CALL DWORD PTR [DI]
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
RET (to Return from procedure)
• Syntax: RET POP Value
• This inst is used to exit from the procedure that has been called. This inst pops
the appropriate information off the stack. Following tasks are performed by this
inst internally.
1. For a near procedure, pop one word from the stack and place into IP.
2. For a far procedure, pop two words from stack and place into IP and CS.
• There are two operands available for RET, none and Disp16. There is an
additional option with the return inst; a 2 byte constant can be included
with the return inst.
• For instance RET 2 when executed adds 2 to SP, this discards one word
parameter as part of the return sequence.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
MACROS
• In the case where number of inst appear again and again in the main
program, the listing becomes lengthy so a MACRO is defined.
• Hence a label is assigned with the repeatedly appearing inst.
• The process of assigning a label or MACRO name to the string is called
defining a MACRO.
Syntax:
<MACRO NAME> MACRO
<BODY OF MACRO>
ENDM
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Example:
PPP MACRO
Mov AH, 09H ; display string function
INT 21H ;
ENDM
Calling statement of this MACRO
STR1 DB ‘First String’,’$’
STR2 DB ‘Second String’,’$’
.
.
.
LEA DX, STR1
PPP
LEA DX, STR2
PPP
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
The resulting source code is:
STR1 DB ‘First String’,’$’
STR2 DB ‘Second String’,’$’
.
.
LEA DX, STR1
Mov AH, 09H
INT 21H
LEA DX, STR2
Mov AH, 09H
INT 21H
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Difference b/w Macros and Subroutines
1. In the case of Macro, the complete code of inst is inserted at each place
where the Macro name appears. Whereas in the case of subroutines, they
are called by the CALL inst and the control is transferred to subroutine
where it is called.
2. Macros do not utilize stack, but subroutines do.
3. The executable code in case of Macros becomes lengthy when compared
with subroutines.
4. Time for execution for Macros is less when compared to subroutines.
5. The program making use of subroutines require less memory space for
execution as compared to Macro.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Recursive Procedures
• Sometimes an algorithm is defined in terms of itself i.e; the algorithm is
nested within itself in such a way that its computational procedure calls
upon itself. Such algorithms are called recursive algorithms.
• Care has to be taken to assure that each successive call does not destroy the
parameters and results generated by the previous call .
• This means that each call must store its set of parameters, registers and all
the temporary results.
• The data stored by an application of a procedure is called a frame.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Common Procedure Sharing/Reentrant Procedures
• In a multiprogramming system, it is desirable to allow two or more users to
share procedures. These procedures are called common procedures.
• A common procedure may be shared in serial fashion just like any other
resource. But the problem is that the parameters need to be reinitialized for
every call.
• Hence a procedure must be such that it can be called by another process
before the execution of the procedure due to a prior call is completed. Such
procedure is called reentrant.
• A reentrant code must consists of code, called pure code, which does not
modify itself.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Programs:
1. Write a procedure named SQUARE that squares the contents 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.
2. Identify the type of call, the type of operand, and operation performed by
each of the inst.
1. CALL 1000H
2. CALL WORD PTR [100H]
3. CALL DWORD PTR [BX + SI]
4. RET 6
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Answers:
SQUARE: PROC NEAR
PUSH AX
Mov AL, BL ; place the number in AL
IMUL BL ; multiply with itself
Mov BX, AX
POP AX
RET ; return from procedure
SQUARE: ENDP
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Programs:
1. Write an inst sequence for saving the original contents of flags SF, ZF, AF,
PF, and CF at address 0A000H and clear CF.
2. Find the greater of the two signed numbers and store it in the address
1234H.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Answers:
LAHF ; flag bits in AH loaded
Mov BX, 0000H ; setting the segment address at 0H
Mov DS, BX
Mov [A000H], AH save contents at 0000:A000
CLC ; clear carry flag
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
2. Assuming two 8 bit signed numbers in AL and BL and data segment is
initially at 0000H
CMP AL, BL
JNC LARGE
Mov [1234H], BL
JMP Exit
LARGE: Mov [1234H], AL
Exit: HLT
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Program:
1. Write an inst sequence
1. If bit 15 of DX is set, call SUBA
2. If bit 14 of DX is set but bit 15 is not set, call SUBB
3. If bit 13 of DX is set but bit 14 & 15 are not set, call SUBC
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Answer:
TEST DX, 8000H ; testing bit 15
JZ NEXT1 ; jump if bit is zero
CALL SUBA ; if bit is 1 call SUBA
JMP EXIT
NEXT1: TEST DX, 4000H ; testing bit 14
JZ NEXT2 ; jump if bit is zero
CALL SUBB ; if bit is 1 call SUBB
JMP EXIT
NEXT2: TEST DX, 2000H ; testing bit 13
JZ EXIT ; exit if zero
CALL SUBC
EXIT: HLT
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Program:
1. A data byte at location STATUS controls the calling of four subroutines. If
bit 7 is set, ROUT1 is called, If bit 5 is clear, ROUT2 is called, Rout3 is
called when bits 2 and 3 are high, and ROUT4 is called if bit 0 is clear and
bit 1 is set. These conditions may all exists at one time, so prioritize the
routines in this way: ROUT1, ROUT3, ROUT2 and ROUT4.
2. Write a procedure named CUBE that cubes the contents of BL and places
the results in BX. Assume that this procedure is called from the main
program which is located in the same code segment.
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Answer:
Mov AX, DATA_SEG
Mov DS, AX
Mov AL, STATUS
TEST AL, 80H
JZ NEXT1
CALL ROUT1
JMP EXIT
NEXT1: TEST AL, 0CH
JZ NEXT2
CALL ROUT3
JMP EXIT
NEXT2: TEST AL, 20H
JNZ NEXT3
CALL ROUT2
JMP EXIT
NEXT3: TEST AL, 01H
JNZ EXIT
TEST AL, 02H
JZ EXIT
CALL ROUT4
EXIT: HLT
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
Answer:
CUBE PROC NEAR
PUSH AX
MOV AL, BL
IMUL BL
IMUL BL
MOV BX, AX
POP AX
RET
CUBE ENDP
4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
top related