News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Using Local Variables

Started by Robert Collins, January 11, 2005, 12:31:21 AM

Previous topic - Next topic

thomasantony

Hi,
     I want to implement local variables in the function in my OS made using NASM. I have a doubt. SS is set at linear address 90000h. Then how can I add 0FFFFFFF4h etc to it when I have no memory manager to manage address spaces etc. Do I just add the size of the local variable, ie.e a 4 for a dword , 2 for a word etc?

Thomas Antony
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

Vortex

Rain Dog,


void myfunc()
{
    int x = 5;
}


An equivalent in asm:

_myfunc:
                enter   4,0
                mov     dword ptr -4[EBP],5
                leave
                ret

MichaelW

Hi Thomas,

I don't see what the setting of SS has to do with using local variables. Your procedure will manipulate only the stack pointer, and the only potential problems I can see would be if the stack grows so far downwards that is overwrites something else in the same segment, or the stack pointer collides with the base of the segment.

In the instruction:

add  esp, 0FFFFFFF4h

The 0FFFFFFF4h is actually -12, so the instruction is effectively subtracting 12 from ESP, as would be necessary to reserve space on the stack for 3 local dwords.

Here are examples of NASM procedures that use local variables. Compared to MASM, NASM automatic local variables are cumbersome to use. For simple procedures where you can easily keep track of which local variable is at which offset, I think the manual method would be a better choice.

segment .data

  NL  db 13,10,0

segment .text

%stacksize flat

%include "nasm_macros.asm"

extern _cprintf
extern exit

global _manual, _auto

; Manual local variables
_manual:
    push ebp              ; preserve ebp before changing it
    mov ebp,esp           ; init ebp as stack frame pointer
    sub esp,8             ; reserve space for 2 local dwords

    mov [ebp-4],esi
    mov [ebp-8],edi
    mov esi,[ebp-8]
    mov edi,[ebp-4]

    mov esp,ebp           ; restore esp
    pop ebp               ; recover ebp
    ret

; Automatic local variables
_auto:
    %push currentcontext
    %assign %$localsize 0 ; must be defined in the current
                          ;  context to use %local
    %local local1:dword, local2:dword
      enter %$localsize,0
 
      mov [local1],esi
      mov [local2],edi
      mov esi,[local2]
      mov edi,[local1]
 
      leave
      ret
    %pop

Start:
    mov   esi,1234
    mov   edi,5678
    INVOKE _cprintf, "esi = %d edi = %d%s", esi, edi, NL
    add esp,16

    call _manual

    INVOKE _cprintf, "esi = %d edi = %d%s", esi, edi, NL
    add esp,16

    call _auto

    INVOKE _cprintf, "esi = %d edi = %d%s%s", esi, edi, NL, NL
    add esp,20

    INVOKE _cprintf, "Press any key to exit..."
    add esp,4

    PAUSE
    call exit


Before you try to build this, take a look at the comments in MAKEIT.BAT (in the attachment)



[attachment deleted by admin]
eschew obfuscation

thomasantony

Thanx for the info. I just started using leave instead of pop ebp crap in my OS. Maybe I will change the pus ebp stuff to enter thingy also. I had the doubt abt 0FFFFFFFFh cuz I for got that 0FFFFFFFFh was -1 and not 4gig :green2

Thomas Antony
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free