Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures

2 Sahar Mosleh California State University San MarcosPage 2 Stack Operations If you were to stack ten dinner plates on top of each other, you would be creating a stack. A stack is called LIFO structure, because the last value put into the stack is always the first value taken out. The principle of using the stack is: New values are added to the top of the stack Eexisting values are removed from the top. Stacks in general are useful structures for a variety of programming applications, and they can easily be implemented using object-oriented programming methods. In this course we concentrate on what is called the runtime stack

3 Sahar Mosleh California State University San MarcosPage 3 Runtime Stack The runtime stack is a memory array that is managed directly by the CPU, using two registers: SS and ESP. In protected mode, the SS register holds a segment descriptor and is not modified by user programs. The ESP register holds a 32-bit offset into some location on the stack. We rarely manipulate ESP directly instead, it is indirectly modified by instructions such as Call, RET, PUSH, and PUP.

4 Sahar Mosleh California State University San MarcosPage 4 The stack pointer register (ESP) points to the last integer to be added to, or pushed on, the stack. To demonstrate, let’s begin with a stack containing one value. In the fallowing illustration, the ESP ( extended stack pointer) contains hexadecimal 0001000, the offset of the most recently Pushed value (00000006): Each stack location in this figure contains 32 bits, which is the case when a program is running in protected mode. In real-address mode, each stack location is 16 bits, and the SP register points to the most recently pushed value.

5 Sahar Mosleh California State University San MarcosPage 5 Push operation A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location in the stack pointed to by the stack pointer. In the following figure, we push 0000005A on the stack: Before the push, ESP=00001000h, and after the push, ESP=000000FFCh. ESP

6 Sahar Mosleh California State University San MarcosPage 6 Pop Operation A pop operation removes a value from the stack and places it in a register or variable. After the value is popped from the stack, the stack pointer is incremented to point to the next highest location in the stack. The following diagram shows the stack before and after the value 00000002 is popped from the stack: 00001000 00000FFC 00000FF8 00000FF4 00000FF0

7 Sahar Mosleh California State University San MarcosPage 7 Stack applications There are several important uses of stack in program: A stack makes a convenient temporary save area for registers when they used for more than one purpose. After they are modified, they can be restored to their original values. When the CALL instruction executes, the CPU saves the current procedure's return address on the stack. When calling a procedure, we pass input values called arguments. These can be pushed on the stack.

8 Sahar Mosleh California State University San MarcosPage 8 PUSH and PUP Instructions PUSH Instruction: The PUSH instruction first decrements ESP and then copies either a 16 or 32 bit source operand into the stack. A 16 bit operation causes the ESP to be decremented by 2. A 32-bit operand causes ESP to be decremented by 4. There are Three instruction format PUSHr/m16 PUSHr/m32 PUSHimm32 If your program calls procedures from the Irvine 32 library, you should always push 32-bit values

9 Sahar Mosleh California State University San MarcosPage 9 Pop Instruction The pop instruction first copies the contents of the stack element pointed to by ESP into a 16- or 32-bit destination operand and then increments ESP. If the operand is 16 bits, ESP is incremented by 2. if the operand 32 bits, ESP is incremented by 4. POPr/m16 POPr/m32

10 Sahar Mosleh California State University San MarcosPage 10 PUSHAD,POPAD The PUSHAD instruction pushes all of the 32-bit general purpose registers on the stack in the following order: EAX, ECX, EBX, ESP (original value), EBP, ESI, and EDI. The POPAD instruction pops the same registers off the stack in reverse order. If you write a procedure that modifies a number of 32-bit registers, use PUSHAD at the beginning of procedure and POPAD at the end to save and restore the registers. Example: MySub proc pushad : mov eax,… movedx,.. movecx,… : popad ret MySub ENDP

11 Sahar Mosleh California State University San MarcosPage 11 Defining and Using Procedures Normally we define a procedure as a named block of stamens that ends in a return statement. A procedure is declared using the PROC and ENDP directives. It must be assigned a name (as a valid Identifier). Each program we have written far contains a procedure named main. Example: MainPROC : MainEND

12 Sahar Mosleh California State University San MarcosPage 12 When you create a procedures other than your program's main procedure, end it with a RET instruction. It forces the CPU to return to the location from the procedure was called: SumOf PROC addeax,ebx addeax,ecx Ret ; Return the Sum value in EAX SumOf ENDP The startup procedure ( main Procedure) is a special case because it ends with the exit statement. When you INCLUDE Irvine 32.inc statement, exit Is an alias for a call EXITProcess, a MS_Windows function call that terminates the program.

13 Sahar Mosleh California State University San MarcosPage 13 Documenting Procedures A good habit to cultivate is that of adding clear and readable documentation to your programs. The following are a few suggestions for information that you can put at the beginning of each procedure: A description of all tasks accomplished by the procedure. A list of input parameters and their usage, labeled by a word such as Receives. If any input parameters have specific requirements by the procedure, labeled by a word such a Returns. A list of special requirements, called preconditions, that must be satisfied before the procedure is called. These can be labeled by the word Requires.

14 Sahar Mosleh California State University San MarcosPage 14 Example: ;-------------------------------------------------------------------- SumOf PROC ; ;calculates and returns the sum of three 32-bit integers. ; recieives: EAX, EBX, ECX, the three integers. May be signed or ;unsigned. ; Returnes: EAX=Sum ;------------------------------------------------------------------- addeax,ebx addeax,ecx ret SumOf ENDP

15 Sahar Mosleh California State University San MarcosPage 15 Call and ret instructions The CALL instruction call a procedures by directing the processor to begin execution at a new memory location. The call instruction pushes its return address on the stack and copies the called procedure’s address into the instruction pointer (EIP) When the procedures is ready it uses RET instruction to bring the Processor back to the point in the program where the procedure was called. By returning the address form stack to instruction pointer register EIP The CPU always executes the instruction in memory pointed to by EIP

16 Sahar Mosleh California State University San MarcosPage 16 Example: Suppose that in main, a CALL statement is located at offset 00000020. Typically this instruction requires five bytes of machine code, so the next statement is located at offset 00000025 Mainproc 00000020Call MySub 00000025Movebx,eax Next suppose that the first executable instruction in MySub is located at offset 0000040: MySubproc Moveax, edx : Ret MySubENDP

17 Sahar Mosleh California State University San MarcosPage 17 When the call instruction executes, The address following the call 00000025 is pushed on the stack and the address of MySub is located into as shown here: Main proc 00000020 Call MySub 00000025 Movebx,eax All the instruction in MySub execute up to its RET instruction. When the RET instruction executes, the value in the stack pointed by ESP is popped into EIP. 00000040MySub proc Mov eax, edx : Ret MySubENDP

18 Sahar Mosleh California State University San MarcosPage 18 Passing Register Arguments to Procedures In assembly Passing integers to procedures (Argument or input passing) is been done inside general purpose registers. Example: The following program creates a procedure named ArraySum that receive two parameters from a calling program: a pointer to an array of 32-bit integer and a count of the number of array values. It calculates and returns the sum of the array in EAX.

19 Sahar Mosleh California State University San MarcosPage 19.data Array Dword10000h, 20000h, 30000h, 40000h, 500000h theSumdowrd? MainPROC movesi, offset array; ESI pointes to array movecx,lenghtof Array ; ECX = array count callArraySum ; call calculate the sum movtheSum,eax ; returned sum in EAX callwriteint ; print the sum exit mainENDP

20 Sahar Mosleh California State University San MarcosPage 20 ;-------------------------------------------------------------------- ArraySumPROC ; ;calculates and returns the array of 32-bit integers. ; recieives: ESI = Array ; ECX= number of elements in the array ; Returnes: EAX=Sum ;------------------------------------------------------------------- pushesi; Save ESI and ECX push ecx moveax,0; Set Sum to Zero L1: addeax,[esi]; add each integer to sum addesi,4; point to next integer loopL1; repeat for array size Popecx: restore ecx and ESI Popesi ret; Return sum in EAX ArraySum ENDP End mian


Download ppt "Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures."

Similar presentations


Ads by Google