Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Procedures and the Stack Chapter 5 S. Dandamudi Outline • What is stack? • Pentium implementation of stack • Stack instructions • Uses of stack • Procedures Pentium instructions • Parameter passing Register method Stack method 2005 • Examples Call-by-value Call-by-reference Bubble sort • Procedures with variable number of parameters • Local variables • Multiple source program modules • Performance: Procedure overheads S. Dandamudi Chapter 5: Page 2 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. What is a Stack? • Stack is a last-in-first-out (LIFO) data structure • If we view the stack as a linear array of elements, both insertion and deletion operations are restricted to one end of the array • Only the element at the top-of-stack (TOS) is directly accessible • Two basic stack operations: push (insertion) pop (deletion) 2005 S. Dandamudi Chapter 5: Page 3 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. What is a Stack? (cont’d) • Example Insertion of data items into the stack » Arrow points to the top-of-stack 2005 S. Dandamudi Chapter 5: Page 4 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. What is a Stack? (cont’d) • Example Deletion of data items from the stack » Arrow points to the top-of-stack 2005 S. Dandamudi Chapter 5: Page 5 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Implementation of the Stack • Stack segment is used to implement the stack Registers SS and ESP (32-bit) or SP (16-bit) are used SS:ESP or SS:SP represents the top-of-stack • Pentium stack implementation characteristics are: Only words (i.e., 16-bit data) or doublewords (i.e., 32bit data) are saved on the stack, never a single byte Stack grows toward lower memory addresses (i.e., stack grows “downward”) Top-of-stack (TOS) always points to the last data item placed on the stack 2005 S. Dandamudi Chapter 5: Page 6 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Stack Instructions • Pentium provides two basic instructions: push pop source destination • source and destination can be a 16- or 32-bit general register a segment register a word or doubleword in memory • source of push can also be an immediate operand of size 8, 16, or 32 bits 2005 S. Dandamudi Chapter 5: Page 7 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Stack Example - 1 2005 S. Dandamudi Chapter 5: Page 8 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Stack Instructions: Examples • On an empty stack, the following sequence of push instructions push push 21ABH 7FBD329AH results in the stack state shown in (c) in the last figure • On this stack, executing pop EBX results in the stack state shown in (b) in the next figure and the register EBX gets the value 7FBD329AH 2005 S. Dandamudi Chapter 5: Page 9 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Stack Example - 2 2005 S. Dandamudi Chapter 5: Page 10 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. 2005 S. Dandamudi Chapter 5: Page 11 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Additional Pentium Stack Instructions Stack Operations on Flags • push and pop instructions cannot be used with the Flags register • Two special instructions for this purpose are pushfd (push 32-bit flags) popfd (pop 32-bit flags) • No operands are required • Use pushfw and popfw for 16-bit flags (FLAGS) 2005 S. Dandamudi Chapter 5: Page 12 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Additional Pentium Stack Instructions (cont’d) Stack Operations on All General-Purpose Registers • pushad and popad instructions can be used to save and restore the eight general-purpose registers EAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI • Pushad pushes these eight registers in the above order (EAX first and EDI last) • popad restores these registers except that ESP value is not loaded into the ESP register • Use pushaw and popaw for saving and restoring 16-bit registers 2005 S. Dandamudi Chapter 5: Page 13 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Uses of the Stack • Three main uses » Temporary storage of data » Transfer of control » Parameter passing Temporary Storage of Data Example: Exchanging value1 and value2 can be done by using the stack to temporarily hold data push push pop pop 2005 value1 value2 value1 value2 S. Dandamudi Chapter 5: Page 14 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Uses of the Stack (cont’d) • Often used to free a set of registers ;save AX & BX registers on the stack push AX push BX ; AX and BX registers can now be used mov AX,value1 mov BX,value2 mov value1,BX mov value2,AX ;restore AX & BX registers from the stack pop BX pop AX . . . 2005 S. Dandamudi Chapter 5: Page 15 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Assembly Example1 ; Assembly language that reads two characters ; and swap them using stack .586 option segment:use16 .model small .stack 200h print_msg MACRO msg mov dx,offset msg mov ah,9 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 16 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example1 read_char MACRO mov ah,1 int 21h sub ah,ah ENDM print_char MACRO char mov dl,char mov ah,2 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 17 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example1 .data msg1 db 'Enter 1st character:$' msg2 db 0ah,0dh,'Enter 2nd character:$' resmsg1 db 0ah,0dh,'1st character after swapping:$' resmsg2 db 0ah,0dh,'2nd character after swapping:$' .code .startup print_msg msg1 read_char push ax print_msg msg2 2005 S. Dandamudi Chapter 5: Page 18 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example1 read_char push ax print_msg resmsg1 pop ax print_char al print_msg resmsg2 pop ax print_char al .exit end 2005 S. Dandamudi Chapter 5: Page 19 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example2 ; Assembly language that reads n characters ; and print them in reverse order using stack .586 option segment:use16 .model small .stack 200h print_msg MACRO msg mov dx,offset msg mov ah,9 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 20 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example2 read_char MACRO mov ah,1 int 21h sub ah,ah push ax ENDM print_char MACRO pop dx mov ah,2 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 21 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example2 .data msg1 db 'How many characters do you want to enter?$' msg2 db 0ah,0dh,'Enter characters:',0ah,0dh,'$' resmsg db 0ah,0dh,'Characters you entered in reverse order:',0ah,0dh,'$' errormsg db 0ah,0dh,'Error,You should enter a number$' counter dw ? .code .startup print_msg msg1 read_char 2005 S. Dandamudi Chapter 5: Page 22 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example2 pop counter sub counter,30h cmp counter,1 jl error cmp counter,9 jg error mov cx,counter print_msg msg2 read_loop: read_char loop read_loop print_msg resmsg mov cx,counter 2005 S. Dandamudi Chapter 5: Page 23 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example2 print_loop: print_char loop print_loop exit:.exit error:print_msg errormsg jmp exit end 2005 S. Dandamudi Chapter 5: Page 24 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 ; Assembly language that reads n numbers ; and then prints all even numbers in reverse ; order using stack print_msg MACRO msg mov dx,offset msg mov ah,9 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 25 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 read_char MACRO mov ah,1 int 21h mov bl,al sub bl,30h sub bh,bh ENDM newline MACRO mov dl,0ah mov ah,2 int 21h mov dl,0dh int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 26 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 print_char MACRO pop dx add dl,30h mov ah,2 int 21h newline ENDM .586 option segment:use16 .model small .stack 200h 2005 S. Dandamudi Chapter 5: Page 27 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 .data msg1 db 'How many numbers do you want to enter?$' msg2 db 0ah,0dh,'Enter numbers:',0ah,0dh,'$' resmsg db 0ah,0dh,'Even numbers you entered in reverse order:',0ah,0dh,'$' errormsg db 0ah,0dh,'Error,You should enter a number$' ecounter dw 0 .code .startup print_msg msg1 read_char cmp bl,1 jl error 2005 S. Dandamudi Chapter 5: Page 28 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 cmp bl,9 jg error mov cl,bl sub ch,ch print_msg msg2 read_loop: read_char test bl,01h jnz next push bx inc ecounter 2005 S. Dandamudi Chapter 5: Page 29 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Assembly Example3 next: newline loop read_loop print_msg resmsg mov cx,ecounter print_loop: print_char loop print_loop exit:.exit error:print_msg errormsg jmp exit end 2005 S. Dandamudi Chapter 5: Page 30 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Uses of the Stack (cont’d) Transfer of Control • In procedure calls and interrupts, the return address is stored on the stack • Our discussion on procedure calls clarifies this particular use of the stack Parameter Passing • Stack is extensively used for parameter passing • Our discussion later on parameter passing describes how the stack is used for this purpose 2005 S. Dandamudi Chapter 5: Page 31 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Procedures • Two types Call-by-value » Receives only values » Similar to mathematical functions Call-by-reference » Receives pointers » Directly manipulates parameter storage 2005 S. Dandamudi Chapter 5: Page 32 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Instructions for Procedures • Pentium provides two instructions: call and ret • call instruction is used to invoke a procedure • The format is call proc-name proc-name is the procedure name • Actions taken during a near procedure call: ESP = ESP - 4 ; push return address SS:ESP = EIP ; onto the stack EIP = EIP + relative displacement ; update EIP to point to the procedure 2005 S. Dandamudi Chapter 5: Page 33 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Instructions for Procedures (cont’d) • ret instruction is used to transfer control back to the calling procedure • How will the processor know where to return? Uses the return address pushed onto the stack as part of executing the call instruction Important that TOS points to this return address when ret instruction is executed • Actions taken during the execution of ret are: EIP = SS:ESP ESP = ESP + 4 2005 ; pop return address ; from the stack S. Dandamudi Chapter 5: Page 34 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pentium Instructions for Procedures (cont’d) • We can specify an optional integer in the ret instruction The format is ret Example: optional-integer ret 8 • Actions taken on ret with optional-integer are: EIP = SS:ESP ESP = ESP + 4 + optional-integer 2005 S. Dandamudi Chapter 5: Page 35 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. How Is Program Control Transferred? Offset(hex) 00000002 00000007 0000001D machine code(hex) main: . . . . E816000000 call sum 89C3 mov EBX,EAX . . . . ; end of main procedure sum: 55 push EBP . . . . ; end of sum procedure avg: 00000028 0000002D 2005 . . . . E8F0FFFFFF call sum 89D8 mov EAX,EBX . . . . ; end of avg procedure S. Dandamudi Chapter 5: Page 36 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Procedure Declaration in MASM • The syntax of procedure declaration is the following: name PROC ; here goes the code ; of the procedure ... RET name ENDP 2005 S. Dandamudi Chapter 5: Page 37 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Procedure Declaration in MASM • Procedure is a part of code that can be called from your program in order to make some specific task. • Procedures make program more structural and easier to understand. • Generally procedure returns to the same point from where it was called. 2005 S. Dandamudi Chapter 5: Page 38 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Parameter Passing • Parameter passing is different and complicated than in a high-level language • In assembly language » You should first place all required parameters in a mutually accessible storage area » Then call the procedure • Type of storage area used » Registers (general-purpose registers are used) » Memory (stack is used) • Two common methods of parameter passing: » Register method » Stack method 2005 S. Dandamudi Chapter 5: Page 39 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Parameter Passing: Register Method • Calling procedure places the necessary parameters in the general-purpose registers before invoking the procedure through the call instruction • Examples: Example1: » call-by-value using the register method » a simple sum procedure Example2: » call-by-reference using the register method » string length procedure 2005 S. Dandamudi Chapter 5: Page 40 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method ; Assembly language that adds two numbers using ;call by value procedure .586 option segment:use16 .model small print_msg Macro msg mov dx,offset msg mov ah,9 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 41 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method newline Macro mov dl,0ah mov ah,2 int 21h mov dl,0dh int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 42 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method read_num Macro mov ah,1 int 21h sub al,30h ENDM .DATA msg1 db 'Please input the first number:$' msg2 db 'Please input the second number:$' sum_msg db 'The sum is: $' 2005 S. Dandamudi Chapter 5: Page 43 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method .code .startup print_msg msg1 read_num mov cl,al newline print_msg msg2 read_num mov ch,al 2005 S. Dandamudi Chapter 5: Page 44 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method newline call sum print_msg sum_msg call print_res .exit ; return to operating system. sum PROC add ch,cl RET ; return to caller. sum ENDP 2005 S. Dandamudi Chapter 5: Page 45 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example1 : Register Method print_res PROC mov dl,ch add dl,30h mov ah,2 int 21h RET ; return to caller. print_res ENDP end 2005 S. Dandamudi Chapter 5: Page 46 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example2 : Register Method ; Assembly language that prints the length of text ; using call by reference procedure .586 option segment:use16 .model small print_msg Macro msg mov dx,offset msg mov ah,9 int 21h ENDM 2005 S. Dandamudi Chapter 5: Page 47 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example2 : Register Method .DATA text db 'Assembly$' txt db 'The length of text <$' res db '> is $' 2005 S. Dandamudi Chapter 5: Page 48 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example2 : Register Method .code .startup mov bx,offset text print_msg txt print_msg text print_msg res call txt_len add dl,'0' mov ah,2 int 21h .exit 2005 S. Dandamudi Chapter 5: Page 49 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example2 : Register Method txt_len PROC sub dl,dl loops: mov dh,[bx] cmp dh,'$' je str_len_done inc bx inc dl jmp loops 2005 S. Dandamudi Chapter 5: Page 50 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Example2 : Register Method str_len_done: RET ; return to caller. txt_len ENDP end 2005 S. Dandamudi Chapter 5: Page 51 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005. Pros and Cons of the Register Method • Advantages Convenient and easier Faster • Disadvantages Only a few parameters can be passed using the register method – Only a small number of registers are available Often these registers are not free – freeing them by pushing their values onto the stack negates the second advantage 2005 S. Dandamudi Chapter 5: Page 52 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.