Lesson 4. Stack, Procedures and Macros

Similar documents
SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]

Experiment 8 8 Subroutine Handling Instructions and Macros

Assembling, Linking and Executing 1) Assembling: .obj obj .obj.lst .crf Assembler Types: a) One pass assembler:

EEM336 Microprocessors I. Data Movement Instructions

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

Computer Architecture and System Software Lecture 07: Assembly Language Programming

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Computer Structure and Organization

Computer Architecture and System Software Lecture 06: Assembly Language Programming

ORG ; TWO. Assembly Language Programming

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

Experiment 3 3 Basic Input Output

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

Segmentation in Assembly Language Programming

Copyright 2000 by Barry B. Brey The CPU Scheduling Processes

UNIVERSITY OF CALIFORNIA, RIVERSIDE

COE 205 Lab Manual Experiment N o 12. Experiment N o Using the Mouse

Summer 2003 Lecture 15 07/03/03

A4 Sample Solution Ch3

PHY4635/5635 Spring Lecture 8: Program Control Instructions

UNIT 4. Modular Programming

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Assembler Programming. Lecture 10

Programming in Module. Near Call

BASIC INTERRUPT PROCESSING

Intel 8086: Instruction Set

8086 INSTRUCTION SET

ADVANCE MICROPROCESSOR & INTERFACING

CS-202 Microprocessor and Assembly Language

ELEC VIDEO BIOS ROUTINES

Data Movement Instructions

Procedures and the Stack. Chapter 4 S. Dandamudi

EE 314 Spring 2003 Microprocessor Systems


VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Topics Introduction to Microprocessors. Chapter 5 Macros and modules. What is macro? How to use macro? (I) How to use macro?

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Summer 2003 Lecture 14 07/02/03

Chapter 3. Assembly Language Programming with 8086

3: 4: 5: 9: ',0 10: ',0 11: ',0 12: 13:.CODE 14: INCLUDE

Experiment N o 8. String Handling Instructions

Code segment Stack segment

1: /********************************************************** 2: * A

RMV ELECTRONICS INC. Application Note:

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

.code. lea dx,msg2. Page 1/8. Problem 1: Programming in Assembly [25 Points]

Come and join us at WebLyceum

EC 333 Microprocessor and Interfacing Techniques (3+1)

Dr. D.M. Akbar Hussain

Intel x86 Memory. Architecture. The x86 isn't all that complex it just doesn't make a lot of sense. Program Segments. x86 Data and Address Ranges

Proposed Common Configuration Method

Macros in Pentium. Assembly Language. October 25

Lesson 1. Fundamentals of assembly language

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Summer 2003 Lecture 4 06/14/03

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

CS401 Assembly Language Solved MCQS From Midterm Papers

Lecture (05) x86 programming 4

Lecture (08) x86 programming 7

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

PHI Learning Private Limited

8086 INTERNAL ARCHITECTURE

IFE: Course in Low Level Programing. Lecture 6

Program controlled semiconductor device (IC) which fetches (from memory), decodes and executes instructions.

Macros and Conditional Assembly. Chapter 10 S. Dandamudi

x86 Assembly Tutorial COS 318: Fall 2017

Chapter 3: Addressing Modes

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

EC 333 Microprocessor and Interfacing Techniques (3+1)

LABORATORY WORK NO. 8 WORKING WITH MACROS AND LIBRARIES

Computer Architecture 1 ح 303

Assembly Language Lab # 9

Chapter 4: Data Movement Instructions. 4 1 MOV Revisited

CG2007 Microprocessor systems.

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB3 FÖYÜ

CS401 - Computer Architecture and Assembly Language Programming Glossary By

Module 3 Instruction Set Architecture (ISA)

MPID MICROPROCESSOR AND ITS INTERFACING DEVICES EEE III II SEMESTER OBJECTIVE QUESTIONS

Chapter Three Addressing Mode MOV AX, BX

CALL instruction and RET instruction :

BLDEA S V.P. DR. P.G. HALAKATTI COLLEGE OF ENGINEERING & TECHNOLOGY, VIJAYAPURA

1. Introduction to Assembly Language

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Video processing The INT instruction enables program to interrupt its own processing. Use INT instruction to handle inputs and outputs

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

2003 LXI H, 42F2H ; this instruction store 42F2 in to the HL pair POP H ; store data from top of the stack to HL pair

Midterm Exam #2 Answer Key

Memory Organization. 27 December 2016 Pramod Ghimire. Slide 1 of 21

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II

Ethical Hacking. Assembly Language Tutorial

Program Control Instructions

PESIT Bangalore South Campus

Week /8086 Microprocessor Programming II

Transcription:

Lesson 4. Stack, Procedures and Macros Computer Structure and Organization Graduated in Computer Sciences / Graduated in Computer Engineering Graduated in Computer Sciences / Graduated in Computer Engineering Curso 2010-2011

Contents Bit instructions The stack How to define procedures Type of procedures: NEAR y FAR How to pass parameters to a procedure: Registers The Stack Structures and parameters passing Macros Procedures vs. Macros 2 / 25

Bit instructions (I) Mnemonic: TEST Format: TEST target, source Description: This funtion is a logical AND over source and target. The differences is that the result is not stored in anywhere. This instruction only modifies flags register bits. Example: ; AX = 1234, BX = 0000 TEST AX, BX ; AX =1234, BX = 0000, Result = 0000 JZ Es_Cero ; IF Zero jumps to Es_Cero label 3 / 25

Bit instructions (II) Mnemonic: CLI Format: CLI Description: Disable masked interrupts handling. Unmasked interrupt can not be disabled Example: CLI 4 / 25

Bit instructions (III) Mnemonic: STI Format: STI Description: Enable interrupts handling Example: STI 5 / 25

Bit instructions (IV) Mnemonic: CLC Format: CLC Description: Carry flag is set to zero Example: CLC 6 / 25

Bit instructions (V) Mnemonic: STC Format: STC Description: Carry flag is set to one Example: STC 7 / 25

Bit instructions (VI) Mnemonic: CLD Format: CLD Description: Direction flag is set to zero (used by string instructions) Example: CLD 8 / 25

Bit instructions (and VII) Mnemonic: STD Format: STD Description: Direction flag is set to one (used by string instructions) Example: STD 9 / 25

The Stack Portion of memory that handles return addresses of procedures and interrupt services LIFO structure (Last In First Out) SS and SP are dedicate registers to point to the top of the stack Each stack position is 16 bits length The stack increases to lower memory positions addresses with PUSH The stack decreases to upper memory positions addresses with POP STACK 10 / 25

Definición de procedimientos Procedures group instructions and allow to call them from different parts of the program without need to repeat them To call a procedure: CALL Last instruciton of a procedure: RET Return address of the procedure is stored on the stacl. Segment address the procedure belongs to is also stored on the stack if the type of the procedure is FAR To define a procedure folloing directives are used: PROC (begin of the procedure) y ENDP (end of the procedure) 11 / 25

Types of procedures Procedures have the following attributes: FAR (if it will be called from a different segment it belongs to) or NEAR (call is within the same segment) Main Procedure is always FAR (implícit) Ejemplo de definición de un procedimiento PrintString PROC FAR ; this procedure calls to 9h DOS MOV AH, 09h ; services to display a character string on the INT 21h ; string whose address is stored on DS:DX RET ; return of the procedure PrintString ENDP 12 / 25

How to pass parameters to a procedure by using registers General purpose registers are used to pass and to recieve paramenters and results from a procedure We so few registers the i8086 has, it s needed to save used registers by the procedure before calling it First set of instructions in a procedure must store on the stack all the registers that will be used in it. Before returning from procedure save registers must be restored. All of them except those in which results will be returned 13 / 25

How to pass parameters to a NEAR PROCEDURE. The stack (I) Actions performed by CALLER procedure Parameters offset or variables themselves are in the same segment so it will be stored on the stack Caller Procedure: MOV AX, VARIABLE1 PUSH AX LEA AX, VARIABLE2 PUSH AX... CALL Nombre_Proc Stack Structure IP (returning address) Offset VARIABLEn Offset Variable2 Offset Variable1 14 / 25

Lesson 4: Stack, Procedures and Macros How to pass parameters to a NEAR PROCEDURE. The stack (II) Actions performed by CALLED procedure 1. BP register is stored on the stack (PUSH BP) BP will be used to access parameters. 2. BP must point to SP (MOV BP, SP) 3. Parameters are recovered from the stack by using BP. The i parameter offset will be: [BP] + 4 + 2 (n - i) CALLER procedure: Estructura de la pila Name_Proc PROC PUSH BP BP register MOV BP,SP PUSH [used registers] Offset VARIABLEn IP (returning address) MOV AX,[BP+4] ;VARIABLEn... MOV BX,[BP+4+2 (n-i)] Offset Variable2 Offset Variable1 15 / 25

Lesson 4: Stack, Procedures and Macros How to pass parameters to a NEAR PROCEDURE. The stack (and III) Actions performed by CALLED procedure RET parameter indicates the stack positions to be removed when returning from CALLED procedure. This number will be 2xnumber of passed parameters. E.g. RET 8 if four parameters have been passed Stack structure BP register CALLED proceure: POP [used registers] POP BP RET 2 n Name_Proc ENDP IP (returning address) Offset VARIABLEn Offset Variable2 Offset Variable1 16 / 25

How to pass parameters to a FAR PROCEDURE. The stack (I) Actions to be performed by CALLER procedure Variables offset and segment must be stored on the stack because CALLER and CALLED procedures belongs to different memory segmentes CALLER procedure: LEA AX, VARIABLE1 PUSH DS PUSH AX LEA AX, VARIABLE2 PUSH DS PUSH AX... CALL PROCEDIMIENTO 17 / 25

How to pass parameters to a FAR PROCEDURE. The stack (and II) Actions performed by CALLED procedure BP register is stored on the stack (PUSH BP) BP will be used to access parameters. BP must point to SP (MOV BP, SP) Parameters are recovered from the stack by using BP. The i parameter offset will be: [BP] + 4 + 2 (n - i) and the segment whose belongs to: [BP] + 8 + 4 (n - i) RET parameter of the CALLED procedure: RET parameter indicates the stack positions to be removed when returning from CALLED procedure. This number will be 4xnumber of passed parameters. E.g. RET 16 if four parameters have been passed 18 / 25

Structs and parameters passing (I) Structures Structures will be defined with followin directives: STRUC (start of structure) and ENDS (ends of structure) Group different type and size data with a single name StructureName.Field will be uses to access to structure records RET argument and parameter passing can be easy by using structures DNI STRUC Name_and_Surname DB 50 DUP ( 0 ) Address DB 60 DUP ( 0 ) Age DB 0 Profession DB 15 DUP ( 0 ) DNI ENDS 19 / 25

Structs and parameters passing (II) Structure code and records for parameters passign to a NEAR procedure: Estructura STRUC bp0 DW? ; 2 bytes for storing BP return DW? ; It s equivalent to a +2 bytes offset from the bebining of the ; structure (first DW named bp0) p2 DW? ; It s equivalent to a +4 bytes offset from the beginning of the ; structure (DW + return) P1 DW? ; It s equivalent to a +6 bytes offset from the beginning of the ; structure (DW + return + p2) Estructura ENDS NRET EQU OFFSET p1 - OFFSET retorn 20 / 25

Structs and parameters passing (and III) Actions performed by NEAR CALLED procedure 1. BP register is stored on the stack (PUSH BP) BP will be used to access parameters. 2. BP must point to SP (MOV BP, SP) 3. Accessing to parameters with BP and the structure 4. RET argument will be NRET Example of previous defined structure Estructura struc bp0 DW? retorno DW? p2 DW? p1 DW? Estructura ends NRET EQU OFFSET p1 - OFFSET retorno CALLED procedure Prueba PROC NEAR PUSH BP MOV BP, SP PUSH AX MOV AX, [BP].p1 ADD [BP].p2, AX POP AX POP BP RET NRET Prueba ENDP 21 / 25

MACROS MACROS are names given to some pieces of code Code expansion instead of jumping is done Extra parameters will be ignored if passed. So have instructions that likely use them Mind the labels inside a macro Labels must be local inside the macro code 22 / 25

Las macros (II) Mnemonic: MACRO Format: macro_name MACRO parameter_list Description: name and parameters to be passed to the macro are defined. Parameter names are separated by commas. The end of a macro definition is specified using ENDM directive without writing macro name Nombre y Format: ENDM Description: to establish the end of a macro definition Example: Add_three MACRO operand1, operand2, result ENDM ;macro body 23 / 25

Las macros (III) Mnemonic: LOCAL Format: LOCAL label,[label,...] Description: indicate label names to be changed when expending a macro. This method solves duplicate label names. Must be used after macro definition line and only use it on macros Example: Delay MACRO number local go_on mov cx, number go_on: ENDM loop go_on 24 / 25

Procedures vs. Macros 25 / 25