assembly_lecture9

Download Report

Transcript assembly_lecture9

Lecture 9

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

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

2

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

The PUSH Instruction

• To add a new word to the stack we

PUS

H it on.

16-bit register or memory location • 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

The PUSH Instruction

1234 AX 5678 BX Offset 00F8 00FA 00FC 00FE 0100 Empty Stack SP Offset 00F8 00FA 00FC 00FE 0100 1234 After PUSH AX SP Offset 00F8 00FA 00FC 00FE 0100 5678 1234 SP AFTER PUSH BX 5

The POP Instruction

• To remove the top item from the stack, we

POP

it.

16-bit register (except IP) or memory location • 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

The POP Instruction

FFFF CX Offset 0001 DX 00F8 00FA 00FC 00FE 0100 5678 1234 SP 5678 CX Offset 0001 DX 00F8 00FA 00FC 00FE 0100 5678 1234 SP Stack After POP CX 5678 CX Offset 1234 DX 00F8 00FA 00FC 00FE 0100 5678 1234 SP After POP DX 7

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

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

Procedure Declaration

• Syntax (except the main procedure): name PROC type ; body of the procedure RET The optional operand type is: • NEAR: the statement that name ENDP calls the procedure is in the same segment as the Name is the user-defined name of the procedure.

The RET (return) instruction causes control to transfer back to the calling procedure procedure itself, or • FAR: the statement that calls the procedure is in a different segment.

If type is omitted, NEAR is assumed.

15

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

The CALL Instruction

• To invoke a procedure, the

CALL

instruction is used.

11

The CALL Instruction

Offset address Code segment MAIN PROC IP 0010 0012 CALL PROC1 next instruction 0200 PROC1 PROC first instruction RET Offset address Stack segment 00FE 0100 Before CALL Offset address 0010 0012 Code segment MAIN PROC CALL PROC1 next instruction SP IP 0200 PROC1 PROC first instruction RET Offset address Stack segment 00FE 0100 After CALL 0012 SP 12

The RET Instruction

Offset address Code segment MAIN PROC 0010 0012 CALL PROC1 next instruction IP 0200 0300 Offset address PROC1 PROC first instruction RET Stack segment 00FE 0100 Before RET 0012 Offset address IP 0010 0012 Code segment MAIN PROC CALL PROC1 next instruction SP 0200 0300 PROC1 PROC first instruction RET Offset address Stack segment 00FE 0100 After RET SP 13

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

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

INDEC / OUTDEC Procedures

OUTDEC PROC PUSH PUSH PUSH PUSH OR AX BX CX DX AX,AX JGE PUSH MOV MOV @END_IF1 AX DL,'-' AH,2 INT POP NEG @END_IF1: XOR MOV POP OR 21H AX AX CX,CX BX,10D @REPEAT1: XOR DIV PUSH INC OR JNE MOV DX,DX BX DX CX AX,AX @REPEAT1 AH,2 @PRINT_LOOP: DX DL,30H INT LOOP POP POP POP POP 21H @PRINT_LOOP DX CX BX AX RET OUTDEC ENDP

16

INDEC / OUTDEC Procedures

INDEC PROC ;;;;;;;;;;;;;;;;;;; READ DECIMAL NUMBER;;;;;;;;;;;; PUSH PUSH PUSH BX CX DX @BEGIN: MOV MOV INT XOR XOR AH,2 DL,'?' 21H BX,BX CX,CX MOV INT CMP JE CMP JE JMP @MINUS: MOV @PLUS: INT @REPEAT2: CMP JNGE CMP JNLE AND PUSH MOV MUL POP AH,1 21H AL,'-' @MINUS AL,'+' @PLUS @REPEAT2 CX,1 21H AL,'0' @NOT_DIGIT AL,'9' @NOT_DIGIT AX,000FH AX AX,10 BX BX

17

INDEC / OUTDEC Procedures Cont…

ADD MOV INT CMP JNE MOV OR JE NEG @EXIT: BX,AX AH,1 21H AL,0DH @REPEAT2 AX,BX CX,CX @EXIT AX POP POP POP RET DX CX BX @NOT_DIGIT: MOV AH,2 MOV INT MOV INT DL,0DH 21H DL,0AH 21H JMP INDEC @BEGIN ENDP ;;;;;;;;;;;;;;;;;;;;;;;;;END READ;;;;;;;;;

18

INDEC / OUTDEC Procedures MAIN PROGRAM

MODEL .STACK

.CODE

MAIN SMALL PROC 100H ---------------------------------------- CALL INDEC CALL OUTDEC ---------------------------------------- MOV AH, 4CH INT 21H MAIN ENDP ; exit to DOS INCLUDE C:ASM\PGM9_1.ASM

INCLUDE C:ASM\PGM9_3.ASM

END MAIN

19