45
Assembly Language Procedures and the Stack

Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

  • View
    228

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Assembly LanguageProcedures and the Stack

Page 2: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Stack

• A stack is a last-in–first-out (LIFO) data structure. • Insert and delete operations are referred to as push

and pop operations, respectively.

Page 3: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Pentium’s Stack• The stack grows toward lower memory addresses.• Top-of-stack (TOS) always points to the last inserted item (points to

the lower byte of the last word inserted into the stack).• The registers SS and ESP are used to implement the Pentium stack. • SS:ESP points to the top-of-stack (the last item inserted).

– SS register pointing to the beginning of the stack segment.– ESP register giving the offset value of the last item

Page 4: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Pentium’s Stack

Page 5: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Basic Instructions

The push and pop instruction operate on word or doubleword data items.

Syntax:

push source

pop destination

• The operand can be a 16- or 32-bit – general-purpose register, segment register

– A word or doubleword in memory.

– For push instruction the course can be an immediate of size 8, 16, or 32 bits. operations.

push 21ABH

push 7FBD329AH

pop EBX

Page 6: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Stack Instruction Summary

Page 7: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Other Stack Instructions

pushfd (push 32-bit flags)

popfd (pop 32-bit flags)

pusha and popa instructions to save and restore the eight general-purpose registers. pushad saves the 32-bit general-purpose registers EAX, ECX, EDX,

EBX, ESP, EBP, ESI, and EDI. These registers are pushed in the order specified.

popad restores these registers except that it will not copy the ESP value

The corresponding instructions for the 16-bit registers are pushaw and popaw.

Page 8: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Temporary Storage of Data. . .;Save EAX and EBX registers on the stackpush EAXpush EBX;EAX and EBX registers can now be usedmov EAX,value1mov EBX,value2mov value1,EBXmov value2,EAX;restore EAX and EBX registers from the stackpop EBXpop EAX. . .

mov EAX,value1mov EBX,value2mov value1,EBXmov value2,EAX

Page 9: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Example Illustration

Page 10: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Procedures

Sub-program (routine) is a logically self-contained unit of code designed to perform a particular task.– Functions

Receives a list of arguments and performs a computation based on the arguments passed onto it and returns a single value.

Procedures

Receives a list of arguments and works similar to functions, but does not

return a value. call and ret (return) instructions are used to handle sub-programs.call routine_name ; routine_name is the name of the routine to be called

Ret return to the instruction following the last call.

Page 11: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Transfer of Control

• The offset value provide by the call instruction is relative displacement in bytes from the instruction following the call instruction.

• The processor adds the 32-bit relative displacement found in the call instruction to the contents of the EIP register.

• The Pentium schemeESP = ESP − 4 ; push return address onto the stack

SS:ESP = EIP

EIP = EIP + relative displacement ; update EIP to point to the procedure

Page 12: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Transfer of Control-Example

Page 13: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Transfer of Control-Example

• E816000000H is the machine language encoding of the call instruction

002 call instruction– E8H is the opcode

– 00000016H (32bit signed) is a relative displacement 0000001DH − 00000007H = 00000016H (in little-endian order)

002 call instruction – The displacement is 0000001DH − 0000002DH = FFFFFFF0H.

– A negative numbers corresponds to −10H (i.e., −16D

Page 14: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

The ret Instruction

• ret [integer] ; the integer is optional

• The ret (return) instruction is used to transfer control from the called procedure to the calling procedure.

• The Pentium Scheme EIP = SS:ESP ; pop return address at TOS into IP

ESP = ESP + 4 ; update TOS by adding 4 to ESP

Page 15: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Parameter Passing

• Parameters could be passed from the caller routine to the call routine in two methods– The register method uses general-purpose registers to pass

parameters.– The stack method passes the parameters in the stack.

• There are two types of parameter passing mechanisms– call-by-value - The called function is provided only the current

value of the arguments for its use.• The values of these actual parameters are not changed in the called

function call-by-reference.

– call-by-value - The called function actually receives the addresses of the parameters from the calling function.

• The function can change the contents of these

Page 16: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Register Method

• The calling routine places the necessary parameters in the general purpose registers before call the routine.

• The called routine has access to these register and their values.

• To pass parameters by value, the current value of these parameters are placed in the registers.

• To pass parameters by reference, the address of these parameters are placed in the registers.

Page 17: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

call-by-value using registers.Example

%include "io.mac".DATA

prompt_msg1 db "Please input the first number: ",0prompt_msg2 db "Please input the second number: ",0sum_msg db "The sum is ",0

.CODE .STARTUP

PutStr prompt_msg1 ; request first number GetInt CX ; CX = first number

PutStr prompt_msg2 ; request second numberGetInt DX ; DX = second numbercall sum ; returns sum in AXPutStr sum_msg ; display sumPutInt AXnewline

done:.EXIT

Page 18: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

call-by-value using registers.Example

;-----------------------------------------------------------;Procedure sum receives two integers in CX and DX.;The sum of the two integers is returned in AX.;-----------------------------------------------------------sum:

mov AX,CX ; sum = first numberadd AX,DX ; sum = sum + second numberret

Page 19: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

call-by-reference using registers.Example

%include "io.mac"BUF_LEN EQU 41 ; string buffer length

.DATAprompt_msg db "Please input a string: ",0length_msg db "The string length is ",0

.UDATAstring resb BUF_LEN ;input string < BUF_LEN chars.

.CODE

.STARTUPPutStr prompt_msg ; request string inputGetStr string,BUF_LEN ; read string from keyboardmov EBX,string ; EBX = string addresscall str_len ; returns string length in AXPutStr length_msg ; display string lengthPutInt AXnwwline

done: .EXIT

Page 20: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

call-by-reference using registers.Example

;-----------------------------------------------------------;Procedure str_len receives a pointer to a string in EBX.;String length is returned in AX.;-----------------------------------------------------------str_len:

push EBXsub AX,AX ; string length = 0

repeat:cmp byte [EBX],0 ; compare with NULL char.je str_len_done ; if NULL we are doneinc AX ; else, increment string lengthinc EBX ; point EBX to the next char.jmp repeat ; and repeat the process

str_len_done:pop EBXret

Page 21: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Passing Parameters Via Registers

• Advantages– The register method is convenient and easier for passing a small

number of parameters.

– This method is also faster because all the parameters are available in registers.

• Disadvantages– Only a few parameters can be passed by using registers, as there is

a limited number of general-purpose registers available.

– The general-purpose registers are often used by the calling procedure for some other purpose. Thus, it is necessary to temporarily save their contents before calling a procedure, and restore them after returning.

Page 22: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Passing Parameters using the Stack• The parameters are pushed onto the stack before the procedure is

called.Example

push param1push param2call sum

• How to access these parameters pop EAXpop BXpop CX

• And then push EAX

• This approach is problematic, as it require using more registers and involves unnecessary push and pop operations.

Page 23: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Accessing Parameters

• To simplify accessing the parameters, it is better to leave them on the stack and read them off the stack as needed.

• The stack is a sequence of memory locations – ESP + 4 points to param2– ESP + 6 points to param1 – Ex. mov BX,[ESP+4]

• However, the stack pointer is updated by the push and pop instructions, which changes the relative

• We can use the EBP register instead of ESP to specify an offset into the stack segment.mov EBP, ESPmov AX, [EBP+4]

Page 24: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Accessing Parameters

Param1Param2 Param1

Param2 Param1Param2

Save the EBP register and set to ESPpush EBPmov EBP,ESP…

Before returning restore the EBP register pop EBP

Page 25: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Organizing the Stack• After completing the routine (procedure) the memory of the stack occupied by

the parameters are no longer useful. • To free this bytes, one could increment ESP accordingly after the call

push param1 push param2 call sum cdd ESP, 4

• Or before returning sum:

. . .add ESP,4ret

• But the best solution is using the argument of the ret instruction.ret optional-valueWhich mean (optional-value is a 16-bit immediate ).EIP = SS:ESPESP = ESP + 4 + optional-value

Page 26: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

The State of The Calling Routine

• Routines share the system registers • The system registers are an essential part of each routine

– Save the registers that are used by the calling routine but changed by the called procedure.

– Which routine , the calling or the called, should save the registers?– The programs will be longer as they need to save and restore

register for each time a routine is called.

• The calling routine saves the registers – It needs to know the registers used by the called procedure.

• The called routine saves the registers – It know what registers it is going to use and save them.– It also restore them before it returns.

Page 27: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Why not to use Pusha and Popa

• Some of the registers saved by pusha are used for returning results? – The EAX register is often used to return integer results.

• Pusha is usuall more expensive than a singe push.– Pusha takes around five time the time of a single push

– It is more efficient to save one or two registers using push than using pusha

• pusha improves the readability of code and reduces memory required for the instructions.

Page 28: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

ENTER and LEAVE Instructions

• The enter instruction can be used to allocate a stack frame on entering a routine.– enter bytes, level– bytes specifies the number of bytes of local variable storage we want on

the stack. level the nesting level of the procedure; a nonzero level copies level stack frame pointers into the new frame.

enter XX,0

• The leave instruction

proc-name:enter XX,0. . .routine body. . .leaveret YY

push EBPmov EBP,ESPsub ESP,XX

mov ESP,EBPpop EBP

Page 29: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example%include "io.mac".DATA

prompt_msg1 db "Please input the first number: ",0prompt_msg2 db "Please input the second number: ",0sum_msg db "The sum is ",0

.CODE

.STARTUPPutStr prompt_msg1 ; request first numberGetInt CX ; CX = first numberPutStr prompt_msg2 ; request second numberGetInt DX ; DX = second numberpush CX ; place first number on stackpush DX ; place second number on stackcall sum ; returns sum in AXPutStr sum_msg ; display sumPutInt AXnewline

done:.EXIT

Page 30: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example

;-----------------------------------------------------------;Procedure sum receives two integers via the stack.; The sum of the two integers is returned in AX.;-----------------------------------------------------------sum:

enter 0,0 ; save EBPmov AX,[EBP+10] ; sum = first numberadd AX,[EBP+8] ; sum = sum + second numberleave ; restore EBPret 4 ; return and clear parameters

Page 31: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example%include "io.mac".DATA

prompt_msg db "Please input a string: ",0output_msg db "The swapped string is: ",0

.UDATAstring resb BUF_LEN ;input string < BUF_LEN chars.

.CODE

.STARTUPPutStr prompt_msg ; request string inputGetStr string,BUF_LEN ; read string from the usermov EAX,string ; EAX = string[0] pointerpush EAXinc EAX ; EAX = string[1] pointerpush EAXcall swap ; swaps the first two charactersPutStr output_msg ; display the swapped stringPutStr stringnewlinedone:

.EXIT

Page 32: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example;-----------------------------------------------------------;Procedure swap receives two pointers (via the stack) to; characters of a string. It exchanges these two characters.;-----------------------------------------------------------.CODEswap:

enter 0, 0push EBX ; save EBX - procedure uses EBX; swap begins here. Because of xchg, AL is preserved.mov EBX, [EBP+12] ; EBX = first character pointerxchg AL, [EBX]mov EBX, [EBP+8] ; EBX = second character pointerxchg AL, [EBX]mov EBX, [EBP+12] ; EBX = first character pointerxchg AL, [EBX]; swap ends herepop EBX ; restore registersleaveret 8 ; return and clear parameters

Page 33: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example%define CRLF 0DH, 0AHMAX_SIZE EQU 20%include "io.mac".DATA

prompt_msg db "Enter nonzero integers to be sorted.",CRLFdb "Enter zero to terminate the input.",0output_msg db "Input numbers in ascending order:",0

.UDATAarray resd MAX_SIZE ; input array for integers

.CODE

.STARTUPPutStr prompt_msg ; request input numbersnewlinemov EBX, array ; EBX = array pointermov ECX, MAX_SIZE ; ECX = array sizesub EDX, EDX ; number count = 0

read_loop:GetLInt EAX ; read input numbercmp EAX, 0 ; if the number is zeroje stop_reading ; no more numbers to read

Page 34: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Examplemov [EBX], EAX ; copy the number into arrayadd EBX,4 ; EBX points to the next elementinc EDX ; increment number countloop read_loop ; reads a max. of MAX_SIZE numbers

stop_reading:push EDX ; push array size onto stackpush array ; place array pointer on stack

call bubble_sortPutStr output_msg ; display sorted input numbersnewlinemov EBX,arraymov ECX,EDX ; ECX = number count

print_loop:PutLInt [EBX]newlineadd EBX,4loop print_loop

done:.EXIT

Page 35: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example;This procedure receives a pointer to an array of integers; and the size of the array via the stack. It sorts the; array in ascending order using the bubble sort algorithm.SORTED EQU 0UNSORTED EQU 1bubble_sort:

pushadmov EBP,ESP; ECX serves the same purpose as the end_index variable; in the C procedure. ECX keeps the number of comparisons; to be done in each pass. Note that ECX is decremented; by 1 after each pass.mov ECX, [EBP+40] ; load array size into ECX

next_pass:dec ECX ; if # of comparisons is zerojz sort_done ; then we are donemov EDI,ECX ; else start another pass;DL is used to keep SORTED/UNSORTED statusmov DL,SORTED ; set status to SORTEDmov ESI,[EBP+36] ; load array address into ESI; ESI points to element i and ESI+4 to the next element

Page 36: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Examplepass:

; This loop represents one pass of the algorithm.; Each iteration compares elements at [ESI] and [ESI+4]; and swaps them if ([ESI]) < ([ESI+4]).mov EAX, [ESI]mov EBX, [ESI+4]cmp EAX, EBXjg swap

increment:; Increment ESI by 4 to point to the next elementadd ESI,4dec EDIjnz passcmp EDX,SORTED ; if status remains SORTEDje sort_done ; then sorting is donejmp next_pass ; else initiate another pass

Page 37: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Exampleswap:

; swap elements at [ESI] and [ESI+4]mov [ESI+4],EAX ; copy [ESI] in EAX to [ESI+4]mov [ESI],EBX ; copy [ESI+4] in EBX to [ESI]mov EDX,UNSORTED ; set status to UNSORTEDjmp increment

sort_done:popadret 8

Page 38: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Variable Number of Parameters

• Functions and procedure may take variable number of parameters – printf and scanf in C

• The called routine is not aware of the number of parameters– The first parameter in the parameter list

specifies the number of parameters

• The stack size imposes a limit on the number of parameters that can be passed.

Page 39: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Variable number of parameters passed via stack

%define CRLF 0DH,0AH ; carriage return and line feed%include "io.mac".DATA

prompt_msg db "Please input a set of nonzero integers.",CRLFdb "You must enter at least one integer.",CRLFdb "Enter zero to terminate the input.",0sum_msg db "The sum of the input numbers is: ",0

.CODE

.STARTUPPutStr prompt_msg ; request input numbersnewlinesub ECX,ECX ; ECX keeps number count

read_number:GetLInt EAX ; read input numbercmp EAX,0 ; if the number is zeroje stop_reading ; no more numbers to readpush EAX ; place the number on stackinc ECX ; increment number countjmp read_number

Page 40: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Variable number of parameters passed via stack

stop_reading:push ECX ; place number count on stackcall variable_sum ; returns sum in EAX; clear parameter space on the stackinc ECX ; increment ECX to include countadd ECX, ECX ; ECX = ECX * 4 (space in bytes)add ECX, ECXadd ESP,ECX ; update ESP to clear parameter; space on the stackPutStr sum_msg ; display the sumPutLInt EAXnewline

done:.EXIT

Page 41: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Variable number of parameters passed via stack

;This procedure receives variable number of integers via the; stack. The last parameter pushed on the stack should be; the number of integers to be added. Sum is returned in EAX.variable_sum:

enter 0,0push EBX ; save EBX and ECXpush ECXmov ECX,[EBP+8] ; ECX = # of integers to be addedmov EBX,EBPadd EBX,12 ; EBX = pointer to first numbersub EAX,EAX ; sum = 0

add_loop:add EAX,[SS:EBX] ; sum = sum + next numberadd EBX,4 ; EBX points to the next integerloop add_loop ; repeat count in ECXpop ECX ; restore registers

pop EBX leave

ret ; parameter space cleared by main

Page 42: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Local Variables

• Consider a code in Cint average(int a, int b){

int temp, N;

. . .

. . .

}

• Allocating local variable in data segment– It is static and remains active even when the

procedure is not.

– It does not work for recursive routines

• Space for local variables is reserved on the stack.

Page 43: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Variable number of parameters passed via stack

stop_reading:push ECX ; place number count on stackcall variable_sum ; returns sum in EAX; clear parameter space on the stackinc ECX ; increment ECX to include countadd ECX, ECX ; ECX = ECX * 4 (space in bytes)add ECX, ECXadd ESP,ECX ; update ESP to clear parameter; space on the stackPutStr sum_msg ; display the sumPutLInt EAXnewline

done:.EXIT

Page 44: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Local variables - Fibonacci numbers

%include "io.mac".DATA

prompt_msg db "Please input a positive number (>1): ",0output_msg1 db "The largest Fibonacci number less than "db "or equal to ",0output_msg2 db " is ",0

.CODE

.STARTUPPutStr prompt_msg ; request input numberGetLInt EDX ; EDX = input numbercall fibonacciPutStr output_msg1 ; display Fibonacci numberPutLInt EDXPutStr output_msg2PutLInt EAXnewline

done:.EXIT

Page 45: Assembly Language Procedures and the Stack. Stack A stack is a last-in–first-out (LIFO) data structure. Insert and delete operations are referred to as

Code Example Local variables - Fibonacci numbers

;Procedure fibonacci receives an integer in EDX and computes; the largest Fibonacci number that is less than or equal to; the input number. The Fibonacci number is returned in EAX.

fibonacci:push EBX; EAX maintains the smaller of the last two Fibonacci; numbers computed ; EBX maintains the larger one.mov EAX, 1 ; initialize EAX and EBX tomov EBX, EAX ; first two Fibonacci numbers

fib_loop:add EAX, EBX ; compute next Fibonacci numberxchg EAX, EBX ; maintain the required ordercmp EBX, EDX ; compare with input number in EDXjle fib_loop ; if not greater, find next number; EAX contains the required Fibonacci numberpop EBXret