21
Lecture 8 (The Stack and Procedures)

Lecture 8 (The Stack and Procedures)

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Lecture 8

(The Stack and Procedures)

1

Lecture Outline

• Introduction• The Stack

• The PUSH Instruction• The POP Instruction•Terminology of Procedures•INDEC / OUTDEC procedures

Introduction

2

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

3

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.

4

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.

16-bit register or memory location

5

The PUSH Instruction

Offset

00F8

00FA

00FC

00FE

0100

Empty Stack

SP

1234 5678AX BX

1234

Offset

00F8

00FA

00FC

00FE

0100

After PUSH AX

SP 1234

Offset

00F8

00FA

00FC

00FE

0100

AFTER PUSH BX

SP5678

6

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.

16-bit register (except IP) or memory location

7

The POP Instruction

FFFF

0001

CX

DX

1234

Offset

00F8

00FA

00FC

00FE

0100

Stack

SP5678 5678

1234

Offset

00F8

00FA

00FC

00FE

0100

After POP CX

SP

5678

0001

CX

DX

5678

1234

Offset

00F8

00FA

00FC

00FE

0100

After POP DX

SP

5678

1234

CX

DX

8

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

9

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.

15

Procedure Declaration

• Syntax (except the main procedure): name PROC type ; body of the procedure

RET name ENDP

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-definedname of the procedure.

The RET (return) instruction causes control to transfer back to the callingprocedure

10

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.

11

The CALL Instruction

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

12

The CALL InstructionOffset address

0010 0012

0200

Code segment

MAIN PROC

CALL PROC1next instruction

PROC1 PROCfirst instruction

RET

Offset address

00FE0100

Stack segment

IP

SP

Before CALL

Offset address

0010 0012

0200

Code segment

MAIN PROC

CALL PROC1next instruction

PROC1 PROCfirst instruction

RET

Offset address

00FE0100

Stack segment

IP

SP

After CALL

0012

13

The RET InstructionOffset address

0010 0012

0200

0300

Code segment

MAIN PROC

CALL PROC1next instruction

PROC1 PROCfirst instruction

RET

Offset address

00FE0100

Stack segment

IP

SP

Before RET

Offset address

0010 0012

0200

0300

Code segment

MAIN PROC

CALL PROC1next instruction

PROC1 PROCfirst instruction

RET

Offset address

00FE0100

Stack segment

IP

SP

After RET

0012

14

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

15

INDEC / OUTDEC Procedures• INDEC Read character input from user and convert it to decimal stored in AX registerCode of INDEC exist in file PGM9_3.ASM•OUTDEC Display the decimal number in register AX to output screenCode of OUTDEC exist in file PGM9_1.ASM

•Include the two files using INCLUDE directive Syntax:INCLUDE C:\ASM\ PGM9_3.ASMINCLUDE C:\ASM\ PGM9_1.ASM

16

INDEC / OUTDEC ProceduresOUTDEC PROCPUSH AXPUSH BXPUSH CXPUSH DXOR AX,AXJGE @END_IF1PUSH AXMOV DL,'-'MOV AH,2INT 21HPOP AXNEG AX@END_IF1:XOR CX,CXMOV BX,10D@REPEAT1:XOR DX,DXDIV BXPUSH DXINC CXOR AX,AXJNE @REPEAT1MOV AH,2@PRINT_LOOP:POP DXOR DL,30HINT 21HLOOP @PRINT_LOOPPOP DXPOP CXPOP BXPOP AX

RETOUTDEC ENDP

17

INDEC / OUTDEC ProceduresINDEC PROC;;;;;;;;;;;;;;;;;;; READ DECIMAL NUMBER;;;;;;;;;;;;PUSH BXPUSH CXPUSH DX

@BEGIN:MOV AH,2MOV DL,'?'INT 21HXOR BX,BXXOR CX,CX

MOV AH,1INT 21HCMP AL,'-'JE @MINUSCMP AL,'+'JE @PLUSJMP @REPEAT2@MINUS:MOV CX,1@PLUS:INT 21H@REPEAT2:CMP AL,'0'JNGE @NOT_DIGITCMP AL,'9'JNLE @NOT_DIGITAND AX,000FHPUSH AXMOV AX,10MUL BXPOP BX

ADD BX,AXMOV AH,1INT 21HCMP AL,0DHJNE @REPEAT2MOV AX,BXOR CX,CXJE @EXITNEG AX@EXIT:POP DXPOP CXPOP BXRET@NOT_DIGIT:MOV AH,2MOV DL,0DHINT 21HMOV DL,0AHINT 21HJMP @BEGININDEC ENDP;;;;;;;;;;;;;;;;;;;;;;;;;END READ;;;;;;;;;

INDEC / OUTDEC ProceduresCont…

18

MODEL SMALL.STACK 100H.CODEMAIN PROC

-----------------------------------------

CALL INDEC

CALL OUTDEC

-----------------------------------------MOV AH, 4CH ; exit to DOSINT 21H

MAIN ENDP

INCLUDE C:ASM\PGM9_1.ASM

INCLUDE C:ASM\PGM9_3.ASM

END MAIN

INDEC / OUTDEC ProceduresMAIN PROGRAM

19