Programming 8086 – Part IV Stacks, Macros

Slides:



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

Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Azir ALIU 1 What is an assembly language?. Azir ALIU 2 Inside the CPU.
8086 Assembly Language Programming I
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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.
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.
Objective At the conclusion of this chapter you will be able to:
Strings, Procedures and Macros
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Writing and using procedures
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
INTRODUCTION TO MICROPROCESSOR Engr. Ammar Anwar Khan.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
The Stack Stack, Procedures and Macros. Outline Stack organization PUSH and POP instructions Calling procedures Macros Programming guidelines.
Microprocessors used in Personal Computers. The Memory Map of a Personal Computers Transient Program Area (TPA): Holds the operating system (interrupt.
University of Sargodha, Lahore Campus Prepared by Ali Saeed.
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.
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
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Stack Operations Dr. Hadi AL Saadi.
Assembly language programming
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
COURSE OUTCOMES OF MICROPROCESSOR AND PROGRAMMING
Additional Assembly Programming Concepts
Instruksi Set Prosesor 8088
ADDRESSING MODES.
Microprocessor and Assembly Language
Microprocessor and Assembly Language
EEM336 Microprocessors Laboratory Orientation
Assembly Language Programming Part 2
ADDRESSING MODES.
University of Gujrat Department of Computer Science
(The Stack and Procedures)
Chapter 3 Addressing Modes
Morgan Kaufmann Publishers Computer Organization and Assembly Language
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.
8086 Registers Module M14.2 Sections 9.2, 10.1.
CS-401 Computer Architecture & Assembly Language Programming
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
(The Stack and Procedures)
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Chapter 6 - Procedures and Macros
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
Lecture 06 Programming language.
Chapter 6 –Symbolic Instruction and Addressing
Process.
(The Stack and Procedures)
CSC 497/583 Advanced Topics in Computer Security
Chapter 8: Instruction Set 8086 CPU Architecture
Computer Organization and Assembly Language
Procedures & Macros Introduction Syntax Difference.
(The Stack and Procedures)
(Array and Addressing Modes)
Procedures and Macros.
Presentation transcript:

Programming 8086 – Part IV Stacks, Macros Microprocessors Programming 8086 – Part IV Stacks, Macros

Contents Stacks Macros

Stack Stack is an area of memory for keeping temporary data 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 The stack can also be used to keep any other data There are two instructions that work with the stack: PUSH - stores 16 bit value in the stack. POP - gets 16 bit value from the stack.

PUSH Syntax for PUSH instruction: REG: AX, BX, CX, DX, DI, SI, BP, SP PUSH REG PUSH SREG PUSH memory PUSH immediate (works only on 80186 and later) REG: AX, BX, CX, DX, DI, SI, BP, SP SREG: DS, ES, SS, CS memory: [BX], [BX+SI+7], 16 bit variable, etc. immediate: 5, -24, 3Fh, 10001101b, etc.

POP Syntax for POP instruction: REG: AX, BX, CX, DX, DI, SI, BP, SP POP REG POP SREG POP memory REG: AX, BX, CX, DX, DI, SI, BP, SP SREG: DS, ES, SS, (except CS) memory: [BX], [BX+SI+7], 16 bit variable, etc.

LIFO The stack uses LIFO (Last In First Out) algorithm, this means that if we push these values one by one into the stack: 1, 2, 3, 4, 5 the first value that we will get on pop will be 5, then 4, 3, 2, and only then 1.

Using the Stack It is very important to do equal number of PUSHs and POPs, otherwise the stack maybe corrupted and it will be impossible to return to operating system. PUSH and POP instruction are especially useful because we don't have too much registers to operate with, so here is a trick: Store original value of the register in stack (using PUSH). Use the register for any purpose. Restore the original value of the register from stack (using POP).

A Simple Stack Example ORG 100h MOV AX, 1234h PUSH AX ; store value of AX in stack. MOV AX, 5678h ; modify the AX value. POP AX ; restore the original value of AX. RET END

Exchanging Values Using Stack ORG 100h MOV AX, 1234h ; store 1234h in AX. MOV BX, 5678h ; store 5678h in BX PUSH AX ; store value of AX in stack. PUSH BX ; store value of BX in stack. POP AX ; set AX to original value of BX. POP BX ; set BX to original value of AX. RET END

SS and SP The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register. Generally operating system sets values of these registers on program start. The current address pointed by SS:SP is called the top of the stack. For COM files stack segment is generally the code segment, and stack pointer is set to value of 0FFFEh. At the address SS:0FFFEh stored a return address for RET instruction that is executed in the end of the program. You can visually see the stack operation by clicking on [Stack] button on emulator window. The top of the stack is marked with "<" sign.

PUSH and POP "PUSH source" instruction does the following: Subtract 2 from SP register. Write the value of source to the address SS:SP. "POP destination" instruction does the following: Write the value at the address SS:SP to destination. Add 2 to SP register.

Initial State of the Stack FFFF AX 1234H (TOS) SP FFFE BX 5678H FFFD FFFC FFFB FFFA … SS 0000

After PUSH AX FFFF AX 1234H FFFE BX 5678H 12 FFFD (TOS) SP 34 FFFC FFFB FFFA … SS 0000

After PUSH BX FFFF AX 1234H FFFE BX 5678H 12 FFFD 34 FFFC 56 FFFB (TOS) SP 78 FFFA … SS 0000

After POP AX FFFF AX 5678H FFFE BX 5678H 12 FFFD (TOS) SP 34 FFFC 56 FFFB 78 FFFA … SS 0000

After POP BX FFFF AX 5678H (TOS) SP FFFE BX 1234H 12 FFFD 34 FFFC 56 FFFB 78 FFFA … SS 0000

Macros Macros are just like procedures, but not really Macros look like procedures, but they exist only until your code is compiled After compilation all macros are replaced with real instructions If you declared a macro and never used it in your code, compiler will simply ignore it.

Macro Definition name MACRO [parameters,...] <instructions> ENDM

Unlike procedures, macros should be defined above the code that uses it. MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 MOV CX, p3 ENDM ORG 100h MyMacro 1, 2, 3 MyMacro 4, 5, DX RET The code on the left is expanded into: MOV AX, 00001h MOV BX, 00002h MOV CX, 00003h MOV AX, 00004h MOV BX, 00005h MOV CX, DX

Let us try this already given example to see the difference: ORG 100H MOV AX,1 CALL m1; try to comment this line MOV AX, 2 RET ; işletim sistemine dön. m1 PROC MOV BX, 5 RET ; çağrıyı yapan kısma dön. m1 ENDP END

Macros and Procedures When you want to use a procedure you should use CALL instruction, for example: CALL MyProc When you want to use a macro, you can just type its name. For example: MyMacro You should use stack or any general purpose registers to pass parameters to procedure. To pass parameters to macro, you can just type them after the macro name. For example: MyMacro 1, 2, 3

Macros and Procedures Macros end with ENDM directive and procedures end with ENDP directive. Procedures are located at some specific address in memory but macros are expanded directly in program's code.

Labels in Macros Macros are expanded directly in code, therefore if there are labels inside the macro definition you may get "Duplicate declaration" error when macro is used for twice or more. To avoid such problem, use LOCAL directive followed by names of variables, labels or procedure names.

Example MyMacro2 MACRO LOCAL label1, label2, stop CMP AX, 2 JE label1 CMP AX, 3 JE label2 label1: INC AX JMP STOP label2: ADD AX, 2 Stop: ENDM ORG 100h Mov Ax, 2 MyMacro2 RET