Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to deal with a local variable.

maengyimaengyi Member Posts: 19
Hi guys.


I am trying to make a program that translate a c code to the assembly code
like complier. But i stacked when i deal with local variable.

what i want to do is,

1, save esp to a variable which is declared in data segment
2, push a variable into stack.
3, access the stack to find out the variable

The problem is this program uses the stack all the time. So, esp is
changing all the time. Even though i saved the pointer of the variable,
i still cannot access the variable in the stack.
Because, We can't access the memory space like.

mov eax, dword ptr [mem + di]
;mem is a variable that declared in data segment
;mem has the value of esp

Is there any easy way to access the data in specific place of stack.
Without waring about the esp.


Sorry for my break English, I am not sure, have i explained clearly.
Feel free to ask me, if you didn't understand my question.

As always,
Thank you all & have a good night.

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    In the (online) book I use it is advised to save the value of ESP in EBP and use that register to reference local variables and parameters (if you are using functions) on the stack.
    The example in my book for x86 assembly (note that: mov dest, src)
    [code]
    push ebp ;Save value of ebp
    mov ebp, esp

    ;Other code here
    ;For example, access a variable on the stack:
    mov eax, [ebp + 8]

    ;at the end restore value of ebp
    pop ebp
    [/code]
    But I'm just a beginner and haven't fully grasped assembly yet.
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [code]:
    : push ebp ;Save value of ebp
    : [color=Red]sub esp, room for local vars ; reserve room[/color]
    : mov ebp, esp
    :
    : ;Other code here
    : ;For example, access a variable on the stack:
    : mov eax, [ebp + 8]
    :
    : ;at the end restore value of ebp
    : pop ebp
    : [/code]:

    [color=Blue]Also, BP was needed for 16-bit DOS coding, because the older CPUs could not use SP to address stuff. Now, you can do just that and be OK:[/color]

    [code]
    function:
    sub esp, room

    ...
    mov eax, [esp + 8]
    ...

    add esp, room
    ret
    [/code]
  • maengyimaengyi Member Posts: 19
    : : [code]: :
    : : push ebp ;Save value of ebp
    : : [color=Red]sub esp, room for local vars ; reserve room[/color]
    : : mov ebp, esp
    : :
    : : ;Other code here
    : : ;For example, access a variable on the stack:
    : : mov eax, [ebp + 8]
    : :
    : : ;at the end restore value of ebp
    : : pop ebp
    : : [/code]: :
    :
    : [color=Blue]Also, BP was needed for 16-bit DOS coding, because the
    : older CPUs could not use SP to address stuff. Now, you can do just
    : that and be OK:[/color]
    :
    : [code]:
    : function:
    : sub esp, room
    :
    : ...
    : mov eax, [esp + 8]
    : ...
    :
    : add esp, room
    : ret
    : [/code]:

    Thank you for your reply.

    I didn't really understood your code. The variable of the room.
    what does it mean.

    I got thought this problem by myself. I did like this.


    [code]
    data segment
    var_ptr dw 0
    data ends

    ...

    mov var_ptr, esp
    ...

    push ebp
    mov ebp, var_ptr

    mov eax, dword ptr [ebp]

    pop ebp


    ...
    [/code]


    This way works good.


    Thank you all. & Have a good day.
  • SerjRDSSerjRDS Member Posts: 8
    ...room is not a variable. it's a constant.
    room = amount of space you reserve for local variables in bytes.
    this number must be aligned on dword boundary. You shouldnt use the method where room is used caz if you use stack in you code f.e. store some registers esp will be modified so it cannot be used to refer to local variables. The best variant is this one (believe my experience)

    : : : push ebp ;Save value of ebp
    : : : mov ebp, esp
    : : : sub esp, room for local vars ; reserve room
    : : :
    : : : ;Other code here
    : : : ;For example, access a variable on the stack:
    : : : mov eax, [ebp - 4]
    : : :
    : : : leave
    : : : ret numberOfBytesUsedForCallArguments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    :[color=Blue]Also, BP was needed for 16-bit DOS coding, because the
    : older CPUs could not use SP to address stuff. Now, you can do just
    : that and be OK:[/color]
    :

    What I gathered from my tutorial was that EBP was used because then you can push and pop all you want in your function. Else, if you push/pop ESP will change along and [esp+8] will become something else.

    At any rate, I have a lot to learn ;)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • anthrax11anthrax11 Member Posts: 511
    : What I gathered from my tutorial was that EBP was used because then
    : you can push and pop all you want in your function. Else, if you
    : push/pop ESP will change along and [esp+8] will become something
    : else.


    [color=Green]Yep, this is called making a stack frame. When you store ESP to EBP, then EBP remains constant, thus you can always use it to address local variables. ESP on the other hand changes when you push/pop.

    Think of it this way:[/color]
    [code]
    SomeFunction:
    push ebp
    sub esp, 8
    mov ebp, esp
    [color=Green]; now 8 bytes (2 dwords) have been allocated for local variables from the stack[/color]

    [color=Green]; you can store local stuff within the address space [ebp] up to [ebp+8],
    ; which currently matches [esp] to [esp+8]

    [color=Black]...[/color]

    ; now let's do something that uses the edi register,
    ; as we'd like to preserve edi, we need to store it[/color]
    push edi

    [color=Green]; esp has changed, but ebp has remained the same
    ; clear the second local dword variable just for the sake of it..[/color]
    mov edi, 4
    mov dword ptr [ebp+edi], 0

    [color=Green]; restore edi[/color]
    pop edi

    ...

    [color=Green]; restore esp and ebp
    ; the following is the equivalent of the [b]leave[/b] instruction, btw[/color]
    mov esp, ebp
    pop ebp

    ret
    [/code]
    [color=Green]Note that some assemblers like Masm take care of the nasty stack frame stuff for you. This is the Masm equivalent of the above code:[/color]
    [code]
    SomeFunction proc uses edi
    LOCAL LocalVars[8]:BYTE

    mov edi, 4
    mov dword ptr [LocalVars+edi], 0

    ret
    SomeFunction endp
    [/code]
    [color=Green]Either way the ebp register never changes within the function
    (unless you need to use it as a general-purpose register, which is rare),
    so you don't need to use a variable in the data section..[/color]


    : At any rate, I have a lot to learn ;)

    [color=Green]Good luck and have fun! ;)[/color]
Sign In or Register to comment.