activation block. usage example

Upload: marianorico

Post on 30-May-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Activation Block. Usage Example

    1/32

    Activation block

    Usage example

    Mariano Rico, UC3M 2010

  • 8/9/2019 Activation Block. Usage Example

    2/32

    Statement

    Write a program that uses the functioncounter. This function has twoparameters: (1)the address of a zero-

    terminated string and (2) a character.This function returns the number oftimes (an integer) that the givencharacter appears in the string

    In C terminology the funtion signaturewould be int counter (char* s, char c)

  • 8/9/2019 Activation Block. Usage Example

    3/32

    Calling the function (bigpicture)

    The main program must

    1) Reserve space in the stack to store theresult(s)

    2) Load the function parameters in thestack (in the right order)

    3) Call the function

    4) Download the function parametersfrom the stack

    5) Get the result from the stack

  • 8/9/2019 Activation Block. Usage Example

    4/32

    Calling the function (step bystep)

    Initial state

    The stack pointer (%esp) points to thetop of the stack

    %esp Initial position

  • 8/9/2019 Activation Block. Usage Example

    5/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    %esp

    Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    6/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    thestringthestring

    thelettertheletter

    %esp

    Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    7/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    @RET

    thestringthestring

    thelettertheletter

    %esp

    Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    8/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %esp

    Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    9/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    @RET

    thestringthestring

    thelettertheletter

    resultresult%esp

    Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    10/32

    Calling the function (step bystep)

    1) Reserve space in the stack to store the result(s)2) Load the functions parameters in the stack (in the right

    order)

    3) Call the function- Execute the funcion

    1) Download the functions parameters from the stack2) Get the result(s) from the stack

    sub $4, %esp

    push theletter

    push thestring

    call counter

    add $8, %esp

    pop %eax

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %esp Initial position

    Results space

    Paramsspace

  • 8/9/2019 Activation Block. Usage Example

    11/32

    The function (big picture)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Initialposition

    Resultsspace

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %esp%esp

    Initial state

    The stack islike this:(step 3 inthe callingprogram)

  • 8/9/2019 Activation Block. Usage Example

    12/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eax

    push %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp%esp%esp

    Initialposition

    Resultsspace

  • 8/9/2019 Activation Block. Usage Example

    13/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

  • 8/9/2019 Activation Block. Usage Example

    14/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    %esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

  • 8/9/2019 Activation Block. Usage Example

    15/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

  • 8/9/2019 Activation Block. Usage Example

    16/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspacelocal var = 0

  • 8/9/2019 Activation Block. Usage Example

    17/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    %eax points tothestring

  • 8/9/2019 Activation Block. Usage Example

    18/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    %eax points tothestring%cl points totheletter

  • 8/9/2019 Activation Block. Usage Example

    19/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    %eax points tothestring%cl points totheletter%ebx is an index (ini

    val=0)

  • 8/9/2019 Activation Block. Usage Example

    20/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop: cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    %eax points tothestring%cl points totheletter%ebx is an index (ini

    val=0)Comparison of byte

    h f i ( b )

  • 8/9/2019 Activation Block. Usage Example

    21/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    If it reaches theend of the stringthen jump to res,otherwise

    h f i ( b )

  • 8/9/2019 Activation Block. Usage Example

    22/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esppop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    compares thebyte in %cl to thebyte pointed by theindex

    Th f i ( b )

  • 8/9/2019 Activation Block. Usage Example

    23/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    If equal (letterdetected) go toincr

    Th f i ( b )

  • 8/9/2019 Activation Block. Usage Example

    24/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    If equal (letterdetected) go toincr increase theindex

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    25/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000000

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    If equal (letterdetected) go toincr increase theindex and do theloop again

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    26/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    0x00000001

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    otherwiseincrease the localvar

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    27/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    Copies the value ofthe local var to%eax

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    28/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx%esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    Copies the valuestored in %eax tothe results space

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    29/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx

    %esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    30/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:

    cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx

    %esp%esp

    Initialposition

    Resultsspace

    %ebp%ebp

    Local vaspace

    Deletes localvariables

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    31/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:

    cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    ret

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx

    %esp%esp

    Initialposition

    Resultsspace

    Local vaspace

    Th f ti ( t b t )

  • 8/9/2019 Activation Block. Usage Example

    32/32

    counter:

    push %ebp

    mov %esp, %ebp

    sub $4, %esp

    push %eaxpush %ebx

    push %ecx

    movl $0, -4(%ebp)

    mov 8(%ebp), %eax

    mov 12(%ebp), %cl

    mov $0, %ebx

    loop:

    cmpb $0, (%eax, %ebx)

    je res

    cmpb %cl, (%eax, %ebx)

    jne incr

    incl -4(%ebp)

    incr:

    inc %ebx

    jmp loopres:

    mov -4(%ebp), %eax

    mov %eax, 16(%ebp)

    pop %ecx

    pop %ebx

    pop %eax

    mov %ebp, %esp

    pop %ebp

    The function (step by step)1) Save the value of%ebp (as a pointer to the

    activation block)

    2) Copy the value of%esp to %ebp

    3) Allocate space in the stack for local variables

    4) Save registries that will be used by thesubroutine

    5) Do the subroutines job6) Store results in the space allocated

    7) Restore the registries used by the subroutine

    8) Move the top of the stack to the activationblock pointer

    9) Restore the value of%ebp

    10) Execute RET

    Paramsspace

    @RET

    thestringthestring

    thelettertheletter

    resultresult

    %ebp

    loc varvalue

    %eax

    %ebx

    %ecx

    %esp%esp

    Initialposition

    Resultsspace

    Local vaspace