Writing and using procedures

Slides:



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

Register In computer architecture, a processor register is a small amount of storage available on the CPU whose contents can be accessed more quickly than.
There are two types of addressing schemes:
Assembly Programming Notes for Practical2 Munaf Sheikh
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
80x86 Processor Architecture
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.
Factorial of a number data segment x1 db 4 fact dw ? data ends
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
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.
Types of Registers (8086 Microprocessor Based)
Objective At the conclusion of this chapter you will be able to:
Strings, Procedures and Macros
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Click to add Title Comunicación y Gerencia Click To add Subtitle Click to add Text Fundamentals of Assembly Language.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
University of Tehran 1 Microprocessor System Design Omid Fatemi Machine Language Programming
Review of Assembly language. Recalling main concepts.
Multi-module programming. Requirements of an assembly language module when it is linked with another module PUBLIC directive - it exports to other modules.
ECE 353 Introduction to Microprocessor Systems Michael J. Schulte Week 6.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Internal Programming Architecture or Model
BITS Pilani Pilani Campus Pawan Sharma Lecture EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.
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
Additional Assembly Programming Concepts
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Microprocessor and Assembly Language
INSTRUCTION SET.
Machine control instruction
Assembly Language Programming Part 2
ADDRESSING MODES.
(The Stack and Procedures)
Chapter 3 Addressing Modes
Symbolic Instruction and Addressing
CS 301 Fall 2002 Control Structures
Introduction to Assembly Language
Chapter 4: Instructions
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
UNIT-I An Over View Of 8085 Architecture Of 8086 Microprocessor
(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.
X86 Assembly Review.
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
Chapter 8: Instruction Set 8086 CPU Architecture
UNIT-II ADDRESSING MODES & Instruction set
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

Writing and using procedures Sub program/procedure Procedure - single procedure - nested procedure - recursive procedure CALL and RET instructions

CALL- Call a procedure Used to transfer execution to a sub program or procedure. Types Near call Far call call to a procedure which is in the same code segment as the call instruction Decrement the sp by 2 and copies the offset of the next instruction after the CALL onto the stack.

FAR call Call to procedure which is in different segment from the one that contains the CALL instruction Decrement the sp by 2 and copies the contents of the CS register to the stack

Intersegment Direct -- new CS and IP are encoded in NEAR JUMPS and CALLS Intrasegment (CS does not change) Direct -- IP relative displacement new IP = old IP + displacement Allows program relocation with no change in code. Indirect -- new IP is in memory or a register. FAR Intersegment Direct -- new CS and IP are encoded in (CS changes) the instruction. Indirect -- new CS and IP are in memory. Hindu College, Amritsar.

Direct within-segment near call Syntax: CALL DST IP (IP)+16-bit displacement Example: CALL multo

Indirect within-segment near call Syntax: CALL DST IP (EA) EXAMPLE: CALL BX - BX contains the offset of the first instruction’ s procedure - IP is replaced with a 16-bit value from a specified register or memory location.

Direct inter-segment FAR call Syntax: CALL DST IP 16-bit displacement CS segment address A direct call to another segment Example: CALL smart-divide

Indirect inter-segment far call Syntax: CALL DST IP (EA) CS (EA+2) Example: CALL dword ptr[bx] - new values for CS and IP are fetched from 4 memory location in DS. - the new values for CS is fetched from [bx] and [bx+1] and the new IP is fetched from [bx+2] and [bx+3].

RET-return execution from procedure to calling program End of the procedure returns execution to the next instruction in the mainline

The 8086 stack Advantages Section of memory to Store return address save the contents of registers for the calling program while a procedure executes. To Hold data or addresses that will be acted upon by a procedure. Stack segment register:- hold the upper 16-bits of the starting address Stack pointer:- hold the offset of the last word written on the stack.

Label-directive Used to give a name to the current value in the location counter Example: STACK_SEG SEGMENT STACK DW 100 DUP(0) ; set aside 100 words for stack STACK_TOP LABEL WORD ; give name to next ; location after last STACK_SEG ENDS ; word in stack

Initialization of stack segment register and stack pointer register STACK_SEG SEGMENT STACK DW 40 DUP(0) ; set aside 40 words for stack STACK_TOP LABEL WORD ; give name to next location after last STACK_SEG ENDS ; word in stack CODE SEGMENT ASSUME: CS:CODE, SS: STACK_SEG MOV AX, STACK_SEG ;initialize ss register MOV SS, AX LEA SP, STACK_TOP ;initialize sp continue with program : CODE ENDS END

Instruction set

PUSHF: push flags Push flag register on the stack Syntax: PUSHF (Sp) (sp)-2 No flags are affected

POPF : pop flags Pop word from top of stack to flag register Syntax: POPF (SP) (SP)+2 Flags are affected

Passing parameters to and from procedure In registers In dedicated memory locations accessed by name With pointers passed in register With the stack Syntax for procedure

Consider the example to convert a packed BCD 0100 0101 1001 0110(459610)to binary. (0001 0001 1111 0100 or 11F4h)

Steps to convert packed BCD number(17) to its binary equivalent Separate nibbles Save lower nibble (don’t need to multiply by 1) Multiply upper nibble by 0Ah Add lower nibble to result of multiplication

1.Passing parameters in registers The values we need to pass to procedure are copied from memory to registers and this registers are used inside the procedure. Here,BCD to binary conversion is written as a procedure and hence the BCD value to be converted is moved to AL reg and AL is used inside the procedure.

data segment bcd_input db 17h bin_value db data segment bcd_input db 17h bin_value db ? data ends stack_s segment stack dw 100 dup(0) stack_top label word stack_s ends code segment assume cs:code ,ds:data,ss:stack_s start:mov ax,data mov ds,ax mov ax,stack_s mov ss,ax mov sp,offset stack_top mov al,bcd_input call bcd_bin mov bin_value,al bcd_bin proc near pushf ; push AX push BX push CX ; mov al,bcd_input mov bl,al and bl,0Fh and al,0F0h mov cl,04h ror al,cl

mov bh,0Ah mul bh add al,bl ; mov bin_value,al pop CX pop BX ; pop AX popf ret bcd_bin endp code ends end start

2.Passing Parameters In General Memory Instead of moving values to a reg ,the values are directly accessed inside procedure. Changes to be made are 1.The variable itself is used inside in procedure(mov al,bcd_input) 2.The result is written to a variable(mov bin_value,al)

3. Passing parameters using pointer Make use of pointers to pass data values inside procedure. Offset of data values are moved to SI and DI and these pointers are used inside the procedure. MOV SI, OFFSET BCD_INPUT MOV DI, OFFSET BIN_VALUE CALL BCD_BIN 1.The variable itself is used inside in procedure(mov al,[SI]) 2.The result is written to a variable(mov [DI],al)

4.Passing Parameters Using Stack push bp mov bp,sp mov ax,[bp+10] ……… ……. add al,bl mov [bp+10],ax pop bp pop cx pop bx pop ax ret mov al,bcd_input push ax ; moving AL value on ;to the stack. call bcd_bin pop ax mov bin_value,al bcd_bin proc near pushf ; ;push ax push bx push cx

Stack Overflow: stack fills up and overflows the memory space alloted for it. Advantages of procedure disadvantages of procedure

Writing and debugging programs containing procedures Module breakpoints Re-entrant procedures: Procedures that can be interrupted, used and re-entered without losing or writing over anything. To be re-entrant,a procedure must a)Push the flags and all registers used in the procedure b)Proc. Pgm should use only reg or stack to pass parameters

Recursive procedure Process of calling the same function again and again until some condition satisfied. Example: - factorial

Writing and Calling Far Procedure code segment assume cs:code,ds:data,ss:stack_seg …… call mul .. code ends procedures segment mul proc far assume cs:procedures mul endp procedures end

Factorial of a number using Far procedure stackseg segment stack dw 40 dup(0) tos label word stackseg ends dataseg segment public num db 5 res dw ? dataseg ends procedures segment public extrn fact:far procedures ends codeseg segment public assume cs:codeseg,ds:dataseg,ss:stackseg start:mov ax,dataseg mov ds,ax mov ax,stackseg mov ss,ax lea sp,tos mov al,1 mov ah,00 mov cl,num call fact mov res,ax mov ax,4c00h int 21h codeseg ends end start

; procedure which is called from other program public fact procedures segment public fact proc far assume cs:procedures cmp cl,00h jne l1 mov ah,00 ret l1: cmp cl,01h jne l2 mov ah,00 ret l2: mul cl dec cl jnz l2 fact endp procedures ends end

code segment assume cs:code, ds:data, ss:stack start: mov ax, data mov ds, ax mov ax, stack mov ss, ax mov sp, offset top mov ax, dividend mov dx, dividend+2 mov cx, divisor call divide jnc l1 jmp exit l1: mov quo, ax mov quo+2,dx mov rem, cx exit: mov ah, 4ch int 21h code ends end start ;eg-2 mainline program data segment public dividend dw 000ch, 000ch divisor dw 002h quo dw 2 dup(0) rem dw 0 data ends stack segment dw 40 dup(0) top label word stack ends public divisor code1 segment public extrn divide : far code1 ends

; procedure which is called from other program data segment public extrn divisor: word data ends public divide code1 segment public assume cs:code1 divide proc far start:cmp divisor,0 je exit mov bx, ax mov ax, dx mov dx,0000 div cx mov bp, ax mov ax, bx div cx mov cx, dx mov dx, bp clc jmp exit1 exit: stc exit1: ret ; mov ah, 4ch ;int 21h code1 ends end start

Writing and Using Assembler Macros Two ways to repeat a set of instructions a.Procedures b.Macros a.Procedures Adv:machine code for a group of instructions in the proc only have to put in memory once. Disadv. 1.Need of a stack 2.Overhead time required to call the proc. and return to the calling program.

When repeated group of instructions is too short or not appropriate to be written as a proc.,then we use macro. Macro is a group of instructions we bracket and give a name at the start of the program. Each time we call macro,the assembler will insert the set of instructions in place of call.(called as expanding macro) Assembler will generate the machine codes for the set of instructions each time macro is called.

Adv:avoids overhead time in calling and returning from procedure Adv:avoids overhead time in calling and returning from procedure. Disadv:each time in-line code is generated and hence it use more memory. Syntax: macroname MACRO ………….. ENDM passing parameters to macro Syntax: Macroname MACRO parameters ….

Assume cs:procedures, ds:patient_parameters Push_all ; macro call Example: Breath_rate proc FAR Assume cs:procedures, ds:patient_parameters Push_all ; macro call Mov ax,patient_parameters ;initialize data Move DS,AX ;segment register Push_all MACRO Pushf Push ax Push bx Push cx Push bp Push si Push di Push ds Push es Push ss ENDM