lecture 7 stack operations and introduction to procedure
DESCRIPTION
Lecture 7 Stack Operations and Introduction to Procedure. Modified and Presented By Dr. Rajesh Palit Asst. Professor, EECS, NSU Originally Prepared By Dr. Shazzad Hosain , EECS, NSU. Stack vs. Queue. Stack LIFO : Last In First Out Queue FIFO : First In First Out. Queue. Stack. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/1.jpg)
Lecture 7
Stack Operations and Introduction to Procedure
Modified and Presented ByDr. Rajesh Palit
Asst. Professor, EECS, NSUOriginally Prepared By
Dr. Shazzad Hosain, EECS, NSU
![Page 2: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/2.jpg)
Stack vs. Queue
• Stack– LIFO : Last In First Out
• Queue– FIFO : First In First Out
StackQueue
![Page 3: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/3.jpg)
PUSH vs. POP in Stack
![Page 4: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/4.jpg)
Stack Operations
![Page 5: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/5.jpg)
PUSH Instructions
![Page 6: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/6.jpg)
POP Instructions
![Page 7: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/7.jpg)
Stack example
![Page 8: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/8.jpg)
Introduction to Procedures• Way to implement top-down programming approach• Procedure declaration name PROC type
RETname ENDP
• Some other place the procedure is called bycall name
• Type can be FAR or NEAR, and it is optional, in absence of type NEAR is assumed
• NEAR indicates that the calling statement is in the same segment, FAR implies different segment
![Page 9: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/9.jpg)
Procedure contd.
• The RET instruction causes control to transfer back to the calling procedure.
• Every procedure, except the MAIN procedure should have a RET someplace, and usually it’s the last statement in the procedure
• A procedure must have a way to receive values from the caller, and a way to return results
• In assembly language, there is no parameter lists, so it’s up to the programmer to devise way to communicate
• In case of a few parameters, registers are fine to pass values
![Page 10: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/10.jpg)
Procedure Example
Product = 0REPEAT IF lsb of B is 1 THEN product = product + A END_IF Shift left A Shift right BUNTIL B = 0
![Page 11: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/11.jpg)
Passing Data Between Procedures
• Using Global Variables• Through Register (call by values)• Passing the address of the data• Using the stack– Suitable for recursive procedure– Used by high-level programming languages
• Procedure calling starts by pushing the return address on the stack
![Page 12: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/12.jpg)
Using BP for Accessing Stack
(original BP)return addressdata 1data 2
data n
SP, BPMOV AL, aPUSH AX…MOV AL, bPUSH AX
CALL addnos
addnos PROC NEARPUSH BPMOV BP, SPMOV AX, [BP+6]ADD AX, [BP+4]POP BPRET 4
addnos ENDP
74 75 BP
76 77 Return Address
78 79 b
80 81 a
SP, BP
![Page 13: Lecture 7 Stack Operations and Introduction to Procedure](https://reader035.vdocuments.us/reader035/viewer/2022062315/56815032550346895dbe2ff5/html5/thumbnails/13.jpg)
References
• Chapter 8 and Chapter 14 – Assembly Language Programming by Charles Marut