EENG 4005 Microprocessors.

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.
There are two types of addressing schemes:
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.
Data Movement Instructions
8086 Assembly Language Programming I
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
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.
Assembly Language – Lab 5
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
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.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Objective At the conclusion of this chapter you will be able to:
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
4-Oct Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept  direct mode: OK for static addresses  indirect register mode:
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.
EENG4005 Outline Program organization Debugging hints MASM directives.
The Stack Stack, Procedures and Macros. Outline Stack organization PUSH and POP instructions Calling procedures Macros Programming guidelines.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
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.
Microprocessors used in Personal Computers. The Memory Map of a Personal Computers Transient Program Area (TPA): Holds the operating system (interrupt.
University of Tehran 1 Microprocessor System Design Omid Fatemi Machine Language Programming
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.
Internal Programming Architecture or Model
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.
ECE291 Computer Engineering II Lecture 12 Josh Potts University of Illinois at Urbana- Champaign.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Assembly language programming
Instruction set Architecture
Format of Assembly language
COURSE OUTCOMES OF Microprocessor and programming
Additional Assembly Programming Concepts
Instruksi Set Prosesor 8088
Microprocessor Systems Design I
Microprocessor and Assembly Language
Assembly Language Programming Part 2
(The Stack and Procedures)
Chapter 3 Addressing Modes
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.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
Symbolic Instruction and Addressing
Practical Session 4.
(The Stack and Procedures)
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
By Nasser Halasa Assembly Language.
Procedures & Macros Introduction Syntax Difference.
(The Stack and Procedures)
Procedures and Macros.
Presentation transcript:

EENG 4005 Microprocessors

Outline Program Stack PUSH & POP instructions Procedures Macros Macros vs. procedures EENG4005

Stack Key Characteristics Used to store temporary data during program execution One point of access - the top of the stack A stack is always operated as Last-In-First-Out (LIFO) storage, i.e., data are retrieved in the reverse order to which they were stored Instructions that directly manipulate the stack PUSH - place element on top of stack POP - remove element from top of stack EENG4005

Stack Implementation in Memory Original SP In Use Stack grows in direction of decreasing memory addresses Direction of increasing memory addresses SS:SP FREE FREE FREE FREE SS EENG4005

Stack Implementation in Memory (cont.) SS - Stack Segment SP (stack pointer) always points to the top of the stack SP initially points to top of the stack (high memory address). SP decreases as data is PUSHed PUSH AX ==> SUB SP, 2 ; MOV [SS:SP], AX SP increases as data is POPed POP AX ==> MOV AX, [SS:SP] ; ADD SP, 2 BP (base pointer) can point to any element on the stack EENG4005

PUSH Instruction Example To address 12FFF Register Array 03800 PUSH BX AX 6A 037FF BX 6AB3 6AB3 B3 037FE CX DX SP Before PUSH BX SP After PUSH BX SP 0800 SS 0300 03000 STACK segment EENG4005

POP Instruction Example To address 0FFFF Register Array 01008 AX POP BX 39 01007 BX 392F 392F 2F 01006 CX DX SP After POP BX SP Before POP BX SP 1006 SS 0000 00000 STACK segment EENG4005

PUSH & POP (More I) PUSH and POP always store or retrieve words of data (never bytes) in the 8086-80286 microprocessor The 80386/80486 allow words or double words to be transferred to and from the stack The source of data for PUSH any internal 16-bit/32-bit register, immediate data, any segment register, or any two bytes of memory data The POP places data into internal register, segment register (except CS), or a memory location EENG4005

PUSH & POP (More II) The 80286 and later microprocessors there is also PUSHA and POPA to store and retrieve, respectively the contents of internal register set (AX, CX, DX, BX, SP, BP, SI, and DI) Stack initialization, example: assume that the stack segment resides in memory locations 10000h-1FFFFh the stack segment (SS)is loaded with 1000h the SP is loaded with 0000h - to start the stack at the top of the 64K…WHY? EENG4005

The Stack Use To store To pass parameters to procedures registers return address information while procedures are executing local variables that procedures may require To pass parameters to procedures EENG4005

Temporary Register Storage Push and Pop registers to preserve their value Example: PUSH AX ; Place AX on the stack PUSH BX ; Place BX on the stack ... < modify contents of Registers AX & BX > POP BX ; Restore original value of BX POP AX ; Restore original value of AX EENG4005

Store Return Address of a Procedure PrintRec PROC NEAR ... <Print value of a record> RET PrintRec ENDP main PROC FAR <Calculate Scores> CALL PrintRec <Continue Execution HERE> CALL DOSXIT main ENDP At execution time 1. processor encounters the CALL to the procedure 2. pushes the return address (instruction pointer of the next instruction after the CALL) onto the stack 3. jumps to PrintRec 4. executes the code therein 5. pops the return address off the stack 6. returns to the calling code EENG4005

Passing Parameters on the Stack Stack can be used to pass parameter(s) to a procedure The caller pushes the procedure parameter onto the stack and the callee finds the parameter there When the procedure completes its task, the parameter should be popped from the stack MOV AX, OFFSET String PUSH AX CALL getStr ;the proc getStr expects the offset to the String to ;be on the stack EENG4005

Passing Parameters on the Stack Example ;Use of Procedures when parameters are passed using Stack ………. ;====== Stack ======================================================== stkseg segment stack ; *** STACK SEGMENT *** db 64 dup ('STACK ') ; 64*8 = 512 Bytes of Stack stkseg ends ;====== Begin Code/Data ============================================== cseg segment public 'CODE' ; *** CODE SEGMENT *** assume cs:cseg, ds:cseg, ss:stkseg, es:nothing LEN EQU 80 CR EQU 0dh LF EQU 0ah Prompt1 BYTE "Input a string", 0 Prompt2 BYTE "Do another? ", 0 String BYTE (LEN +1) DUP (?) EENG4005

Passing Parameters on the Stack Example (cont.) Main PROC FAR Begin: mov ax, OFFSET Prompt1 push ax call putStr mov ax, OFFSET String call getStr mov ax, OFFSET Prompt2 mov ax, OFFSET String push ax call getStr mov bx, OFFSET String cmp BYTE PTR [bx], 'y' je Begin mov ax, 4c00h int 21h MAIN ENDP CSEG ENDS END MAIN EENG4005

Passing Parameters on the Stack Example (cont.) ;OFFSET of string to be printed must ;be on the stack and the string must ;be null terminated putStr PROC NEAR push bp mov bp, sp push ax push bx push dx mov bx, [bp + 4] ;expect bx to point to string mov ah, 2h ; prepare to print a char with 21h nextChar: cmp BYTE PTR [bx], 0h ;check for null terminator je foundEnd ;when found exit mov dl, [bx] int 21h ; print with 21h inc bx ;point to next char jmp nextChar foundEnd: pop dx pop bx pop ax pop bp ret 2 putStr ENDP OFFSET String RETURN IP BP OLD BP AX BX Removes passed parameters from the stack SP DX EENG4005

Passing Parameters on the Stack Example (cont.) ;OFFSET of large enough buffer must ;have been pushed onto stack ;string will be null terminated getStr PROC NEAR push bp mov bp, sp push ax push bx mov bx, [bp + 4] ;base address of storing buffer mov ah, 01h getLoop: int 21h cmp al, CR ;look for CR in al je getEnd mov [bx], al ;bx points to storage location inc bx jmp getLoop getEnd: mov BYTE PTR [bx], 0 ;CR is converted in null term pop bx pop ax pop bp ret 2 getStr ENDP OFFSET String RETURN IP BP OLD BP AX SP BX EENG4005

Procedures (Overview) Group of instructions that usually perform one task Reusable section of the software that is stored in memory once, but use as often as necessary The stack stores the return address whenever a procedure is called during the execution of the program CALL pushes the address of the instruction following it on the stack RET removes an address from the stack so the program returns to the instruction following the call PROC NEAR My_Subroutine PUSH IP JUMP Offset My_Subroutine PROC FAR My_Subroutine PUSH CS PUSH IP JUMP Segment My_Subroutine:Offset My_Subroutine RET POP (CS:) IP Near calls and returns transfer control between procedures in the same code segment Far calls and returns pass control between different segments EENG4005

Procedures (Overview cont.) Procedures should save and restore registers that are modified in a subroutine. PrintRec PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI < Code modifies AX,BX,CX,DX,SI > POP SI POP DX POP CX POP BX POP AX RET PrintRec ENDP LIB291 Routine to save ALL registers RSAVE: Save ALL registers RREST: Restore ALL registers EENG4005

Procedures (Overview) Parameters to a procedure can be passed in on the stack global memory locations registers in the code stream in a parameter block reference by a pointer EENG4005

Passing Parameters in Registers Example: putsi (put short integer) routine outputs the value in AL as a signed integer putsi PROC PUSH AX ;saves AH’s values CBW ;sign extend AL --> AX PUTI ;do the work; puti expects the value of the ; signed integer in the AX register POP AX ;restore AH RET putsi ENDP EENG4005

Passing Parameters in the Code Stream Example: MyPrint BYTE “Code stream parameter.”, 0 Consider the following implementation of MyPrint MyPrint PROC NEAR PUSH BP MOVE BP, SP PUSH BX PUSH AX MOV BX, 2[BP] ;load return address into BX PrintLp: MOV AL, CS:[BX] ;get next character CMP AL, 0 ;check for end of the string JZ EndStr PUTC INC BX ;move to the next char JMP PrintLp EndStr: INC BX ;point at first byte beyond zero MOV 2[BP], BX ;save as a new return address POP AX POP BX POP BP RET MyPrint ENDP EENG4005

Passing Parameters via a Parameter Block Consider simple subroutine that adds J and K together, storing the result in I. ParmBlock WORD I I WORD ? ;I, J, K must appear in this order J WORD ? K WORD ? …… LES bx, ParmBlock CALL AddEm AddEm PROC NEAR PUSH AX MOV AX, ES:2[BX] ;get J’s value ADD AX, ES:4[BX] ;add in K’s value MOV ES:[BX], AX ;store result in I RET AddEM ENDP EENG4005

Macros A macro inserts a block of statements at various points in a program during assembly Text substitutions made at compile time NOT a procedure -- Code is literally dumped into program Parameter names are substituted Useful for tedious programming tasks Instantiated within code segment. EENG4005

Macros (cont.) General Format MACRO_NAME MACRO Param1,Param2,...,ParamN LOCAL MyLabel Your Code ... ... Param1 ... ...Param2 ... JMP MyLabel MyLabel: ... ParamN ... ENDM EENG4005

Local Variable(s) in a Macro A local variable is one that appears in the macro, but is not available outside the macro We use the LOCAL directive for defining a local variable If the label MyLabel in the previous example is not defined as local, the assembler will flag it with errors on the second and subsequent attempts to use the macro The LOCAL directive must always immediately follow the MACRO directive without any intervening comments or spaces Macros can be placed in a separate file use INCLUDE directive to include the file with external macro definitions into a program no EXTERN statement is needed to access the macro statements that have been included EENG4005

Macros (cont.) Example: DIV16 MACRO Result, X, Y ; Store into Result the signed result of X / Y ; Calculate Result = X / Y ; (all 16-bit signed integers) ; Destroys Registers AX,DX MOV AX, X ; Load AX with Dividend CWD ; Extend Sign into DX IDIV Y ; Signed Division MOV Result, AX ; Store Quotient ENDM EENG4005

Macros (cont.) Example: Using the macro in a program ; Variable Section varX1 DW 20 varX2 DW 4 varR DW ? ; Code Section DIV16 varR,varX1,varX2 Will Actually Generate the following code (You won't actually see this unless you debug the program). MOV AX, varX1 CWD IDIV varX2 MOV varR, AX EENG4005

Macros vs Procedures Proc_1 PROC NEAR MOV AX, 0 MOV BX, AX MOV CX, 5 RET Proc_1 ENDP Macro_1 MACRO ENDM CALL Proc_1 …... Macro_1 …… EENG4005

Macros vs Procedures (cont.) In the example the macro and procedure produce the same result The procedure definition generates code when the assembler encounters the PROC directive The macro does not emit any code when processing the statements between the MACRO and ENDM Upon encountering Macro_1 in the mnemonic field, MASM assembles every statement between the MACRO and ENDM directives and emits that code to the output file At run time, the processor executes these instructions without the call/ret overhead EENG4005

Macros vs Procedures (cont.) Advantage of using macros execution of MACRO expansion is usually faster (no call and ret) than the execution of the same code implemented with procedures Disadvantage assembler copies the macro code into the program at each macro invocation if the number of macro invocations within the program is large then the program will be much larger than when using procedures EENG4005