Computer and Information Sciences College Computer Science Department

  • Slides: 22
Download presentation
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization

Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language

Lecture 8 The Stack and Procedures

Lecture 8 The Stack and Procedures

Lecture Outline • Introduction • The Stack • The PUSH Instruction • The POP

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 dynamic storage

Introduction • The stack segment of a program is used for temporary dynamic storage of data and addresses. • PUSH and POP instructions are used to add and remove words from the stack. • The stack is a one-dimensional data structure. • Items are added and removed from one end of the structure; that is, it processes in: “Last-In-First-Out” LIFO 2

The Stack • A program must set aside a block of memory to hold

The Stack • A program must set aside a block of memory to hold the stack. Ex: . STACK 100 H • When the program is assembled and loaded in memory: • SS will contain the segment number of the stack segment. • SP is initialized to 100 H, 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 PUSH

The PUSH Instruction • To add a new word to the stack we PUSH it on. • Syntax: PUSH source 16 -bit register or memory location • 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 AX 1234 5678 BX Offset 00 F 8 00 FA 00

The PUSH Instruction AX 1234 5678 BX Offset 00 F 8 00 FA 00 FC 5678 00 FE 1234 0100 Empty Stack SP SP 0100 After PUSH AX 00 FE SP 1234 0100 AFTER PUSH BX 5

The POP Instruction • To remove the top item from the stack, we POP

The POP Instruction • To remove the top item from the stack, we POP it. • Syntax: POP destination 16 -bit register (except IP) or memory location • 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 5678 CX 0001 DX 1234 DX Offset 00 F

The POP Instruction FFFF CX 5678 CX 0001 DX 1234 DX Offset 00 F 8 00 FA 00 FC 5678 00 FE 1234 SP 0100 Stack After POP CX 00 FE 0100 1234 SP After POP DX 7

The Stack Segment SS: 0 A 000 H 0 A 00 SP 0100 +

The Stack Segment SS: 0 A 000 H 0 A 00 SP 0100 + Offset Physical Address 0 A 100 H 0 A 00 Segment Register 00000 H 0 SS: SP Memory 0100 0 A 100 H 0 FFFFFH §The stack is always referenced with respect to the stack segment register. §The offset is given by the SP register. §The stack grows toward decreasing memory locations. §The SP points to the last or top item on the stack. Ø PUSH - pre-decrement the SP Ø POP - post-increment the SP

Exercise 1 • Write assembly code that uses the stack operations to swap the

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 • A subroutine or procedure (sometimes referred to as a subprogram)

Terminology of Procedures • A subroutine or procedure (sometimes referred to as a subprogram) is a segment of a larger program that is typically relatively independent of that program. • 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 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 The optional operand

Procedure Declaration • Syntax (except the main procedure): name PROC type The optional operand type is: ; body of the procedure • NEAR: the statement that RET calls the procedure is in the name ENDP same segment as the procedure itself, or Name is the The RET (return) • FAR: the statement that user-defined instruction causes calls the procedure is in a name of the control to transfer different segment. procedure. back to the calling If type is omitted, NEAR is procedure assumed. 15

Communication Between Procedures • Assembly language procedures do not have parameter lists. • It’s

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. • The CALL Instruction • To invoke a procedure, the CALL instruction is used. • 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. Quite the same thing when INT and IRET 10

The CALL Instruction Offset address Code segment Offset address MAIN PROC IP 0010 0012

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

The RET Instruction Offset address Code segment Offset address MAIN PROC IP 0010 0012

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

CALL example ORG 100 h MOV AL, 1 MOV BL, 2 CALL m 2

CALL example ORG 100 h MOV AL, 1 MOV BL, 2 CALL m 2 RET ; return to operating system. m 2 PROC MUL BL ; AX = AL * BL. RET ; return to caller. m 2 ENDP END This example update AL every time the procedure is called, BL register stays unchanged, so this algorithm calculates 2 in power of 4, so final result in AX register is 16 (or 10 h).

INDEC / OUTDEC Procedures • Procedures used to read and print decimal data •

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

INDEC / OUTDEC Procedures • INDEC ; Read character input from user and convert it to ; decimal stored in AX register ; Code of INDEC exist in file PGM 9_3. ASM • OUTDEC ; Display the decimal number in register AX to ; output screen ; Code of OUTDEC exist in file PGM 9_1. ASM • Include the two files using INCLUDE directive Syntax: INCLUDE C: ASM PGM 9_3. ASM INCLUDE C: ASM PGM 9_1. ASM 15

INDEC / OUTDEC Procedures OUTDEC PROC PUSH AX PUSH BX PUSH CX PUSH DX

INDEC / OUTDEC Procedures OUTDEC PROC PUSH AX PUSH BX PUSH CX PUSH DX OR AX, AX JGE @END_IF 1 PUSH AX MOV DL, '-' MOV AH, 2 INT 21 H POP AX NEG AX @END_IF 1: XOR CX, CX MOV BX, 10 D @REPEAT 1: XOR DX, DX DIV BX PUSH DX INC CX OR AX, AX JNE @REPEAT 1 MOV AH, 2 @PRINT_LOOP: POP DX OR DL, 30 H INT 21 H LOOP @PRINT_LOOP POP DX POP CX POP BX POP AX RET OUTDEC ENDP 16

INDEC / OUTDEC Procedures INDEC PROC ; READ DECIMAL NUMBER PUSH BX PUSH CX

INDEC / OUTDEC Procedures INDEC PROC ; READ DECIMAL NUMBER PUSH BX PUSH CX PUSH DX @BEGIN: MOV AH, 2 MOV DL, '? ' INT 21 H XOR BX, BX XOR CX, CX MOV AH, 1 INT 21 H CMP AL, '-' JE @MINUS CMP AL, '+' JE @PLUS JMP @REPEAT 2 @MINUS: MOV CX, 1 @PLUS: INT 21 H @REPEAT 2: CMP AL, '0' JNGE @NOT_DIGIT CMP AL, '9' JNLE @NOT_DIGIT AND AX, 000 FH PUSH AX MOV AX, 10 MUL BX POP BX ADD BX, AX MOV AH, 1 INT 21 H CMP AL, 0 DH JNE @REPEAT 2 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, 0 DH INT 21 H MOV DL, 0 AH INT 21 H JMP @BEGIN INDEC ENDP ; END READ

INDEC / OUTDEC Procedures MAIN PROGRAM MODEL SMALL. STACK 100 H. CODE MAIN PROC

INDEC / OUTDEC Procedures MAIN PROGRAM MODEL SMALL. STACK 100 H. CODE MAIN PROC --------------------CALL INDEC CALL OUTDEC --------------------MOV AH, 4 CH ; exit to DOS INT 21 H MAIN ENDP INCLUDE C: ASMPGM 9_1. ASM INCLUDE C: ASMPGM 9_3. ASM END MAIN 19