SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
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
POPAX
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
POPAX
RET
CUBE ENDP
4/1/2014
Prof. K. U. Sharma, PRMCEAM, Contact:
karthik8777@gmail.com, 9096996329

Weitere ähnliche Inhalte

Was ist angesagt?

Data transfer and manipulation
Data transfer and manipulationData transfer and manipulation
Data transfer and manipulationSanjeev Patel
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipeliningMazin Alwaaly
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
 
Computer architecture register transfer languages rtl
Computer architecture register transfer languages rtlComputer architecture register transfer languages rtl
Computer architecture register transfer languages rtlMazin Alwaaly
 
MASM -UNIT-III
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-IIIDr.YNM
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference InstructionsRabin BK
 
Assembly level language
Assembly level languageAssembly level language
Assembly level languagePDFSHARE
 
Unit 2 assembly language programming
Unit 2   assembly language programmingUnit 2   assembly language programming
Unit 2 assembly language programmingKartik Sharma
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly languageMir Majid
 
Shift Microoperations by Pir Sarfraz RSDT larkana
Shift Microoperations by Pir Sarfraz RSDT larkanaShift Microoperations by Pir Sarfraz RSDT larkana
Shift Microoperations by Pir Sarfraz RSDT larkanaPir Sarfraz Ahmed
 
Instruction cycle presentation
Instruction   cycle presentationInstruction   cycle presentation
Instruction cycle presentationMoniba Irfan
 
Stack Operations
Stack Operations Stack Operations
Stack Operations RidaZaman1
 
Arm architecture
Arm architectureArm architecture
Arm architectureMinYeop Na
 

Was ist angesagt? (20)

Modes of transfer
Modes of transferModes of transfer
Modes of transfer
 
Data transfer and manipulation
Data transfer and manipulationData transfer and manipulation
Data transfer and manipulation
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Computer architecture register transfer languages rtl
Computer architecture register transfer languages rtlComputer architecture register transfer languages rtl
Computer architecture register transfer languages rtl
 
Control Memory
Control MemoryControl Memory
Control Memory
 
MASM -UNIT-III
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-III
 
Unit 1
Unit 1Unit 1
Unit 1
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference Instructions
 
Assembly level language
Assembly level languageAssembly level language
Assembly level language
 
Unit 2 assembly language programming
Unit 2   assembly language programmingUnit 2   assembly language programming
Unit 2 assembly language programming
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
 
Pentium
PentiumPentium
Pentium
 
Shift Microoperations by Pir Sarfraz RSDT larkana
Shift Microoperations by Pir Sarfraz RSDT larkanaShift Microoperations by Pir Sarfraz RSDT larkana
Shift Microoperations by Pir Sarfraz RSDT larkana
 
Instruction cycle presentation
Instruction   cycle presentationInstruction   cycle presentation
Instruction cycle presentation
 
Registers
RegistersRegisters
Registers
 
Stack Operations
Stack Operations Stack Operations
Stack Operations
 
Arm architecture
Arm architectureArm architecture
Arm architecture
 
Memory mapping
Memory mappingMemory mapping
Memory mapping
 
Interrupts
InterruptsInterrupts
Interrupts
 

Ähnlich wie Assembly Language Stack and Procedures Guide

Chap6 procedures &amp; macros
Chap6 procedures &amp; macrosChap6 procedures &amp; macros
Chap6 procedures &amp; macrosHarshitParkar6677
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Central processing unit
Central processing unitCentral processing unit
Central processing unitHeman Pathak
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfHimanshu883663
 
Unit 6 assembly language programming
Unit 6   assembly language programmingUnit 6   assembly language programming
Unit 6 assembly language programmingKartik Sharma
 
Microprocessors-based systems (under graduate course) Lecture 6 of 9
Microprocessors-based systems (under graduate course) Lecture 6 of 9 Microprocessors-based systems (under graduate course) Lecture 6 of 9
Microprocessors-based systems (under graduate course) Lecture 6 of 9 Randa Elanwar
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Janki Shah
 
4bit pc report[cse 08-section-b2_group-02]
4bit pc report[cse 08-section-b2_group-02]4bit pc report[cse 08-section-b2_group-02]
4bit pc report[cse 08-section-b2_group-02]shibbirtanvin
 
4bit PC report
4bit PC report4bit PC report
4bit PC reporttanvin
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization Ganesan Narayanasamy
 
CD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxCD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxKANDE ARCHANA
 

Ähnlich wie Assembly Language Stack and Procedures Guide (20)

Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
Chap6 procedures &amp; macros
Chap6 procedures &amp; macrosChap6 procedures &amp; macros
Chap6 procedures &amp; macros
 
Chapter 6 notes
Chapter 6 notesChapter 6 notes
Chapter 6 notes
 
Chapter 5 notes new
Chapter 5 notes newChapter 5 notes new
Chapter 5 notes new
 
class-Stacks.pptx
class-Stacks.pptxclass-Stacks.pptx
class-Stacks.pptx
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Central processing unit
Central processing unitCentral processing unit
Central processing unit
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
 
Unit 6 assembly language programming
Unit 6   assembly language programmingUnit 6   assembly language programming
Unit 6 assembly language programming
 
Microprocessors-based systems (under graduate course) Lecture 6 of 9
Microprocessors-based systems (under graduate course) Lecture 6 of 9 Microprocessors-based systems (under graduate course) Lecture 6 of 9
Microprocessors-based systems (under graduate course) Lecture 6 of 9
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...
 
Different addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessorDifferent addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessor
 
module-3.pptx
module-3.pptxmodule-3.pptx
module-3.pptx
 
4bit pc report[cse 08-section-b2_group-02]
4bit pc report[cse 08-section-b2_group-02]4bit pc report[cse 08-section-b2_group-02]
4bit pc report[cse 08-section-b2_group-02]
 
4bit PC report
4bit PC report4bit PC report
4bit PC report
 
OpenPOWER Application Optimization
OpenPOWER Application Optimization OpenPOWER Application Optimization
OpenPOWER Application Optimization
 
CD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docxCD CLASS NOTES- UNIT-4.docx
CD CLASS NOTES- UNIT-4.docx
 
seminar 4 sem
seminar 4 semseminar 4 sem
seminar 4 sem
 

Kürzlich hochgeladen

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 

Kürzlich hochgeladen (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 

Assembly Language Stack and Procedures Guide

  • 1. UNIT 4 – Assembly Language Programming -By Prof. K. U. Sharma
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. • 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. Answers: SQUARE: PROC NEAR PUSH AX Mov AL, BL ; place the number in AL IMUL BL ; multiply with itself Mov BX, AX POPAX RET ; return from procedure SQUARE: ENDP 4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. Answer: CUBE PROC NEAR PUSH AX MOV AL, BL IMUL BL IMUL BL MOV BX, AX POPAX RET CUBE ENDP 4/1/2014 Prof. K. U. Sharma, PRMCEAM, Contact: karthik8777@gmail.com, 9096996329