Stacks and Subroutines Stacks The stack is part
Stacks and Subroutines • Stacks : The stack is part of memory used for temporary storage of addresses and data. • It's LIFO structure, The stack sorts data in 2 bytes • The SS and SP registers point to the top of the stack like this: SS: [SP]. So the SS register is the segment and the SP register contains the offset. • The PUSH instruction is used to place values on the stack and the • POP instruction is used to remove values from the stack. 1
• When we dealing with stack in the program we write: . stack • To determine the size of the stack, which we want to use it, we put it as follows: . stack 100 h 2
PUSH Instruction The PUSH instruction first decrements SP and then copies a 16 -bits source operand into the stack. • A 16 -bit operand causes SP to be decremented by 2. • There are two instruction formats: PUSH r /m 16 PUSH imm 16 3
POP Instruction • The POP instruction first copies the contents of the stack element pointed to by SP into a 16 -bit destination operand then increments SP. • If the operand is 16 bits, ESP is incremented by 2. • The instruction format: POP r /m 16 4
PUSHF and POPF Instructions • The PUSHF instruction pushes the 16 -bit FLAGS register on the stack, and POPF pops the stack into FLAGS register : pushf popf 5
PUSHA, POPA • The PUSHA instruction pushes all of the 16 -bit general purpose registers on • the stack in the following order: AX, CX, DX, BX, SP , BP, SI, and DI. • The POPA instruction pops the same registers off the stack in reverse order. 6
Subroutines/procedures • a subroutine is a special part of the program that can be called for execution from any point in the program. The subroutine is written to provide a function that must be performed frequently at various points in the main program. Whenever the function must be performed, a single instruction is inserted into the main body of the program to CALL the subroutine. • RET instruction must be included at the end of the subroutine to return to the main program. 7
Procedures • Procedure is a part of code that can be called from your program in order to make some specific task. makes program more structural and easier to understand. returns to the same point from where it was called. Syntax: name PROC ; the code of the procedure. . . RET name ENDP 8
Procedures • CALL instruction is used to call a procedure • CALL is used to transfer controls • RET is used to end execution of procedure Syntax: [label: ] CALL procedure [label: ] RET [immediate] 9
Example m 1 m 2 CALL m 1 MOV AX, 2 RET ; return to operating system. PROC MOV BX, 5 CALL m 2 RET ; return to caller. ENDP PROC MOV CX, 10 RET; return to caller ENDP 10
CALL, RET • The CALL instruction: calls a procedure - pushes offset of next instruction on the stack - copies the address of the called procedure into IP • The RET instruction: returns from a procedure - pops top of stack into IP 11
Suggested documentation for each procedure: • Suggested documentation for each procedure: -A description of all tasks accomplished by the procedure. -Receives: A list of input parameters; state their usage and requirements. -Returns: A description of values returned by the procedure. -Requires: Optional list of requirements called preconditions that must be - satisfied before the procedure is called. 12
Passing Parameters To Procedures There are basically three ways that data can be passed to a procedure, they. re actually • Passing using processor registers, • Passing parameters using variable names, • Passing parameters using the stack. 13
Passing using processor registers sum proc ; sum function to sum two values ; the first value must be in bx ; the seconde value must be in dx ; the result will be in ax mov ax, bx add ax, dx ret 14
Using The Stack To Pass Parameters push b push a call sum 2 proc push bp mov bp, sp mov ax, [bp+4[ mov bx, [bp+6[ add ax, bx pop bp ret 4 sum 2 endp 15
16
Example 2: This program calls a subroutine that finds the largest number in a vector with N elements: 17
18
19
Procedure • NEAR: intra-segment procedure • FAR PROC: inter-segment procedure 20
Program execution and Stack • CALL and PUSH: store one word address or value onto stack • RET and POP: pop the stack and access the previously pushed word 21
Example 22
Example 23
- Slides: 23