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

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

There are two types of addressing schemes:
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Converting a Negative Decimal Number to the 2’s Complement Representation : Represent (-18) 10 as a signed 8-bit binary number Solution: Abs(-18) 10 :0001.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
CS2422 Assembly Language & System Programming October 24, 2006.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
6.828: PC hardware and x86 Frans Kaashoek
Sahar Mosleh California State University San MarcosPage 1 JMP and Loops Memory Operand Move Instruction Array Data Related Operation and Directives.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
CSC 221 Computer Organization and Assembly Language
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Procedure Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/27 with slides by Kip Irvine.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Assembly language.
Microprocessor and Assembly Language
CS 301 Fall 2002 Control Structures
Stack Frames and Advanced Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
MIPS Procedure Calls CSE 378 – Section 3.
Practical Session 4.
Assembly Language for Intel-Based Computers, 4th Edition
Miscellaneous Topics.
Computer Organization and Assembly Language
Presentation transcript:

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

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

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.

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 , the offset of the most recently Pushed value ( ): 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.

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 A on the stack: Before the push, ESP= h, and after the push, ESP=000000FFCh. ESP

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 is popped from the stack: FFC 00000FF FF FF0

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.

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

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

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

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

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.

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.

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

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

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

Sahar Mosleh California State University San MarcosPage 17 When the call instruction executes, The address following the call is pushed on the stack and the address of MySub is located into as shown here: Main proc Call MySub 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 MySub proc Mov eax, edx : Ret MySubENDP

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.

Sahar Mosleh California State University San MarcosPage 19.data Array Dword10000h, 20000h, 30000h, 40000h, h 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

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