flat assembler
Message board for the users of flat assembler.

Index > Main > Global/local scope and functions

Author
Thread Post new topic Reply to topic
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 02 Feb 2020, 16:17
Hello,

im still at the beginning at learning fasm and assembly.

My question is: where are local scoped and global scoped variables are stored in fasm and also how do function definitions and function calls look like in fasm?
Im comming from high level languages and i want to translate simple C programs by hand to understand fasm better.

Suppose i have the following C program:

Code:
int var1 = 1;

int func()
{
    int var2 = 2;
    return var2;
}

int main()
{
    int var2 = func();
    return var2 + var1;
}
    


How would i translate this C code into fasm code?

var1 would probably go in the data section as it is global:
Code:
format PE
entry main

section ".data" data readable writeable
    var1: dd 1

section '.text' code readable executable

main:
    


Where would i store local variables like var2 from func() and main() to reference them correctly in a later stage and free them when there scope ends and how are function definition and calls are realised in fasm?

How would this snippet of C code exactly look like in fasm code?

Thanks a lot!


Last edited by rc on 02 Feb 2020, 19:51; edited 2 times in total
Post 02 Feb 2020, 16:17
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 02 Feb 2020, 16:24
Local variables are stored on the stack.
Code:
my_function:
 sub esp,4  ;make space for one dword
 ;...
 mov dword[esp],my_value ;local variable stored on the stack
 ;...
 add esp,4 ;release the stack variable
 ret    
Sometimes using ebp, instead of esp, is more useful for accessing local variables.
Code:
my_function:
 sub esp,4  ;make space for one dword
 mov ebp,esp
 ;...
 mov dword[ebp],my_value ;local variable stored on the stack
 ;...
 add esp,4 ;release the stack variable
 ret    
Post 02 Feb 2020, 16:24
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 04 Feb 2020, 19:36
Thank you for the help.

So i translated the above C code into fasm code and came up with the following:

Code:
format PE
entry main
include 'win32a.inc'

section '.data' data readable writeable
var1 dd 1


section '.text' code readable executable

main:

        call func

        add eax, [var1]

        push eax

        call [ExitProcess]



func:
        push ebp
        mov ebp, esp

        mov eax, 2

        mov esp, ebp
        pop ebp
        ret


section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL'

  import kernel,\
         ExitProcess,'ExitProcess'
    



Another question that i have is: how would i store floats in fasm?
Post 04 Feb 2020, 19:36
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 04 Feb 2020, 19:42
You can simplify func to this:
Code:
func:
    mov eax,2
    ret    
To use floating point numbers you can do this:
Code:
mov eax,1.6180339887
mov rax,3.1415926535897932384
mov dword[edx],2.718281828459    
Post 04 Feb 2020, 19:42
View user's profile Send private message Visit poster's website Reply with quote
rc



Joined: 03 Nov 2019
Posts: 55
Location: Germany
rc 05 Feb 2020, 20:33
Ah ok, didn't know it was that simple. Thanks!
Post 05 Feb 2020, 20:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.