Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.

Slides:



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

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
1 Assembly Instructions Assembly language instructions may involve mnemonics, labels, variables, constants, and directives. Examples are as follows. here.
EENG 4005 Microprocessors.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
8086 Assembly Language Programming I
LAB Flow Control Instructions
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Assembly Language – Lab 5
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Objective At the conclusion of this chapter you will be able to:
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
4-Oct Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept  direct mode: OK for static addresses  indirect register mode:
L AB 2. P ROGRAM STRUCTURE The assembly language program consist of code, data and stack. Data segment: contains all the variable definition..Data Code.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
Lab 6 Stack.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
In Class Program Write, assemble and test a program: –Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
1 CS 201 Computer Systems Programming Chapter 12 x86 Call & Return Herbert G. Mayer, PSU Status 6/28/2015.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
BITS Pilani Pilani Campus Pawan Sharma Lecture /12/ EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly language programming
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
Microprocessor and Assembly Language
Microprocessor and Assembly Language
(The Stack and Procedures)
Chapter 3 Addressing Modes
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Stack and Subroutines Module M17.1 Section 11.2.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(The Stack and Procedures)
Symbolic Instruction and Addressing
Chapter 6 - Procedures and Macros
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Lecture 06 Programming language.
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
Computer Organization and Assembly Language
Procedures & Macros Introduction Syntax Difference.
(The Stack and Procedures)
Procedures and Macros.
Presentation transcript:

Lecture 9 (The Stack and Procedures)

1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC / OUTDEC procedures

Introduction 2 The stack segment of a program is used for temporary storage of data and addresses. PUSH and POP instructions are used to add and remove words from the stack.

3 The Stack A stack is a one-dimensional data structure. Items are added and removed from one end of the structure; that is, it processes in a “last-in-first-out” manner. A program must set aside a block of memory to hold the stack. Ex:.STACK 100H When the program is assembled and loaded in memory: SS will contain the segment number of the stack segment. SP is initialized to 100H, which represents the empty stack position. When the stack is not empty, SP contains the offset address of the top of the stack.

4 The PUSH Instruction To add a new word to the stack we PUSH it on. Syntax: PUSH source Execution of PUSH causes the following to happen: SP is decreased/decremented by 2. A copy of the source content is moved to the address specified by SS:SP. The source is unchanged. 16-bit register or memory location

5 The PUSH Instruction Offset 00F8 00FA 00FC 00FE 0100 Empty Stack SP AXBX 1234 Offset 00F8 00FA 00FC 00FE 0100 After PUSH AX SP 1234 Offset 00F8 00FA 00FC 00FE 0100 AFTER PUSH BX SP5678

6 The POP Instruction To remove the top item from the stack, we POP it. Syntax: POP destination Execution of POP causes the following to happen: The content of SS:SP (the top of the stack) is moved to the destination. SP is increased by bit register (except IP) or memory location

7 The POP Instruction FFFF 0001 CX DX 1234 Offset 00F8 00FA 00FC 00FE 0100 Stack SP Offset 00F8 00FA 00FC 00FE 0100 After POP CX SP CX DX Offset 00F8 00FA 00FC 00FE 0100 After POP DX SP CX DX

8 Exercise 1 Write assembly code that uses the stack operations to swap the content of AX and DX. PUSH AX PUSH DX POP AX POP DX

9 Terminology of Procedures An assembly program can be structured as a collection of procedures. The MAIN procedure, contains the entry point to the program. To carry out a task, the main procedure calls one of the other procedures. It is also possible for these procedures to call each other, or for a procedure to call itself. When one procedure calls another, control transfers to the called procedure and its instructions are executed; the called procedure usually returns control to the caller at the next instruction after the call statement.

15 Procedure Declaration Syntax (except the main procedure): name PROC type ; body of the procedure RET name ENDP The optional operand type is: NEAR: the statement that calls the procedure is in the same segment as the procedure itself, or FAR: the statement that calls the procedure is in a different segment. If type is omitted, NEAR is assumed. Name is the user-defined name of the procedure. The RET (return) instruction causes control to transfer back to the calling procedure

10 Communication Between Procedures Assembly language procedures do not have parameter lists. It’s up to the programmer to devise a way for procedures to communicate. E.g. If there are only few input and output values, they can be placed in registers.

11 The CALL Instruction To invoke a procedure, the CALL instruction is used.

12 The CALL Instruction Offset address Code segment MAIN PROC CALL PROC1 next instruction PROC1 PROC first instruction RET Offset address 00FE 0100 Stack segment IP SP Before CALL Offset address Code segment MAIN PROC CALL PROC1 next instruction PROC1 PROC first instruction RET Offset address 00FE 0100 Stack segment IP SP After CALL 0012

13 The RET Instruction Offset address Code segment MAIN PROC CALL PROC1 next instruction PROC1 PROC first instruction RET Offset address 00FE 0100 Stack segment IP SP Before RET Offset address Code segment MAIN PROC CALL PROC1 next instruction PROC1 PROC first instruction RET Offset address 00FE 0100 Stack segment IP SP After RET 0012

14 INDEC / OUTDEC Procedures procedures used to read and print decimal data To invoke the two procedures, use CALL instruction inside the MAIN PROC. Example CALL INDEC. CALL OUTDEC

15 INDEC / OUTDEC Procedures INDEC Read character input from user and convert it to decimal stored in AX register Code of INDEC exist in file PGM9_3.ASM OUTDEC Display the decimal number in register AX to output screen Code of OUTDEC exist in file PGM9_1.ASM Include the two files using INCLUDE directive Syntax: INCLUDE C:\ASM\ PGM9_3.ASM INCLUDE C:\ASM\ PGM9_1.ASM

16 INDEC / OUTDEC Procedures OUTDECPROC PUSHAX PUSHBX PUSHCX PUSHDX ORAX,AX PUSHAX MOVDL,'-' MOVAH,2 INT21H POPAX XORCX,CX XORDX,DX DIVBX PUSHDX INCCX ORAX,AX POPDX ORDL,30H INT21H POPDX POPCX POPBX POPAX RET OUTDECENDP

17 INDEC / OUTDEC Procedures INDECPROC ;;;;;;;;;;;;;;;;;;; READ DECIMAL NUMBER;;;;;;;;;;;; PUSHBX PUSHCX MOVAH,2 MOVDL,'?' INT21H XORBX,BX XORCX,CX MOVAH,1 INT21H CMPAL,'-' CMPAL,'+' @MINUS: CMPAL,'0' CMPAL,'9' ANDAX,000FH PUSHAX MOVAX,10 MULBX POPBX

ADDBX,AX MOVAH,1 INT21H CMPAL,0DH MOVAX,BX ORCX,CX POPDX POPCX POPBX MOVAH,2 MOVDL,0DH INT21H MOVDL,0AH INT21H INDECENDP ;;;;;;;;;;;;;;;;;;;;;;;;;END READ;;;;;;;;; INDEC / OUTDEC Procedures Cont… 18

MODELSMALL.STACK100H.CODE MAINPROC CALL INDEC CALL OUTDEC MOVAH, 4CH; exit to DOS INT 21H MAINENDP INCLUDE C:ASM\PGM9_1.ASM INCLUDE C:ASM\PGM9_3.ASM ENDMAIN INDEC / OUTDEC Procedures MAIN PROGRAM 19