ICS51 Introductory Computer Organization

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
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.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
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.
Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Y86 Processor State Program Registers
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack Usage with MS Visual Studio Without Stack Protection.
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.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
1 Assembly Language: Function Calls Jennifer Rexford.
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.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
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.
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 5.
Recitation 3: Procedures and the Stack
Stack Operations Dr. Hadi AL Saadi.
Assembly function call convention
Computer Architecture and Assembly Language
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
CS/COE 0447 (term 2181) Jarrett Billingsley
Exploiting & Defense Day 2 Recap
Aaron Miller David Cohen Spring 2011
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
High-Level Language Interface
asum.ys A Y86 Programming Example
Computer Architecture and Assembly Language
Y86 Processor State Program Registers
Stack Frames and Advanced Procedures
Logical and Decision Operations
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Procedure Calls CSE 378 – Section 3.
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
X86 Assembly Review.
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

ICS51 Introductory Computer Organization Another Function Call Example and Logical Operations

The caller should • Before calling a procedure: • pass parameters to the procedure: push to the stack in reverse order (the last parameter is pushed first) • execute call (to perform the procedure call) • After the procedure returns: • pop parameters from the stack (to restore the stack to its state before call was performed) • the return value is in EAX

The callee should • At the beginning of procedure: • access passed parameters using ESP • When the procedure is done: • the return value should be placed in EAX • execute ret instruction (return to the caller)

Another example Passing parameters using the stack: Save registers used inside the function on the stack then restore them after Accessing parameters: To access the a location on the stack, use an instruction like mov REG, dword ptr[ESP+OFFSET] Passing parameters using the stack: Pushed on the stack from right to left The return address of the procedure is also on the stack!! The “ret” instruction Return value in the “eax” register

void main(void) { int arr[] = {0,1,2,3,4}; int sum = firstpass(arr,5); printf(" The sum is = %d\n",sum); } __declspec(naked) int firstpass (int *arr, int len){ __asm{ /* Save the registers that may be modified by the called function*/ push ecx push edx /* Get parameters from the stack */ mov ecx, dword ptr[esp + 12] // load the arr parameter from the stack mov edx, dword ptr[esp + 16] // load the len parameter from the stack /* put the parameters on the stack for the add2nums function */ push edx // edx contains length push ecx // ecx contains array call add2nums /* pop the parameters of the stack after the call – to restore the stack*/ pop ecx pop edx /* restore the saved registers from the stack*/ ret

__declspec(naked) int add2nums(int *array, int length) { __asm { /* Save the registers used in this function to the stack Do not save eax ! */ push edi xor eax,eax xor edi,edi /* this accesses the "array" parameter on the stack */ mov ebx, dword ptr[esp + 8] /* load the "length" parameter from the stack */ mov edx, dword ptr[esp + 12] loop1: cmp edx,edi je all_done /* copies i-th element or an array */ mov ecx, dword ptr[ebx+4*edi] /* the value returned by this function is put in eax */ add eax,ecx inc edi jmp loop1 all_done: /* restore the saved registers from the stack */ pop edi ret }

Logical Operations Get a bit Count number of ones Be creative! Using AND/OR Count number of ones Using DIV Using AND and SHR Be creative!