flat assembler
Message board for the users of flat assembler.

Index > Windows > "Can't open existing file".

Author
Thread Post new topic Reply to topic
mcmike



Joined: 11 Mar 2019
Posts: 3
Location: Lviv, Ukraine
mcmike 11 Mar 2019, 23:38
Hey, I'm a beginner in learning fasm. I decided to start with something simple and wrote this code:
Code:
format PE
entry main
section '.text' code readable executable
main:
          mov   eax,    2
          add   eax,    3
          add   eax,    4
ret
karl:
         push   ebp
          mov   ebp,    esp
          mov   eax,    0
          mov   esp,    ebp
          pop   ebp
ret
section '.data' data readable writeable
duper equ 1    


When I compile and execute, windows says "Can't open existing file". What is this code lacking to execute? I know that there is nothing to see, but I want to at least get a return value. Thank you in advance!
Post 11 Mar 2019, 23:38
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 11 Mar 2019, 23:48
You will need to link the output with a linker to make an executable file.

Another option is to use fasm itself to make the executable directly.
Code:
format pe console
...    
Or
Code:
format pe gui
...    
Have a look at the example files that are in the fasm download.
Post 11 Mar 2019, 23:48
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 11 Mar 2019, 23:51
you need at least 1 winapi call in order to execute the file, as win loader will say its not a valid win32 application.

Code:
format PE
entry main
section '.text' code readable executable
main:
          mov   eax,    2
          add   eax,    3
          add   eax,    4 ; eax = 9
ret ; on ret you will exit with error code 9
karl: ; karl is never called or executed
         push   ebp
          mov   ebp,    esp
          mov   eax,    0
          mov   esp,    ebp
          pop   ebp
ret
section '.data' data readable writeable
duper equ 1    

_________________
Asm For Wise Humans
Post 11 Mar 2019, 23:51
View user's profile Send private message Reply with quote
mcmike



Joined: 11 Mar 2019
Posts: 3
Location: Lviv, Ukraine
mcmike 12 Mar 2019, 10:00
Ali.Z wrote:
you need at least 1 winapi call in order to execute the file, as win loader will say its not a valid win32 application.

Code:
format PE
entry main
section '.text' code readable executable
main:
          mov   eax,    2
          add   eax,    3
          add   eax,    4 ; eax = 9
ret ; on ret you will exit with error code 9
karl: ; karl is never called or executed
         push   ebp
          mov   ebp,    esp
          mov   eax,    0
          mov   esp,    ebp
          pop   ebp
ret
section '.data' data readable writeable
duper equ 1    


Thank you for the hint! I know that karl is never called, I decided to include it still for future reference. I have modified code like this:
Code:
format PE64 console 
entry main 
include 'win64a.inc' 

section '.code' code readable executable 
main: 
  call karen
  mov eax, 2
  add eax, 3
  add eax, 4
          invoke        ExitProcess, eax
          
karen:
        push ebp
        mov ebp, esp
        mov eax, 0
        mov esp, ebp
        pop ebp
        ret

section '.idata' import data readable writeable 

library kernel32,'kernel32.dll'

import kernel32,\ 
  ExitProcess,       'ExitProcess'    

And it doesn't compile, saying push ebp is an illegal instruction.
Post 12 Mar 2019, 10:00
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 12 Mar 2019, 12:50
For 64-bit code there is no encoding for 32-bit pushes. But since your code is 32-bit then using "format pe console" should solve your problem.
Post 12 Mar 2019, 12:50
View user's profile Send private message Visit poster's website Reply with quote
Marut



Joined: 18 Jun 2017
Posts: 12
Location: Veneto, Italy
Marut 12 Mar 2019, 14:09
I.E. you should use PE instead of PE64, and win32a.inc instead of win64a.inc
Or, you could compile a 64-bit binary and have:
Code:
format PE64 console 
entry main 
include 'win64a.inc' 

section '.code' code readable executable 
main: 
  call karen
  mov eax, 2
  add eax, 3
  add eax, 4
          invoke        ExitProcess, rax
          
karen:
        push rbp
        mov rbp, rsp
        mov eax, 0
        mov rsp, rbp
        pop rbp
        ret

section '.idata' import data readable writeable 

library kernel32,'kernel32.dll'

import kernel32,\ 
  ExitProcess,       'ExitProcess'    
    
Post 12 Mar 2019, 14:09
View user's profile Send private message Reply with quote
mcmike



Joined: 11 Mar 2019
Posts: 3
Location: Lviv, Ukraine
mcmike 12 Mar 2019, 18:43
Thank you for the answer! It seems so obvious now when I know what is up. What is better to use on modern windows, 64 or 32 bit? Obviously 32 bit allows for backwards compatibility, but this isn't really my priority in a learning environment and it doesn't seem complicated to change.
Post 12 Mar 2019, 18:43
View user's profile Send private message Send e-mail Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 12 Mar 2019, 23:19
I’d say 64-bit programming is not as assembly-programmer-friendly as 32-bit is due to the calling conventions used by the OS that seem to be more compiler-oriented than human-oriented. So, if WinAPI is also a subject to learn at the same time, I’d still suggest using 32 bits, at least for a while.
Post 12 Mar 2019, 23:19
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 13 Mar 2019, 17:03
mcmike wrote:
Thank you for the answer! It seems so obvious now when I know what is up. What is better to use on modern windows, 64 or 32 bit? Obviously 32 bit allows for backwards compatibility, but this isn't really my priority in a learning environment and it doesn't seem complicated to change.


learning the environment is important, and its kinda complicated.

you should learn the architecture you want to program for i.e. x86 or x86_64, as well as the operating system you are programming in.

_________________
Asm For Wise Humans
Post 13 Mar 2019, 17:03
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.