flat assembler
Message board for the users of flat assembler.

Index > Main > fasm C version (as close as possible)

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi there,
is there possible for mod to stick a thread that explain how to use fasm as close as possible like C (with all the default fasm macro package support)?

eg, the C feature, if, elseif, while, do, switch ... case, and more.

it is still fasm, but just we use dd in every types.

anyone got time and .... sort of samaritan's heart?
Post 14 May 2009, 03:47
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
I don't get what you mean. The above features you enumerated do not generate instructions, they more like preprocessor (although fasm has an ever previous pre-preprocessor Smile).

_________________
Previously known as The_Grey_Beast
Post 14 May 2009, 21:55
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
i mean, to put a list of example on how to use the fasm macroinstruction in a C like pattern.

eg.
Code:
cinvoke   printf,'EAX = 0x%X',eax

.if eax = 8
        jmp     somewhere
.elseif
        jmp     anotherwhere
.endif
    
Post 17 May 2009, 14:11
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
Ah you're talking about .if, not if

there's a difference.
Post 17 May 2009, 19:30
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
how to use the .while? and .wend?
i tried but to no avail Sad
anyone wanna chip in?
Post 18 May 2009, 00:21
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
sleepsleep, something like this?
Code:
; This is a reproduction of the Fahrenheit-Celsius table code from "C Programming Language (2nd Edition)"

UPPER = 300
LOWER = 0
STEP  = 20

format pe console
entry main
include 'win32ax.inc'

main:
  sub     esp, 4*3 ; Reserve space for printf arguments
  virtual at esp
    .fmt     dd ?
    .fahr    dd ?
    .celsius dd ?
  end virtual

  ; fahr = lower
  mov    esi, LOWER

  .while esi <= UPPER
     ; celsius = 5 * (fahr-32) / 9
     lea     eax, [esi*4+esi-32*5]
     cdq
     mov     ecx, 9
     idiv    ecx
     ; printf("%d\t%d\t", fahr, celsius);
     mov     [.fmt], fmt
     mov     [.fahr], esi
     mov     [.celsius], eax
     call    [printf]

     ; fahr = fahr + step;
     add     esi, STEP
  .endw

  add     esp, 4*3 ; Probably not required
  invoke  ExitProcess, 0

fmt db "%d", 9,"%d", 10, 0

align 4 ; Just to be safe
data import 
  library kernel32, 'kernel32.dll',\
          msvcrt,'msvcrt.dll'

  import kernel32,\
         ExitProcess, 'ExitProcess'

  import msvcrt,\
         printf, 'printf'
end data    
Post 18 May 2009, 00:49
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks loco, that is exactly what i hope this thread turns out.

more people, chips in Smile come on..

switch, select case...
how to use array ... chip in.. this thread will forsure help many people who coming from C.
Post 18 May 2009, 13:25
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
Complete program: http://board.flatassembler.net/topic.php?t=9398
It use the c++ runtime, msvcrt.dll
Code:
  mov     [sock_addr.sin_addr],NULL 
    invoke  bind, [sock], sock_addr,sizeof.sockaddr_in 
    .if eax <> 0 
        invoke  WSAGetLastError 
        cinvoke printf, szError, eax         
    .endif     
    invoke listen, [sock],MAX_QUEUE 
accepted: 
    ;socket listening 
    invoke accept, [sock],peerAddr,sizePeerAddr 
    mov     [peer],eax  
    stdcall ipToString,[peerAddr.sin_addr],szIp 
    cinvoke printf, szClient,szIp 
    ;new thread for each connection 
    invoke CreateThread, NULL,NULL, resolveConnection,[peer],NULL,NULL 
    jmp     accepted 
        invoke  ExitProcess,0 

;the connection manager 
proc resolveConnection, lpParam 
local lpeer: DWORD, lbuffer: DWORD, lurl: DWORD, lfile: DWORD  
    mov     eax, [lpParam] 
    mov     [lpeer],eax 
    cinvoke malloc, BUFFER_SIZE 
    mov     [lbuffer],eax 
    invoke  recv, [lpeer],[lbuffer],BUFFER_SIZE,0  
    ;if there is a message then continue 
    .if eax <> 0    
        mov ebx,[lbuffer] 
        mov byte [ebx+eax],0 
        .if SHOW_DETAILED_MSG 
            cinvoke printf,szRequest, [lbuffer]    
        .endif 
        cinvoke strtok, [lbuffer], szTockens 
        .if eax <> 0  
            ;handling the GET message 
            .if dword[eax] = "GET"           
                ;url sring allocation 
                ;getting the URL from GET message 
                cinvoke malloc, MAX_URL_SIZE 
                mov    [lurl],eax 
                cinvoke strcpy, [lurl],szRoot 
                cinvoke strtok, NULL, szTockens 
                cinvoke strcat,[lurl],eax   
                mov     ebx,eax      
                cinvoke strlen, eax 
                mov edx,eax 
                dec edx 
                .repeat  
                    dec eax 
                    cmp byte[ebx+eax],"/" 
                    .if ZERO? 
                        mov byte[ebx+eax],"\" 
                    .endif                     
                .until eax = 0 
                .if byte[ebx+edx] = "\" 
                    cinvoke strcat,[lurl],szIndex 
                .endif 
                cinvoke printf, szFile, [lurl] 
                ;opening the file to send to browser 
                invoke  CreateFile,[lurl],GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,0 
                ;file exists, sending,,,, 
                .if eax <> INVALID_HANDLE_VALUE  
                    mov [lfile],eax             
                    invoke  GetFileSize, [lfile],NULL 
                    invoke  TransmitFile, [lpeer],[lfile],eax,NULL,NULL,transmiteBuffer,NULL  
                    .if eax 
                        cinvoke printf, szStatus, szFileSent 
                    .endif 
                    invoke  CloseHandle, [lfile] 
                .else 
                    cinvoke printf, szStatus, szFileNotExists                     
                    invoke  send, [lpeer], h404, h404.size, 0                
                .endif 
                cinvoke free, [lurl] 
            .endif 
        .endif 
    .endif 
    .exit: 
    cinvoke free, [lbuffer] 

    
Post 18 May 2009, 15:15
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
Here is an example of the SetConsoleCtrlHandler function that is used to install a control handler.

Code:
            ; Registering a Control Handler Function
            ; http://msdn.microsoft.com/en-us/library/ms685049(v=vs.85).aspx

            format pe console
            entry main

            include "win32ax.inc"

            macro return x {
                mov eax, x
                ret }

            crlf equ 13,10

section ".code" code readable executable
main:
            invoke SetConsoleCtrlHandler, CtrlHandler, TRUE
            .if ( eax )

                cinvoke printf, <crlf,"The Control Handler is installed.",crlf>
                cinvoke printf, <crlf," -- Now try pressing Ctrl+C or Ctrl+Break, or">
                cinvoke printf, <crlf,"    try logging off or closing the console...",crlf>
                cinvoke printf, <crlf,"(...waiting in a loop for events...)",crlf,crlf>

                .while ( 1 )

                .endw

            .else

                cinvoke printf, <crlf,"ERROR: Could not set control handler">
                return 1

            .endif

            return 0


proc CtrlHandler uses ecx edx, fdwCtrlType:DWORD

            mov eax, [fdwCtrlType]

            ; Handle the CTRL-C signal.
            .if ( eax = CTRL_C_EVENT )
                cinvoke printf, <"Ctrl-C event",crlf,crlf>
                return TRUE

            ; CTRL-CLOSE: confirm that the user wants to exit.
            .elseif ( eax = CTRL_CLOSE_EVENT )
                cinvoke printf, <"Ctrl-Close event",crlf,crlf>
                return TRUE

            ; Pass other signals to the next handler.
            .elseif ( eax = CTRL_BREAK_EVENT )
                cinvoke printf, <"Ctrl-Break event",crlf,crlf>
                return FALSE

            .elseif ( eax = CTRL_LOGOFF_EVENT )
                cinvoke printf, <"Ctrl-Logoff event",crlf,crlf>
                return FALSE

            .elseif ( eax = CTRL_SHUTDOWN_EVENT )
                cinvoke printf, <"Ctrl-Shutdown event",crlf,crlf>
                return FALSE

            .endif

                .default:
            return FALSE
endp


section ".idata" import data readable writeable

            library\
                kernel32, "kernel32.dll",\
                msvcrt, "msvcrt.dll"

            import kernel32,\
                SetConsoleCtrlHandler, "SetConsoleCtrlHandler"

            import msvcrt,\
                printf, "printf"
    


Last edited by Picnic on 23 Dec 2012, 19:57; edited 1 time in total
Post 18 May 2009, 21:20
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
how bout some simplified example on array?
we can do it Smile come on Razz
Post 19 May 2009, 00:43
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
I'm not sure if that's what you want but checkout this example by Tomasz shows how to make arrays in a HLL-ish syntax using macros.

http://board.flatassembler.net/topic.php?t=3244
Post 19 May 2009, 07:24
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
cool, thanks Smile thimis & tomasz
Post 19 May 2009, 22:07
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
BTW, I haven't noticed before but this thread is in Heap. Do you think it should be moved? It should be moved to Main? Which other forum if not Main?
Post 08 Jun 2009, 15:13
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
I don't know the current state of FASM macros, but I guess it's possible to implement a programming language with it.

Maybe parsing the source codes to adjust to the macros when compiling, but it really helps anyway.

example:

stdio.h
Code:
; C macros
macro init_libc {
      jmp @f
      crt dd 0
      @@:
      invoke LoadLibrary, 'MSVCRT.DLL'
      mov [crt], eax
}

macro load_C_API api {
      invoke GetProcAddress, [crt], api
}

macro unload_libc {
      invoke FreeLibrary, [crt]
}

macro main {
     format PE console
     include '%fasminc%\win32ax.inc'

main:

     init_libc

     load_C_API 'printf'
     mov [printf], eax

     jmp @f
     printf dd 0
     @@:
}

macro return x {

        unload_libc
        invoke ExitProcess, x
.end main
}
; End C macros
    


main.asm:
Code:
include 'stdio.h'

main ;() {
        cinvoke printf, <'Hello world!',13,10,0>
        return 0
;}
    


Have fun! Very Happy
Post 08 Jun 2009, 18:33
View user's profile Send private message Reply with quote
Raedwulf



Joined: 13 Jul 2005
Posts: 375
Location: United Kingdom
Raedwulf
LocoDelAssembly wrote:
BTW, I haven't noticed before but this thread is in Heap. Do you think it should be moved? It should be moved to Main? Which other forum if not Main?


Highlevel languages?

But LocoDelAssembly... sleepsleep would never find this thread if it wasn't in Heap Laughing

_________________
Raedwulf
Post 10 Jun 2009, 14:36
View user's profile Send private message MSN Messenger Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
Quote:

sleepsleep would never find this thread if it wasn't in Heap

Laughing Laughing Laughing
Rolling Eyes
Arrow its ok, please adjust it at the place it supposes to be
Post 10 Jun 2009, 15:49
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
OK, there was no serious opposition to the moving and another moderator agreed via PM this thread should be in Main.

Thread moved.
Post 02 Jul 2009, 22:44
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
what about the thimis (if i am correct) api and text, and he mentioned little bit about fix... any idea how to put such ... "feature" into this idea?
Post 03 Jul 2009, 12:59
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: Paradise Falls
Picnic
sleepsleep here is how 'text' replacing 'cinvoke printf,' in Ozzy's example above, using the fix directive.

main.asm:
Code:
include 'stdio.h'

main ;() {
        text fix cinvoke printf,   
        wait1sec fix cinvoke Sleep,1000    

        text <'Hello world!',13,10,0>
        wait1sec             
        return 0
;}
    
Post 03 Jul 2009, 17:00
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9072
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks thimis Wink cool
Post 04 Jul 2009, 02:38
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.