Morgan Kaufmann Publishers Computer Organization and Assembly Language

Slides:



Advertisements
Similar presentations
Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Flow Control Instructions
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Assembly Language – Lab 5
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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.
Strings, Procedures and Macros
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
Lab 6 Stack.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
University of Tehran 1 Microprocessor System Design Omid Fatemi Machine Language Programming
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
I NTEL 8086 M icroprocessor بسم الله الرحمن الرحيم 1.
Stack Operations Dr. Hadi AL Saadi.
Assembly language programming
Computer Architecture CST 250
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
COURSE OUTCOMES OF MICROPROCESSOR AND PROGRAMMING
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Lecture 4 Control Flow Structures (LOOPS)
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly Language Programming Part 2
Microprocessor and Assembly Language
(The Stack and Procedures)
Chapter 3 Addressing Modes
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
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
Morgan Kaufmann Publishers Computer Organization and Assembly Language
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
(The Stack and Procedures)
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Flow Control Instructions
Lecture 06 Programming language.
University of Gujrat Department of Computer Science
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
Jump & Loop instructions
Chapter 7 –Program Logic and Control
CSC 497/583 Advanced Topics in Computer Security
By Nasser Halasa Assembly Language.
Chapter 7 –Program Logic and Control
Computer Organization and Assembly Language
Procedures & Macros Introduction Syntax Difference.
(The Stack and Procedures)
(Array and Addressing Modes)
Procedures and Macros.
Presentation transcript:

Morgan Kaufmann Publishers Computer Organization and Assembly Language January 18, 2019 CS 206 D Computer Organization and Assembly Language Chapter 1 — Computer Abstractions and Technology

The Stack and Procedures Lecture 8 The Stack and Procedures

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

“Last-In-First-Out” LIFO Introduction The stack segment of a program is used for temporary dynamic storage of data and addresses. PUSH and POP instructions are used to add and remove words from the stack. The stack is a one-dimensional data structure. Items are added and removed from one end of the structure; that is, it processes in: “Last-In-First-Out” LIFO 2

The Stack 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. 3

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 4

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

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 2. 16-bit register (except IP) or memory location 6

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

The Stack Segment 0A000H 00000H 0A00 SS: 0A100H SP 0100 SS:SP Memory 0A00 Segment Register Offset Physical Address + 0100 0A100H 0FFFFFH The stack is always referenced with respect to the stack segment register. The offset is given by the SP register. The stack grows toward decreasing memory locations. The SP points to the last or top item on the stack. PUSH - pre-decrement the SP POP - post-increment the SP

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 8

Terminology of Procedures A subroutine or procedure (sometimes referred to as a subprogram) is a segment of a larger program that is typically relatively independent of that program. 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 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. 9

Procedure Declaration Syntax (except the main procedure): name PROC type ; body of the procedure RET name ENDP Name is the user-defined name of the procedure. 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. The RET (return) instruction causes control to transfer back to the calling procedure 15

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. The CALL Instruction To invoke a procedure, the CALL instruction is used. Stack is used by CALL instruction to keep return address for procedure, RET instruction gets this value from the stack and returns to that offset. Quite the same thing when INT and IRET 10

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

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

CALL example ORG 100h MOV AL, 1 MOV BL, 2 CALL m2 RET ; return to operating system. m2 PROC MUL BL ; AX = AL * BL. RET ; return to caller. m2 ENDP END This example update AL every time the procedure is called, BL register stays unchanged, so this algorithm calculates 2 in power of 4, so final result in AX register is 16 (or 10h).

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 14

INCLUDE C:\ASM\ PGM9_3.ASM INCLUDE C:\ASM\ PGM9_1.ASM 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 15

INDEC / OUTDEC Procedures @REPEAT1: XOR DX,DX DIV BX PUSH DX INC CX OR AX,AX JNE @REPEAT1 MOV AH,2 @PRINT_LOOP: POP DX OR DL,30H INT 21H LOOP @PRINT_LOOP POP CX POP BX POP AX RET OUTDEC ENDP OUTDEC PROC PUSH AX PUSH BX PUSH CX PUSH DX OR AX,AX JGE @END_IF1 MOV DL,'-' MOV AH,2 INT 21H POP AX NEG AX @END_IF1: XOR CX,CX MOV BX,10D 16

INDEC / OUTDEC Procedures @MINUS: MOV CX,1 @PLUS: INT 21H @REPEAT2: CMP AL,'0' JNGE @NOT_DIGIT CMP AL,'9' JNLE @NOT_DIGIT AND AX,000FH PUSH AX MOV AX,10 MUL BX POP BX ADD BX,AX MOV AH,1 CMP AL,0DH JNE @REPEAT2 INDEC PROC ; READ DECIMAL NUMBER PUSH BX PUSH CX PUSH DX @BEGIN: MOV AH,2 MOV DL,'?' INT 21H XOR BX,BX XOR CX,CX MOV AH,1 CMP AL,'-' JE @MINUS CMP AL,'+' JE @PLUS JMP @REPEAT2 MOV AX,BX OR CX,CX JE @EXIT NEG AX @EXIT: POP DX POP CX POP BX RET @NOT_DIGIT: MOV AH,2 MOV DL,0DH INT 21H MOV DL,0AH JMP @BEGIN INDEC ENDP ;END READ

INDEC / OUTDEC Procedures MAIN PROGRAM MODEL SMALL .STACK 100H .CODE MAIN PROC ----------------------------------------- CALL INDEC CALL OUTDEC MOV AH, 4CH ; exit to DOS INT 21H MAIN ENDP INCLUDE C:ASM\PGM9_1.ASM INCLUDE C:ASM\PGM9_3.ASM END MAIN 19