Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.

Slides:



Advertisements
Similar presentations
Programming 8086 – Part IV Stacks, Macros
Advertisements

Registers of the 8086/ /2002 JNM.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
There are two types of addressing schemes:
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
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Data Movement Instructions
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:
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Run time vs. Compile time
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Assembly Language – Lab 5
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Procedures and the Stack Chapter 10 S. Dandamudi.
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.
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.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
1 ICS 51 Introductory Computer Organization Fall 2009.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
The Stack Stack, Procedures and Macros. Outline Stack organization PUSH and POP instructions Calling procedures Macros Programming guidelines.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
University of Tehran 1 Microprocessor System Design Omid Fatemi Machine Language Programming
Microprocessor Fundamentals Week 2 Mount Druitt College of TAFE Dept. Electrical Engineering 2008.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Stack Operations Dr. Hadi AL Saadi.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
Microprocessor and Assembly Language
Introduction to Compilers Tim Teitelbaum
(The Stack and Procedures)
Chapter 3 Addressing Modes
In this lecture Global variables Local variables System stack
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
MIPS Instructions.
Practical Session 4.
(The Stack and Procedures)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Lecture 06 Programming language.
Program and memory layout
Process.
Where is all the knowledge we lost with information? T. S. Eliot
(The Stack and Procedures)
Program and memory layout
Computer Organization and Assembly Language
(The Stack and Procedures)
Presentation transcript:

Procedures and Stacks

Outline Stack organization PUSH and POP instructions Defining and Calling procedures

The stack Space used as temporary storage during the execution of a program Purpose: –saving the return address when calling procedures –saving the contents of registers used in procedures –passing parameters to procedures –Allocation of memory for local variables in procedures A single access point. LIFO data structure –Data is always accessed from the “top” of the stack –Insertion is done by “pushing” data onto the top of the stack –Deletion is done by “popping” data from the top of the stack Stack operations reduce the size of code by using the stack as an implied operand

Stack layout in memory In use Free SS SS:SP Original SP Direction of increasing memory addresses Stack grows in direction of decreasing memory addresses

Stack layout in memory SS – Stack segment register desigates a segment containing the stack SP – points always to the top of the stack –SP is decreased when data is pushed. E.g. if we push a word SP is decreased by 2 –SP is increased when data is popped. E.g. is we pop a word SP is increased by 2 BP points to elements in the stack –Remember that BP is the register that you use in your programs to access data from the stack –It will come into play when we communicate with “C”

Push example 6AB Segment index 6A B3 037FF FE To address 12FFF Stack segment SS SP DX CX BX AX Register array PUSH BX SP before push SP after push

Pop example 392F F To address 0FFFF Stack segment SS SP DX CX BX AX Register array POP BX SP after pop SP before pop

PUSH and POP Instructions which access the stack PUSH and POP always store/load words, not bytes In 386 and above you can also push/pop doublewords PUSH X –X can be immediate data, 16-bit register, segment register or 2 bytes of memory POP X –X can be 16-bit register, segment register except CS and memory location

PUSHA and POPA “Push All” and “Pop All” In 286 and later it is possible to push/pop the entire set of general purpose registers –AX,BX,CX,DX,SP,BP,SI,DI

Using the stack Storage –Return address when a procedure is called –Preserve the contents of registers –Local variables required by procedures –Dynamically allocated memory Communication –Parameters passed to procedures

Why preserve registers? In principle, Registers are global variables Registers can also be used as temporary storage in a procedure If a procedure needs to use registers as temporary storage and these registers contain “useful” global variables, their contents must be preserved The first instructions in a procedure should save them and the last instructions should restore them

Example: preserving registers Take careful note of the ordering PUSH AX; Place AX on the stack PUSH BX; Place BX on the stack PUSH CX; Place CX on the stack PUSH DX; Place DX on the stack PUSH SI; Place SI on the stack PUSH DI; Place DI on the stack ; code that modifies AX,BX,CX,SI,DI POP DI; Restore original value of DI POP SI; Restore original value of SI POP DX; Restore original value of DX POP CX; Restore original value of CX POP BX; Restore original value of BX POP AX; Restore original value of AX

Calling procedures and using the stack call proc_name –Pushes the instruction pointer (IP) onto the stack –Pushes CS onto the stack if the call is to a procedure outside the code segment –Unconditionally jumps to the label proc_name ret –Pop saved IP and, if necessary, the saved CS and restores their values in the registers –Causing instruction execution to resume at the instruction after the original call

Simple Procedure example mov ax, 10h mov bx, 20h mov cx, 30h mov dx, 40h call AddRegs;this proc adds AX + BX + CX + DX  AX call sysExit AddRegs: add ax, bx add ax, cx add ax, dx ret

Procedures at a glance Procedures can access global variables declared at the beginning of the program Procedures can access global variables stored in registers Procedures may have parameters passed to them –Registers holding values is a form of parameter passing –Pushing parameters onto the stack is another form of parameter passing (this is what “C” relies on) Procedures may need to preserve registers –Unless other conventions are in place, calling code usually expects that all registers are restored to their original values upon return from the call Procedures may return results to the caller in registers or write results in memory, or leave results on the stack

Stack Guidelines There must be a one-to-one match between push and pop instructions The first pop is matched to the last push –E.g. Last in, first out Messing up the stack makes for great confusion! Also, Do not push/pop registers intended to store return values. A register in which a return value is expected (usually AX) is expected to be changed, so do not save or restore it.

Organizing your program Create a block diagram or pseudocode of your program in paper –Control flow –Data flow Break the program into logical “components” that can be easily translated to procedures in your code Use descriptive names for variables –Noun_type for types –Nouns for variables –Verbs for procedures

Organizing your program Modular program organization helps debugging –Makes it easier to ‘isolate’ the bug to a single procedure All programs contain bugs! –This is overstated… –It really means that you shouldn’t expect your program to work the first time you run it… Maybe not the 10 th either! –…but you shouldn’t feel bad about it either, relax and trace the bug –Use the debugger! Its VERY HARD to debug assembly code without one.

Tracing bugs The debugging process: –Set breakpoints in your programs and use them as checkpoints for checking the contents of registers/memory –Comment out code, this might help you find out whether the commented out code contains the bug Comment as you go, otherwise you will quickly forget what you expected your code to accomplish Use your pseudo code as comments to the right of the assembly code implementation