Sign in to follow this  
Kohan

[x86 ASM] .com to PE transition a bit rocky.

Recommended Posts

Hey guys. I'm using FASM (http://www.flatassembler.net/), making some x86 programs. Until just recently, I've only ever made .com files (raw executables). However, due to various limitations, I would like to begin assembling Portable Executables. However, things seem to instantly fail to work when doing this. I figure I've included everything necessary for a PE to work. At the beginning of the file, I've got format PE, I've got an entry Start (Start being a label), I've got a section '.code' code readable executable before all my code, and a section '.data' data readable writeable before all my dbs and dws, and I think all the other PE parameters are optional (like stack and heap). My code assembles fine, but hits a 'rand.exe has encountered a problem and blah blah blah' when I run it. So, is there a line I'm forgetting? Would you like my code posted? Thanks in advance.

Share this post


Link to post
Share on other sites
I've a couple of ideas as to what could be causing the problem. To narrow it down, it would help if you posted some code.

Share this post


Link to post
Share on other sites
Alrighty. Streamlined for all our sakes.
format PE console

entry Start

section '.code' code readable executable

; Bunch of labels with instructions that act as functions.
; General stuff, like printing characters, strings, integers,
; and generating random numbers (hence rand.exe).

section '.data' data readable writeable

seed dd 00000000000000000000000000000000b
db 0011101b ; Used for random number generation.
n dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ; Used with integer printing
newline db 0Dh,0Ah,'$' ; To make line breaks faster.


And that's it. As for the functions, I've got ScanChar (takes a keyboard input), PrintChar (shortcut for int 21h, ah=02), PrintString (same for int 21h, ah=09), PrintInt (big ole sucker that prints an integer in any base), SRand (takes an input for a random seed), TSRand (uses system time for seed), Rand (actually makes a number), Start (beginning of program), and Close (int 21h, ax=4C00h).

If you'd like to see their code (120 lines in total), ask again.

Share this post


Link to post
Share on other sites
Well, I don't really know about FASM, but a quick scan seems to indicate that you're trying to run DOS assembler (i.e. calling DOS interrupts) in Windows mode. The 'console' option probably makes a Windows console program not a DOS one. A COM file on the other hand, is always run as a DOS program. In Windows you need to call the Win32 API functions for the same purpose, or find a way to get FASM to produce DOS-format PE programs.

Share this post


Link to post
Share on other sites
format PE console
produces a 32-bit Windows executable. Given that you are calling DOS interrupts and (I assume) using 16-bit code, I think what you really want is to transition from a COM file to a 16-bit DOS EXE. For that, you need
format MZ

Share this post


Link to post
Share on other sites
Well, all those are valid points, and probably correct, but it seems my code has decided to asplode (http://www.uncyclopedia.org/wiki/A_Splode) in general; it no longer even works as a COM (even after removing the PE jargon). Ah well, I'll get back to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this