Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.

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.
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
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
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.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
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.
Quiz #2 Topics Character codes Intel IA-32 architecture Mostly MASM
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
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.
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.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
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
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
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.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
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.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
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.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Chapter 5: Procedures.
Assembly language.
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Introduction to Compilers Tim Teitelbaum
CS 301 Fall 2002 Control Structures
BIC 10503: COMPUTER ARCHITECTURE
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
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
Miscellaneous Topics.
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting procedures Documenting procedures The system stack The system stack Review for Midterm Exam Review for Midterm Exam

Passing values/addresses to/from procedures Methods: Methods: 1. Pass parameters in registers 2. Use shared memory (global variables) 3. Pass parameters on the system stack

1. Pass parameters in registers Set up registers before call and/or before return Set up registers before call and/or before return Generally … it’s a bad idea to pass parameters in registers Generally … it’s a bad idea to pass parameters in registers Procedure might change/restore register contents Procedure might change/restore register contents However However some Irvine library procedures require values in registers (e.g., “Receives” and “Preconditions” for ReadString) some Irvine library procedures require values in registers (e.g., “Receives” and “Preconditions” for ReadString) some Irvine library procedures return values in registers (e.g., “Returns” for ReadInt) some Irvine library procedures return values in registers (e.g., “Returns” for ReadInt)

2. Use shared memory (global variables) Set up memory contents before call and/or before return Set up memory contents before call and/or before return Generally … it’s a bad idea to use global variables Generally … it’s a bad idea to use global variables Procedure might change memory contents needed by other procedures (unwanted side-effects) Procedure might change memory contents needed by other procedures (unwanted side-effects) For now … we use globals. For now … we use globals. Later we will pass parameters on the system stack. Later we will pass parameters on the system stack.

In all cases, when a procedure is called: In all cases, when a procedure is called: be aware of preconditions be aware of preconditions What conditions must be true before the procedure can perform its task? What conditions must be true before the procedure can perform its task? be aware of what registers are changed (document!) be aware of what registers are changed (document!) save and restore registers if necessary save and restore registers if necessary

Saving registers If a procedure changes any registers, the calling procedure might lose important data If a procedure changes any registers, the calling procedure might lose important data Registers may be saved before call, and restored after return Registers may be saved before call, and restored after return Registers may be saved by the procedure, and restored before the return Registers may be saved by the procedure, and restored before the return

Saving/restoring registers Methods: Methods: 1. Move register contents to named memory locations, then restore after procedure returns. 2. Use pushad and popad a) Option 1: calling procedure pushes before call, pops after return b) Option 2: procedure pushes at beginning, and pops before the return 3. Use the USES directive 4. Save selected registers on the system stack a) (Much) more later about this

Method 1: Save register contents in memory Example (in main): Example (in main): movaReg, eax;save registers movbReg, ebx moveax, count;set parameters movebx, OFFSET val callsomeProc moveax, aReg;restore registers movebx, bReg …

Methods 2: Save all registers on the system stack* PUSHAD pushes the 32-bit general-purpose registers onto the stack PUSHAD pushes the 32-bit general-purpose registers onto the stack order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers off the stack in reverse order POPAD pops the same registers off the stack in reverse order Note: it’s best to use 32-bit (DWORD) operands Note: it’s best to use 32-bit (DWORD) operands * More about this soon … * More about this soon …

Method 2: Save all registers on the system stack Example (Option 1: in calling procedure): Example (Option 1: in calling procedure): pushad;save registers callsomeProc popad;restore registers…

Method 2: Save all registers on the system stack Example ( Option 2: in the called procedure): Example ( Option 2: in the called procedure): calcSum PROC pushad;save registers … ;procedure body … popad;restore registers ret calcSumENDP

Method 2: Save all registers on the system stack Warnings: Warnings: Be sure that values don't get lost. Be sure that values don't get lost. Be sure that the system stack is properly aligned Be sure that the system stack is properly aligned More later … More later …

Method 3: USES Directive Specifies which registers will be preserved Specifies which registers will be preserved Saves specified registers on the system stack before any procedure statements are executed. Saves specified registers on the system stack before any procedure statements are executed. Restores registers before the procedure returns. Restores registers before the procedure returns. MASM adds (invisible) save/restore code for specified registers MASM adds (invisible) save/restore code for specified registers at the beginning and end of the procedure at the beginning and end of the procedure

Method 3: USES Directive Example (in the procedure): Example (in the procedure): calcSum PROC USES esi ecx … ;procedure body …ret calcSumENDP Notes: Notes: no commas no commas Be careful ! USES affects the system stack Be careful ! USES affects the system stack

Documenting Procedures Documentation for each procedure: Documentation for each procedure: A description of all tasks accomplished by the procedure. A description of all tasks accomplished by the procedure. Receives: A list of input parameters; state usage and requirements. Receives: A list of input parameters; state usage and requirements. Returns: A description of values returned by the procedure. Returns: A description of values returned by the procedure. Preconditions: List of requirements that must be satisfied before the procedure is called. Preconditions: List of requirements that must be satisfied before the procedure is called. Registers changed: List of registers that may have different values than they had when the procedure was called Registers changed: List of registers that may have different values than they had when the procedure was called If a procedure is called without satisfying its preconditions, the procedure's creator makes no promise that it will work.

Procedure Documentation (example) ; calculate: procedure to calculate ; the summation of integers from ; a to b. ;receives: none (a, b, sum are global) ;returns: sum = a+(a+1)+... +b ;preconditions: a <= b ;registers changed: eax,ebx,ecx

Questions on procedures?

System Stack (Runtime Stack) The operating system maintains a stack The operating system maintains a stack Implemented in memory Implemented in memory LIFO structure LIFO structure Managed by the CPU, using two registers Managed by the CPU, using two registers SS: address of stack segment SS: address of stack segment ESP: stack pointer (always points to “top” of stack) ESP: stack pointer (always points to “top” of stack) i.e., ESP contains the address of the top of the stack i.e., ESP contains the address of the top of the stack

PUSH Operation A push operation A push operation decrements the stack pointer by 4 decrements the stack pointer by 4 copies a value into the location pointed to by the stack pointer. copies a value into the location pointed to by the stack pointer. Actual decrement depends on the size of the operand Actual decrement depends on the size of the operand Note: it’s best to use 32-bit (DWORD, 4-byte) operands Note: it’s best to use 32-bit (DWORD, 4-byte) operands

Example PUSH AddressContents …etc 01ECh? 01F0h? 01F4h? 01F8h? 01FCh? 0200h25 Stack Segment in Memory Suppose that ecx contains 317 and esp contains 0200h. In this case, [esp] is 25 * esp:0200h [esp]:25 The next instruction is pushecx Execute push ecx esp:01FCh [esp]: *Note: [esp] means “contents of memory at the address in esp” Note: esp is decremented, then 317 is stored in the stack

POP Operation A pop operation A pop operation Copies value at ESP into a register or variable. Copies value at ESP into a register or variable. increments the stack pointer by 4 increments the stack pointer by 4 Actual increment depends on the size of the operand Actual increment depends on the size of the operand Note: it’s best to use 32-bit (DWORD, 4-byte) operands Note: it’s best to use 32-bit (DWORD, 4-byte) operands

AddressContents …etc 01ECh? 01F0h? 01F4h? 01F8h? 01FCh h25 esp:01FCh [esp]:317 esp:0200h [esp]:25 Example POP Stack Segment in Memory Suppose that esp contains 01FCh. In this case, [esp] is 317. The next instruction is popeax Execute pop eax eax now contains 317 Note: 317 is copied to EAX, then then ESP is incremented. Memory contents unchanged.

PUSH and POP Instructions (32-bit) PUSH syntax: PUSH syntax: PUSH r/m32 PUSH r/m32 PUSH immed PUSH immed POP syntax: POP syntax: POP r/m32 POP r/m32

Using PUSH and POP push ecx; save registers push ebx mov ecx,100h mov ebx,0 ; etc. pop ebx; restore registers pop ecx Save and restore registers when they contain important values. POP operands occur in the opposite of the order of PUSH operands.

Example: Nested Loop mov ecx,100; set outer loop count L1:; begin the outer loop push ecx; save outer loop count mov ecx,20; set inner loop count L2:; begin the inner loop ; loop L2; repeat the inner loop pop ecx; restore outer loop count loop L1; repeat the outer loop Push the outer loop counter before entering the inner loop. Pop the outer loop counter when the inner loop terminates.

When not to push Be sure that PUSH does not hide a return address Be sure that PUSH does not hide a return address Be sure that POP does not lose a return address and/or replace needed values Be sure that POP does not lose a return address and/or replace needed values

Questions? Midterm Exam Review Midterm Exam Review

Midterm Exam Format: Format: 40% Calculations, definitions, short answer, multiple choice 40% Calculations, definitions, short answer, multiple choice 40% MASM code tracing 40% MASM code tracing 20% Writing MASM code 20% Writing MASM code Covers: Covers: Lectures # 1 – 10 Lectures # 1 – 10 MASM programming MASM programming Programs #1 & 2 Programs #1 & 2 Homework #1 Homework #1 Quizzes #1 & 2 Quizzes #1 & 2 Calculator and one 8x11 notecard permitted (both sides) Calculator and one 8x11 notecard permitted (both sides) No sharing No sharing

Midterm Exam Topics How computers work How computers work CISC: microprograms CISC: microprograms Memory, CPU, registers, ALU, buses, etc. Memory, CPU, registers, ALU, buses, etc. VonNeumann architecture, Instruction Execution Cycle, pipelining, etc. VonNeumann architecture, Instruction Execution Cycle, pipelining, etc. Internal representation Internal representation Numbers, characters, instructions, addresses, etc. Numbers, characters, instructions, addresses, etc. Floating-point, hamming codes, etc. Floating-point, hamming codes, etc. External representation External representation Binary, decimal, hexadecimal Binary, decimal, hexadecimal Peripheral devices Peripheral devices Magnetic disk drives Magnetic disk drives IA-32 architecture IA-32 architecture Register names, etc. Register names, etc. Byte-ordering (little-endian) Byte-ordering (little-endian)

Midterm Exam Topics Assembly Language Assembly Language Related to high-level languages Related to high-level languages Related to machine languages Related to machine languages Related to architecture Related to architecture Motivations for using assembly language Motivations for using assembly language Program development Program development Modularization Modularization Incremental development/testing Incremental development/testing Debugging Debugging

Midterm Exam Topics MASM MASM Program structure, commenting Program structure, commenting Assembly, linking, etc. Assembly, linking, etc. Segments, directives, etc. Segments, directives, etc. Labels, identifiers, constants Labels, identifiers, constants Data types, declarations Data types, declarations BYTE, WORD, DWORD, etc. BYTE, WORD, DWORD, etc. strings strings Addressing modes Addressing modes immediate, register, direct (i.e., memory), indirect (i.e., [register]) immediate, register, direct (i.e., memory), indirect (i.e., [register]) Instructions: Instructions: mov, push, pop mov, push, pop add, sub, mul, div, inc, dec add, sub, mul, div, inc, dec cmp, jmp, j, loop cmp, jmp, j, loop call, ret call, ret

Midterm Exam Topics MASM MASM Control structures Control structures Procedures, procedure calls Procedures, procedure calls return address, etc return address, etc Decisions Decisions Repetition Repetition Data validation Data validation System stack System stack Procedure calls and returns Procedure calls and returns Fundamentals of push and pop Fundamentals of push and pop