flat assembler
Message board for the users of flat assembler.
Index
> Windows > How to insert code in DOS stub of a PE? |
Author |
|
revolution 25 Jan 2023, 07:28
Stubs are only valid for MZ formats within PE files.
Stubs can be any length up to, IIRC, 64kb. |
|||
25 Jan 2023, 07:28 |
|
revolution 25 Jan 2023, 07:31
A stub:
Code: format MZ segment zz entry zz:0 stack 128 heap 0 push cs pop ds mov ah,9 mov dx,message int 21h mov ax,4cffh int 21h message: db 'Windows 95 or higher required for this program!',0dh,0ah,'$' db 256+128 dup 0xaa ; random data to make the stub bigger for testing Code: format PE GUI 4.0 on 'BigStub-Stub.exe' include 'win32ax.inc' .code start: invoke MessageBox,HWND_DESKTOP,"Hi! I'm the example program!","Win32 Assembly",MB_OK invoke ExitProcess,0 .end start |
|||
25 Jan 2023, 07:31 |
|
FlierMate11 25 Jan 2023, 07:48
revolution wrote: A stub: Aww... I like this, tested OK on my PC. I didn't know can insert DOS stub into PE output like that. Thanks. |
|||
25 Jan 2023, 07:48 |
|
revolution 25 Jan 2023, 08:23
The stub can be any MZ format program. It doesn't have to be only a stub, it can be a full application.
|
|||
25 Jan 2023, 08:23 |
|
Tomasz Grysztar 25 Jan 2023, 12:17
revolution wrote: Stubs can be any length up to, IIRC, 64kb. Code: ; alteration in source\ide\fasmw\fasmw.asm: format PE GUI 4.0 large NX on '..\fasmd\fasmd.exe' |
|||
25 Jan 2023, 12:17 |
|
revolution 25 Jan 2023, 15:44
Tomasz Grysztar wrote: MZ files can be larger. You can, in fact, assemble FASMW.EXE on top of FASMD.EXE, and it works. I wonder if it is possible to have a DOS TUI, Windows GUI and a CMD CLI in a single exe. Windows is weird with how to detect and use the console vs a window. The requirement to mark the exe as either GUI or CLI forces an initial choice, and leaves the app with only inferior choices about how to deal with switching to another interface. Many apps don't even try and just make two exe files. |
|||
25 Jan 2023, 15:44 |
|
FlierMate11 06 Feb 2023, 09:52
I solved my original problem in Post #1:
Code: use16 align 16 SIZE_OF_STUB_HEADER := $ - Stub ; The code of a DOS program would go here. dos_stub: push cs pop ds mov dx, msg - dos_stub mov ah, 9 int 21h mov ax, 4c01h int 21h msg db "This program can only be run in Windows.",13,10,"$" SIZE_OF_STUB := $ - Stub use32 align 8 It compiles and runs nicely in DOSBox. Must use offset relative to the DOS stub. I was inspired with the solution after checking other Windows EXE in hexdump mode. |
|||
06 Feb 2023, 09:52 |
|
Tomasz Grysztar 06 Feb 2023, 10:40
FlierMate11 wrote: Must use offset relative to the DOS stub. Code: org $% You can also adjust various values, including the entry point, by tweaking MZ headers. See how fasm-compatible PE formatter for fasmg does it: Code: Stub: .signature dw "MZ" .bytes_in_last_page dw .LENGTH and 1FFh .number_of_pages dw (.LENGTH-1) shr 9 + 1 .number_of_relocations dw 0 .number_of_header_paragraphs dw .HEADER_LENGTH shr 4 .minimum_heap dw .STACK_LENGTH shr 4 .maximum_heap dw 0FFFFh .initial_ss dw 0 .initial_sp dw .LENGTH - .HEADER_LENGTH + .STACK_LENGTH .checksum dw 0 .initial_ip dw 0 .initial_cs dw 0 .relocations_offset dw 40h .overlay_number dw 0 rb 3Ch - $ .new_header_offset dd Header .HEADER_LENGTH = $ .STACK_LENGTH = 100h namespace Stub include 'cpu/8086.inc' start: push cs pop ds mov dx,message - start mov ah,9 int 21h mov ax,4C01h int 21h message db 'This program cannot be run in DOS mode.',0Dh,0Ah,24h end namespace align 16 .LENGTH = $ PS. This thread also reminded me of an inverse stub I made as a joke. |
|||
06 Feb 2023, 10:40 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.