flat assembler
Message board for the users of flat assembler.

Index > OS Construction > exe in pm

Author
Thread Post new topic Reply to topic
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 13 Dec 2008, 06:13
align 8
exefile: file 'test.exe'
rb 8

Well first off I am not loading from a disk.. see above
compile the exe directly into the program.. 1st would this work?

Need info on how to execute it in PM..
So far its been crashing - hard to tell if its the load exe funtion or the
app... Thats why I am loading directly to get it working and then from a disk.. size of header /code start of exe

read the 1st 2 bytes for MZ cmp then .....

Basicly adding my OS ints and graphics funtions to the nasm obj and then using gcc to build c apps... in other words a GUI C lib..

all for testing..

Thanks - in advance for any help!
Post 13 Dec 2008, 06:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20343
Location: In your JS exploiting you and your system
revolution 13 Dec 2008, 06:24
You can't simply load an exe into memory and start executing. Each section needs to be properly located into memory. The Imports table needs to be properly linked and bound to the DLLs. If it has a TLS defined then you need to initialise that. Perhaps some other things to initialise also before you can jump/call to the code start offset. Basically you need to do all the things that the Windows exe loader would do.
Post 13 Dec 2008, 06:24
View user's profile Send private message Visit poster's website Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 13 Dec 2008, 17:21
I think your looking at what I am trying to do wrong...

were the .exe is does not matter - if its on a disk or a mem location..
it should be able to execute from either.. its just in mem not on a disk..

its just a matter of setting up the stack and executing it.. I have done this with code compiled into the program.. its the same concept..

The OS is not crashing because of the loading an .exe to the mem...

its crashing when I try to execute it...

Each section needs to be properly located into memory - right but this
can be done from it being stored in mem.. think of this mem location were its stored -like you would from a disk.. just no need to read the floppy...
Post 13 Dec 2008, 17:21
View user's profile Send private message Reply with quote
kas



Joined: 16 Jan 2008
Posts: 36
Location: UK
kas 13 Dec 2008, 17:31
Are you trying to use fasm to create a RAM disk?

Kas
Post 13 Dec 2008, 17:31
View user's profile Send private message Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 13 Dec 2008, 17:44
The best way to explain is :

The os is loaded from a floppy..
The test.exe would be a user app executed by the OS...

The user app is just loaded into the main kernel..instead of loaded from a disk
Post 13 Dec 2008, 17:44
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 13 Dec 2008, 18:16
Now you first problem is MZ are useally for realmode and even than you need to set stuff up, before running.
Example of loading a com or MZ (realmode)
Code:
;;;;;;;;;;;;;;;;;;;
;; Type checking ;;
;;;;;;;;;;;;;;;;;;;

        cli                             ; for stack adjustments
     mov     ax, ImageLoadSeg
    mov     es, ax
      cmp     word [es:0], 5A4Dh  ; "MZ" signature?
 je      RelocateEXE             ; yes, it's an EXE program

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Setup and Run COM program ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        mov     ax, es
      sub     ax, 10h                 ; "org 100h" stuff Smile
     mov     es, ax
      mov     ds, ax
      mov     ss, ax
      xor     sp, sp
      push    es
  push    word 100h
   jmp     Run

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Relocate, setup and run EXE program ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

RelocateEXE:
      mov     ds, ax
      add     ax, [ds:08h]                ; ax = image base
   mov     cx, [ds:06h]                ; cx = reloc items
  mov     bx, [ds:18h]                ; bx = reloc table pointer
  jcxz    RelocationDone
ReloCycle:
    mov     di, [ds:bx]         ; di = item ofs
     mov     dx, [ds:bx+2]               ; dx = item seg (rel)
       add     dx, ax                  ; dx = item seg (abs)
       push    ds
  mov     ds, dx                  ; ds = dx
   add     [ds:di], ax         ; fixup
     pop     ds
  add     bx, 4                   ; point to next entry
       loop    ReloCycle
RelocationDone:
    mov     bx, ax
      add     bx, [ds:0Eh]
    mov     ss, bx                  ; ss for EXE
        mov     sp, [ds:10h]                ; sp for EXE
        add     ax, [ds:16h]                ; cs
        push    ax
  push    word [ds:14h]               ; ip
Run:
    mov     dl, [cs:bsDriveNumber]      ; let program know boot drive
       mov     dh, 0xff                ; let DexOS know it booted from bootprog
    sti
 retf
    


But it would be best if you tell us what you want to do, eg: do you need to be able to compile it with a C compiler ?, so you need a certain file format ?.
Does it need to be reloctable ? etc.
Post 13 Dec 2008, 18:16
View user's profile Send private message Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 13 Dec 2008, 19:14
I am trying to:

align 8
exefile: file 'test.exe'
rb 8

lea ebx,[exefile]
call load_exe

load_exe:
loading data goes here.....
ret

I have a good idea on how to do it now... I will try later and if works will post how I did it!

Thanks again for the help...
Post 13 Dec 2008, 19:14
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 14 Dec 2008, 15:23
Unless you use a reloctable file format and fix the offset's, you will need to do something like this:
Remember EBX is the base address you need to add this to all vars
Example of needing base add
Code:
mov   eax,[TestVar+ebx]  ;TestVar is a var
call  [WriteHex32+ebx]   ;WriteHex32 is stored in a var
mov   esi,msgService2   ; Move offset to string
add   esi,ebx   ; We add the base address
    

Example of not needing base add
Code:
call  PrintTest  ; call proc
jmp next    ; jump to address
    
Post 14 Dec 2008, 15:23
View user's profile Send private message Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 14 Dec 2008, 17:41
Thanks - I think I am getting closer... or at leist its not crashing now... lol
or it may be working .. I just have nothing to base it on - it could be the C program still needs work or the loading...


in the loadexe I am adding the base to it...

I have been going over the isr int to make sure I did not miss anything and the nasm/gcc .. looks okay,, the program should fill the screen white .. so far its not doing that... There are some diff in nasm versus .. Fasm.. this could be an issue also like with the rw rb (are not in Nasm) ...

maybe I will try without the int. or in txt mode...
Post 14 Dec 2008, 17:41
View user's profile Send private message Reply with quote
dosin



Joined: 24 Aug 2007
Posts: 337
dosin 15 Dec 2008, 13:14
just if anyone was wondernig if

align 8
binfile: file 'test.bin' ;was .exe
rb 8

lea ebx,[binfile]
call run_bin_file

run_bin_file:
;execute the code here



works.. Yes - it does - you can store an excutable/binary file in mem and can run it this way!


I tried with a simpler format with fasm and it worked...
The prob with the gcc - is all the junk it adds to the exe... I may be stuck with compile a binary format - like how they make kernels... no libs freestanding...
but this would be okay if it works for what I am trying to do... or possible another format...
Post 15 Dec 2008, 13:14
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.