1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

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

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
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.
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.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 10 S. Dandamudi.
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
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
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.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
CSC 221 Computer Organization and Assembly Language
Functions/Methods in Assembly
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
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 18 Linking to External Library The Book’s Link Library Stack Operations.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
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.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
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
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
CS 301 Fall 2002 Control Structures
Stack Frames and Advanced Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Practical Session 4.
Assembly Language for Intel-Based Computers, 4th Edition
Week 2: Buffer Overflow Part 1.
Miscellaneous Topics.
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine

Outline  Stack OperationsStack Operations  Defining and Using ProceduresDefining and Using Procedures

Stack Operations n Why Stacks?  Recall data structures class.  Recall how procedure (functions) used.  Procedure calling is a stack operation.  We use stack to keep track of return addresses.  Parameters and local variables are also put on the stack when calling some subroutines.

Stack Operations n Concepts  A stack is a LIFO (last-in, first-out) structure  The runtime stack is a memory that is managed directly by the CPU, using two registers: SS and ESP  SS holds a segment descriptor and is not modified by user program  ESP holds a 32-bit offset into some location on the stack  An Intel stack grows downward from high memory to low memory

 Format: PUSH r/m16/m32/imm16/imm32 Stack Operations n PUSH Operation  Immediate values are 32-bit in protected mode and 16-bit in real mode  A 16-bit/32-bit operand causes ESP to be decremented by 2 and 4, respectively. Push AX Push BX Offset Stack AB AX BX AB ESP XX YY ESP ESP AB 01

 Format: POP r/m16/m32 Stack Operations n POP Operation POP BX POP AX Offset Stack AB AX BX ? 0000? XX YY ESP AB 01 ESP 01AB 0024 ESP  The area of the stack above ESP is logically empty, and will be overwritten

Stack Operations n Additional Stack Operations  PUSHF and POPF Push and pops the Flag register. There are no operands  PUSHAD and POPAD Pushes registers on the stack in this order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI and pops them in reverse order  PUSHA and POPA The same except they work with 16-bit registers

Outline  Stack OperationsStack Operations  Defining and Using ProceduresDefining and Using Procedures

Procedures n Concepts  Procedure: blocks of code that are called and must be returned from  A procedure begins with itsname proc and terminate with itsname endp  To end a procedure other that the program startup procedure (main), use ret instruction  Use call itsname to call the procedure.  It is a highly desirable to preserve registers when writing a procedure. Save at beginning and restore before returning

Procedures n CALL and RET Instructions  CALL: directs the processor to begin execution at a new memory location  Pushes the return address on the stack  Copies the called procedure address into the EIP.  RET: brings the processor back to the point in the program where the procedure was called  Popes the return address from the stack into the EIP.

??? Procedures n Examples main PROC Call MyProc MOV eax, ebx … MyProc PROC MOV eax, edx … ret MyProc Endp ESP EIP ??? ESP EIP

Procedures n Nested Procedures ESP (return to sub2) (return to sub1) (return to main) Low high

Procedures n Local and Global Labels  Global labels are followed by two colons, making them visible to the whole program  By default, a code label (followed by a single colon) has local scope, making it visible only to statements inside its enclosing procedure main PROC Sub PROC JMP L2 L2: L1:: JMP L1 main endp ret … ?

Procedures n Passing Parameter  In registers - Fastest  In global variables - Hard to reuse, poor programming practice  On the stack - Used by high level languages

Procedures n Examples  Calculating the sum of an integer array  It is better to pass the offset of an array to the procedure than to include references to specific variable names inside the procedure

Save and restore registers that are modified by a procedure The return register should not be pushed and popped

Procedures n Design using Procedures  Divide the specification into tasks  Understand the specification of the program  Design each task as a sub-procedure  Design the main procedure (the program start up procedure) to call all sub- procedures

Linking to External Library n Link Library  A file containing procedures that have been assembled into machine code  Linker command  In your program, these procedure could be included and called  The assembler would leave the target address of the Call instruction blank, which will be filled by the linker  link32 %1.obj libraryName.lib

Outline  Stack OperationsStack Operations  Defining and Using ProceduresDefining and Using Procedures