מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
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.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
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
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Reading Condition Codes (Cont.)
Computer Architecture and Assembly Language
Instruction set Architecture
Credits and Disclaimers
C function call conventions and the stack
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Assembly IA-32.
In this lecture Global variables Local variables System stack
BIC 10503: COMPUTER ARCHITECTURE
8086 Registers Module M14.2 Sections 9.2, 10.1.
Stack Frame Linkage.
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Computer Architecture CST 250
X86 Assembly Review.
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

מבנה מחשב תרגול 4 מבנה התוכנית

2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In Assembly, we’ll do it ourselves. Our program is comprised of 3 parts:  Data Segment  Text (=Code) Segment  Stack Segment

3 The Data Segment.data - start of data part of the code. We can give each data item a label, for future reference, by writing: my_label: Later the assembler will exchange the labels with actual memory addresses. Every line must start with a ‘tab’.

4 The Data Segment We must specify the type of each data item, e.g. byte, word, double. For instance: counter:.word 15 In memory, it will look like: The compiler will translate ’15’ to binary. Remember “word” = 16 bit countercounter+1

5 The Data Segment Example:.data vec:.word12089, -89, 130 avi:.word72 So ‘vec’ is actually an array of words. Each item should be read as word, else it would have a different meaning.

6 The Data Segment Another Example:.data alice:.byte3, 5,10, -7 bob:.byte9, 20 In the memory it will be stored sequentially: alice alice+1 alice+2 alice+3 bob bob+1

7 The Data Segment Each variable type can store a signed or an unsigned value. Therefore, byte, for example can store a value in the range: -128 – 255. For simply allocating memory space we can use:.space10*4 for example (10*4 clearer syntax for 10 variables of size 4 - rather than 40).

8 The Data Segment.section.rodata means that from this point until.data or.text this section contains only read-only data. Const, Global, permanent string data.

9 The Text Segment.text – start of the text part of the code. for instance:.text addl$20, %eax …

10 The Stack Segment We’ll use a stack, rather different then the “common” one. We can read and write data anywhere on the stack. But, for each procedure / function, we’ll add space in the stack in a LIFO manner. The Stack Pointer (%esp) is a register pointing to the head of the stack.

11 The Stack Segment For example:.text addl$-64, %esp …(the procedure itself) addl$64, %esp

12 The Stack Segment Why did we add -64 (and not 64)? Data Segment Text Segment Reserved for OS Initial %esp PC New %esp Stack Segment 0x x x x7fffffff New %ebp

13 The Stack Segment The Frame Pointer (%ebp) is used to store the contents of the stack pointer at the beginning of the procedure. So if we’ll make an error managing the SP, we can recover its value, as it was when the procedure started.

14 Registers * Must be saved (pushed) in the stack before changing them, and restored when the program\function ends. ** Do not count on them !!!,Don’t put any valuable information in them if you are going to call a function from outside your program (like: printf,scanf etc..) Register NamesizeUse %al, %cl, %dl8bLoad to the lower 8 bits of these registers** %ah, %ch, %dh8bLoad to bits 8-15 in these registers** %bl, %bh8bLoad the lower 8 bits and bits 8-15 of this register* %ax, %cx, %dx16bLoad to the lower 16 bits of these registers** %bx16bLoad to the lower 16 bits of this register* %ecx, %edx32bCaller save registers = Temporary registers** %eax32bReturn value** %ebx, %esi, %edi32bCallee save registers = Save registers* %esp32bContains the stack pointer %ebp32bContains the frame pointer

15 Program Structure.data l:…#all global data. ….section.rodata k:…#all RO data such as string formats for printf..text#the beginning of the code.globlmain#defining the label “main” as the starting “main” as function. main: pushl%ebp#saving the old frame pointer. movl%esp,%ebp#creating the new frame pointer. …#saving callee-save registers if needed …#The program code …#restoring callee-save registers if needed movl%ebp,%esp#restoring the old stack pointer. popl%ebp#restoring the old frame pointer. ret#returning to the function that called us.

16 Stack Frame Structure The stack is used for: passing arguments storing return information saving registers local storage