flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > DOS > Is there a smaller Hello World?

Author
Thread Post new topic Reply to topic
l4m2



Joined: 15 Jan 2015
Posts: 588
Is there a smaller Hello World?
Rule:
It should be a .exe file, not a .cmd or a .com
No arguments

Code:
;I wrote it in hex code
00000000   4D 5A 25 00 01 00 89 E2  00 00 B4 09 CD 21 B4 FD   MZ%
00000010   18 01 CD 20 06 01 F0 FF  48 65 6C 6C 6F 20 57 6F     Hello Wo
00000020   72 6C 64 21 24                                     rld!$

Post 07 Jul 2016, 12:36
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2122
Location: Usono (aka, USA)
I'm not sure why you demand it has to be an .EXE. The whole point of .EXE is to support relocations, multiple segments, i.e. greater than 64 kb .COMs.

You've probably got your output as small as it can go, but even on FAT12 floppy disks, the cluster size is 512 bytes, so it's still going to waste that much, at minimum. So it may not really be worth saving a few bytes when half a kb is already being used.
Post 07 Jul 2016, 23:57
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
> No arguments
> I wrote it in hex code

Please release source code.

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 08 Jul 2016, 08:31
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15

DOS386 wrote:
Please release source code.

Yeah!
Post 08 Jul 2016, 11:36
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15
Source-code for the Hexa version
Here is what I could understand from the hexa version of the 37-byte "hello" program.
I Compiled it with the latest release of FASM (1.71.54).
The resulting raw file is exactly the same as the original (every byte).
Obviously, it runs perfectly, as the original one. Smile

No need to rename the resulting BIN file to EXE anymore (now using the "format" directive so that it generates file with EXE extension).


9/July: fixed comment about word 0fdb4h: it's the SS, not Checksum, as pointed out by l4m2.


Code:
 format binary as 'exe'         ; no need to rename ".bin" to ".exe" after compiled.

               org  0           ; this stuff should start at offset 0 (zero).

 ExeSignature  db   4dh5ah    ; exe stub: file signature 'MZ' at offset 0.
 LastPageSize  dw   25h         ; exe stub: number of bytes in the last page (in our case, the file size).
 PageCount     dw   1           ; exe stub: number of whole/partial pages.

 Entry_Point:                   ; at offset 106h
     mov  dx,  sp               ; [#1] same as "mov dx, msg", since sp=118h, as set in the header (see #2).

 HeaderSize    dw   0           ; exe stub: header size (in paragraphs).
                                ; NOTE: this runs as "add [bx+si],al" but it doesn't hurt.

     mov  ah,  9                ; dos service to display a $-terminated text.
     int  21h                   ; displays the msg. the addr of the msg was set at the code entry point (see #1).

 SS_InitValue  dw   0fdb4h      ; exe stub: Initial SS relative to start of executable.
                                ; NOTE: this runs as "mov ah,fd" but it doesn't hurt.

 SP_InitValue  dw   118h        ; [#2] exe stub: initial value for the SP register (see #1).
                                ; NOTE: this runs as "sbb [bx+di],al" but it doesn't hurt.

     int  20h                   ; COM-style wayout.

 IP_InitValue  dw   106h        ; exe stub: initial value for the IP register (program entry point).
 CS_InitValue  dw   0fff0h      ; exe stub: relocatable segment address for CS.

          msg  db   'Hello World!$'


Running the BAT, which waits for a keypress:
Image


Description:
Download
Filename: hello37.zip
Filesize: 1.28 KB
Downloaded: 53 Time(s)



Last edited by patulinu on 09 Jul 2016, 12:14; edited 1 time in total
Post 08 Jul 2016, 15:01
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 588
Re: Source-code for the Hexa version

patulinu wrote:
[color=darkblue]Here is what I could understand from the hexa version of the 37-byte "hello" program.
I Compiled it with the latest release of FASM (1.71.54).
The resulti
CheckSum dw 0fdb4h ; exe stub: when added to the sum of all other words in the file, result = zero.
; NOTE: this runs as "mov ah,fd" but it doesn']


fdb4 should be ss init value
Post 09 Jul 2016, 03:21
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15
Re: Source-code for the Hexa version

l4m2 wrote:
fdb4 should be ss init value


Yes, thank you.
Fixed.
Post 09 Jul 2016, 12:16
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 588
This code works both as .exe and .com:

Code:
 format binary as 'exe'         ; no need to rename ".bin" to ".exe" after compiled.
               org  100h        ; this stuff should start at offset 100h.
 ExeSignature  db   4dh5ah    ; 00: 4D 5A      exe stub: file signature 'MZ'
 Entry_Point:  mov  dx,  msg    ; 02: BA 18
                                ; 04: 01         exe stub: number of whole/partial pages.
               db   0,   0ffh   ; 05:    00
                                ; 06: FF
               mov  ax,  0      ; 07:    B8
                                ; 08: 00 00      header size
               mov  ah,  9      ; 0A: B4 09
               int  21h         ; 0C: CD 21
 SS_InitValuemov  ah,  0fdh   ; 0E: B4 FD
 SP_InitValueint  20h         ; 10: CD 20
               nop              ; 12: 90 90
               nop
 IP_InitValuedw   Entry_Point ; 14: 02 01      exe stub: initial value for the IP register (program entry point).
 CS_InitValuedw   0fff0h      ; 16: F0 FF      exe stub: relocatable segment address for CS.
          msg  db   'Hello World!$'  


besides, there is space in the code (the two nops won't execute).
Post 09 Jul 2016, 16:20
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15

l4m2 wrote:
This code works both as .exe and .com:

Code:
 format binary as 'exe'         ; no need to rename ".bin" to ".exe" after compiled.
               org  100h        ; this stuff should start at offset 100h.
 ExeSignature  db   4dh5ah    ; 00: 4D 5A      exe stub: file signature 'MZ'
 Entry_Point:  mov  dx,  msg    ; 02: BA 18
                                ; 04: 01         exe stub: number of whole/partial pages.
               db   0,   0ffh   ; 05:    00
                                ; 06: FF
               mov  ax,  0      ; 07:    B8
                                ; 08: 00 00      header size
               mov  ah,  9      ; 0A: B4 09
               int  21h         ; 0C: CD 21
 SS_InitValuemov  ah,  0fdh   ; 0E: B4 FD
 SP_InitValueint  20h         ; 10: CD 20
               nop              ; 12: 90 90
               nop
 IP_InitValuedw   Entry_Point ; 14: 02 01      exe stub: initial value for the IP register (program entry point).
 CS_InitValuedw   0fff0h      ; 16: F0 FF      exe stub: relocatable segment address for CS.
          msg  db   'Hello World!$'  


besides, there is space in the code (the two nops won't execute).


I didn't try to make your code smaller. Just created the source code that generates your original binaries.
It seems the only way to make it smaller is by changing the message itself. Maybe to just: "Hi!". Very Happy
Post 09 Jul 2016, 16:43
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 588
This code runs perfectly on dosbox debug but not directly run:

Code:
 format binary as 'exe'
        org  $0100
        db   'MZ'
        mov  dx,  msg-2
        dw   $FF00
        push dx
        dw   $0000
        pop  bx
        sub  [bx],word $FFF0-'He'
        mov  ah,  $09
        int  $21
        ;ret
        db   $C3
        dd   $FFF0'0102
msg:    db   'llo World!$'

Post 13 Jul 2016, 03:29
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15

l4m2 wrote:
This code runs perfectly on dosbox debug but not directly run:

Code:
format binary as 'exe'
        org  $0100
        db   'MZ'
        .......
msg:    db   'llo World!$'



If it doesn't run, it doesn't count. Very Happy
Post 13 Jul 2016, 13:03
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 588

Quote:

If it doesn't run, it doesn't count.


So is there a way to make it runable?
Post 13 Jul 2016, 13:11
View user's profile Send private message Reply with quote
patulinu



Joined: 21 Jun 2016
Posts: 15

l4m2 wrote:

Quote:

If it doesn't run, it doesn't count.


So is there a way to make it runable?


I believe your first version is the smallest possible for a EXE/DOS/16-bit program, because all those header values left alone are really required by the OS so that the program can be load and run.

You may even rearrange things as you did in the latest versions, but all you will be able to accomplish is save 1 or 2 bytes in a raw (but not enough to move the "hello" message to a lower addr and get rid of the tail).

Due to the need of keeping those minimum header fields, the only safe way to make your program smaller is by turning the whole "hello" message into a simple "Hi." message. I can't see any other option for a true EXE program, but I'm still a enthusiast.
Post 13 Jul 2016, 16:25
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 588

Code:
 format binary as 'exe'
        org  $0100
        db   'MZ'                  ;00 'MZ'
        mov  bx,  msg-2            ;02
                                   ;04 word 1
        dw   $ED00                 ;05
        mov  ax,  $0000            ;07
                                   ;08 word 0
        mov  [bx],word 'He'        ;0A
        mov  dx,  bx               ;0E SS
        mov  ah,  $09              ;10 SP
        int  $21                   ;12
        ;ret
        dw   $C392                 ;14 IP
        dw   -$C39                 ;16 CS
msg:    db   'llo World!$'

Prints and halt in debug; prints and not halt when directly run
Post 14 Jul 2016, 04:44
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.