Lecture 8
Download
1 / 21

Lecture 8 (The Stack and Procedures) - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

Lecture 8 (The Stack and Procedures). Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC / OUTDEC procedures. 1. Introduction. The stack segment of a program is used for temporary storage of data and addresses.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 8 (The Stack and Procedures)' - owen-holland


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 8 the stack and procedures

Lecture 8

(The Stack and Procedures)


Lecture 8 the stack and procedures

  • Lecture Outline

  • Introduction

  • The Stack

    • The PUSH Instruction

    • The POP Instruction

    • Terminology of Procedures

    • INDEC / OUTDEC procedures

1


Lecture 8 the stack and procedures

Introduction

  • The stack segment of a program is used for temporary storage of

  • data and addresses.

  • PUSH and POP instructions are used to add and remove words from

  • the stack.

2


Lecture 8 the stack and procedures

The Stack

  • A stack is a one-dimensional data structure.

  • Items are added and removed from one end of the structure; that is,

  • it processes in a “last-in-first-out” manner.

  • 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


Lecture 8 the stack and procedures

16-bit register or memory location

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.

4


Lecture 8 the stack and procedures

1234

5678

AX

BX

Offset

00F8

00FA

00FC

00FE

0100

Offset

00F8

00FA

00FC

00FE

0100

Offset

00F8

00FA

00FC

00FE

0100

5678

SP

1234

1234

SP

SP

Empty Stack

After PUSH AX

AFTER PUSH BX

The PUSH Instruction

5


Lecture 8 the stack and procedures

16-bit register (except IP) or memory location

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.

6


Lecture 8 the stack and procedures

FFFF

5678

5678

CX

CX

CX

1234

0001

0001

DX

DX

DX

Offset

00F8

00FA

00FC

00FE

0100

Offset

00F8

00FA

00FC

00FE

0100

Offset

00F8

00FA

00FC

00FE

0100

5678

5678

5678

SP

1234

1234

1234

SP

SP

Stack

After POP CX

After POP DX

The POP Instruction

7


Lecture 8 the stack and procedures

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


Lecture 8 the stack and procedures

Terminology of Procedures

  • An assembly program can be structured as a collection of

  • procedures.

  • 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 for 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


Lecture 8 the stack and procedures

  • 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.

Name is the

user-defined

name of the

procedure.

The RET (return)

instruction causes

control to transfer

back to the calling

procedure

Procedure Declaration

  • Syntax (except the main procedure):

  • name PROC type

  • ; body of the procedure

  • RET

  • name ENDP

15


Lecture 8 the stack and procedures

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.

10


Lecture 8 the stack and procedures

The CALL Instruction

  • To invoke a procedure, the CALL instruction is used.

11


Lecture 8 the stack and procedures

The CALL Instruction

  • 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

SP

0012

SP

Before CALL

After CALL

12


Lecture 8 the stack and procedures

The RET Instruction

  • 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


Lecture 8 the stack and procedures

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


Lecture 8 the stack and procedures

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


Lecture 8 the stack and procedures

INDEC / OUTDEC Procedures

  • OUTDEC PROC

  • PUSH AX

  • PUSH BX

  • PUSH CX

  • PUSH DX

  • OR AX,AX

  • JGE @END_IF1

  • PUSH AX

  • MOV DL,'-'

  • MOV AH,2

  • INT 21H

  • POP AX

  • NEG AX

  • @END_IF1:

  • XOR CX,CX

  • MOV BX,10D

  • @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 DX

  • POP CX

  • POP BX

  • POP AX

  • RET

  • OUTDEC ENDP

16


Lecture 8 the stack and procedures

INDEC / OUTDEC Procedures

  • 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

  • INT 21H

  • CMP AL,'-'

  • JE @MINUS

  • CMP AL,'+'

  • JE @PLUS

  • JMP @REPEAT2

  • @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

17


Lecture 8 the stack and procedures

INDEC / OUTDEC Procedures

Cont…

ADD BX,AX

MOV AH,1

INT 21H

CMP AL,0DH

JNE @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

INT 21H

JMP @BEGIN

INDEC ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;END READ;;;;;;;;;

18


Lecture 8 the stack and procedures

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