flat assembler
Message board for the users of flat assembler.

Index > DOS > Smallest DOS hello world? ASCII CODE

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
Below works under XP. Please can I ask if the below works on your system? Many thanks Smile
Code:
;hello.asm -> hello.com, 21 bytes
format binary as 'com'
        mov     dx,.msg+100h                                            ;DS = PSP address = CS - 100h
       mov     ah,9
        int     21h
 ret
 .msg:       db      'Hello World!$'    

Code:
;hello.asm -> hello.exe, 47 bytes
format binary as 'exe'

IMAGE_DOS_HEADER:                                                       ;Only e_magic and e_lfanew are used by PE loader
DOS_STUB:                                                               ;DOS-STUB is a 16-bit program
 .e_magic      dw "MZ"                                                       ;Magic number (Mark Zbikowski)
 .e_cblp      dw (IMAGE_NT_HEADERS - IMAGE_DOS_HEADER) and 511        ;Bytes on last page
 .e_cp           dw (IMAGE_NT_HEADERS - IMAGE_DOS_HEADER + 511) shr 9    ;Number of 512 byte pages in file, including last page
 .e_crlc      dw 0x0000                                               ;Relocations
 .e_cparhdr     dw (DOS_STUB - IMAGE_DOS_HEADER) shr 4                  ;Size of header in 16 byte paragraphs, EXE start = this * 16
 .e_minalloc    dw 0x0010                                               ;Minimum paragraphs to allocate in addition to executable's size 
 .e_maxalloc      dw 0xFFFF                                               ;Maximum paragraphs to allocate in addition to executable's size 
 .e_ss            dw 0x0000                                               ;initial SS relative to start of executable
 .e_sp           dw .msg+100h                                            ;initial SP 
 .e_csum        dw 0x0000                                               ;checksum (one's complement of sum of all words in executable) 
 .e_ip              dw .stub                                                ;initial CS:IP relative to start of executable 
 .e_cs           dw 0x0000
; .e_lfarlc        dw 0x001C                                               ;offset within header of relocation table, 40h or greater for new-format (NE, LE, LX, W3, PE, etc.) executable 
; .e_ovno    dw 0x0000                                               ;overlay number (normally 0000h = main program) 
 .stub: mov     dx,sp                                                   ;DS = PSP address = CS - 100h
       mov     ah,9
        int     21h
 mov     ah,4Ch
      int     21h
 .msg:       db      'Hello World!$'

IMAGE_NT_HEADERS:    
Post 15 Nov 2009, 09:51
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4225
Location: 2018
edfed
yep, it is the smallest DOS hello world possible. juste you don't need ot add 100h to .msg label.
Post 15 Nov 2009, 11:23
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:

yep, it is the smallest DOS hello world possible. juste you don't need ot add 100h to .msg label.

Yes, although he needs to add "org 100h" after "format binary as 'com'" before removing the addition.
Post 15 Nov 2009, 18:05
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
There is a way to make it smaller, by using command line parameters, first leave out
Code:
.msg:  db      'Hello World!$'
    

instead of
Code:
mov     dx,.msg+100h     

use
Code:
mov     dx,81h    

Then when you run the program type
hello Hello World!$ <enter>

It should than print out 'Hello World!'
It should be smaller by a number of bytes Wink
Post 15 Nov 2009, 18:49
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4225
Location: 2018
edfed
speaking about command line, i meet a problem with it currentlly.
i test for empty params at 80h, if it is 0, then, there is nothing, then, the filename pointer will be default one. else, it will be set to 81h.
i should forget something because this part don't work at all.
Code:
.nameptr dd .filename
.filename db 'c:\file.ext',0
.check: Asm @f
@@:
        push eax ebx
        mov eax,[.nameptr]
        cmp byte[80h],0
        je @f
        mov eax,81h
@@:
        mov [.nameptr],eax
        mov bl,[eax]
        cmp bl,0
        je @f
        cmp bl,10
        je @f
        cmp bl,13
        je @f
        inc eax
        jmp @b
@@:
        mov byte[eax],0
        mov eax,[.nameptr]
        mov [.disp+txt.txt],eax
        pop ebx eax
        ret
    


[edit] problem solved Wink...

but how? only need a @@: label somewhere.
contest only for beginners.
Post 17 Nov 2009, 02:13
View user's profile Send private message Visit poster's website Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
^^ I'm not sure if that includes me or not. Confused

wht36 compiles hello.com to 21 bytes - no command line (works on VHP 32 Wink ).

Dex compiles hello.com to 8 bytes and uses command line (might want to use 82h to get rid of the whitespace).


Here's mine. The executable hello.com compiles to 2 bytes. Shocked
Code:
org 100h
jmp short 82h
    

And the command line to be used with it. Laughing
Code:
hello T]hffX-feP\h!$hldhorh Whlohel50p-m(P-@05N9P5y(P-4y-  -moP-9FPU\x(    

My command line is messy and could be done a lot nicer I think. Better cut & paste. Wink
Post 17 Nov 2009, 11:30
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4225
Location: 2018
edfed
DOS is really a stupid systeme.
why?
because parameters from the comand line cannot be used directlly to open file using handle.

why?
because of ' 'char at the begining of the string. isn't it dumb?
Post 17 Nov 2009, 17:58
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
edfed: just skip one char ahead, space gone, problem solved?

If you think that's the biggest reason for DOS being a stupid system, then oh boy Smile
Post 17 Nov 2009, 18:15
View user's profile Send private message Visit poster's website Reply with quote
Japheth



Joined: 26 Oct 2004
Posts: 151
Japheth
Alphonso wrote:

Code:
org 100h
jmp short 82h
    

And the command line to be used with it. Laughing
Code:
hello T]hffX-feP\h!$hldhorh Whlohel50p-m(P-@05N9P5y(P-4y-  -moP-9FPU\x(    

My command line is messy and could be done a lot nicer I think. Better cut & paste. Wink


It works. Very cool program! Congrats!
Post 17 Nov 2009, 18:55
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Cool code Alphonso, you win the smallest Dos 'hello world!' compo Smile .
Post 17 Nov 2009, 21:21
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
f0dder wrote:
If you think that's the biggest reason for DOS being a stupid system, then oh boy Smile


A poor carpenter blames his tools. (Then again, I whine all the time about Windows, so meh.)

And yes, kudos to Alphonso the cheate^H^H^H^H^H^H winner. Wink
Post 18 Nov 2009, 04:13
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 166
Location: Maryland, USA
rCX
Alphonso. You Win.
Shocked
Post 18 Nov 2009, 05:01
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4225
Location: 2018
edfed
f0dder wrote:

If you think that's the biggest reason for DOS being a stupid system, then oh boy Smile

yep, and better, i copy the parameters somewhere else while cleaning it.

and it works.

later i will post a sort of rawwrite.
but this one will be exclusivelly dos based for file loading, and will be able to copy to any BIOS drive, using a custom CHS/LBA localisation. win98 & dos compatible.

_________________
Smile fool iem.design
Post 18 Nov 2009, 13:32
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 688
Location: Adelaide
sinsi
Wasn't that how you did code obfuscation? I remember an entire .com program that was a text file - almost 1Kb. Nice!
Quote:
Below works under XP
Not under XPPro64 I bet...sorta sucks
Post 19 Nov 2009, 07:53
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
Yeah, I kinda remember something like that from long ago which is where the idea came from and using Dex's clever command line trick. I'm sure the ascii code was done a lot better than my simple implementation too. Very Happy
Post 25 Nov 2009, 12:58
View user's profile Send private message Reply with quote
espartano



Joined: 12 Dec 2009
Posts: 1
espartano
Alphonso wrote:
^^ I'm not sure if that includes me or not. Confused

wht36 compiles hello.com to 21 bytes - no command line (works on VHP 32 Wink ).

Dex compiles hello.com to 8 bytes and uses command line (might want to use 82h to get rid of the whitespace).


Here's mine. The executable hello.com compiles to 2 bytes. Shocked
Code:
org 100h
jmp short 82h
    

And the command line to be used with it. Laughing
Code:
hello T]hffX-feP\h!$hldhorh Whlohel50p-m(P-@05N9P5y(P-4y-  -moP-9FPU\x(    

My command line is messy and could be done a lot nicer I think. Better cut & paste. Wink


I'm quit a while programming in assembly but this is the trickiest I ever saw; congratulations.
Post 12 Dec 2009, 19:28
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7578
Location: Kraków, Poland
Tomasz Grysztar
sinsi wrote:
Wasn't that how you did code obfuscation? I remember an entire .com program that was a text file - almost 1Kb. Nice!

As I recall, the XPack for DOS was made this way. Almost 50 KB executable being a pure ASCII text, even formatted with lines of equal length.
Post 12 Dec 2009, 20:04
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
Alphonso wrote:
^^ I'm not sure if that includes me or not. Confused

wht36 compiles hello.com to 21 bytes - no command line (works on VHP 32 Wink ).

Dex compiles hello.com to 8 bytes and uses command line (might want to use 82h to get rid of the whitespace).


Here's mine. The executable hello.com compiles to 2 bytes. Shocked
Code:
org 100h
jmp short 82h
    

And the command line to be used with it. Laughing
Code:
hello T]hffX-feP\h!$hldhorh Whlohel50p-m(P-@05N9P5y(P-4y-  -moP-9FPU\x(    

My command line is messy and could be done a lot nicer I think. Better cut & paste. Wink


I saw a list somewhere of all the instructions you could make with just ASCII, I'll have to find it. I'd like to see if it could be made any smaller.

EDIT: Ugh, this is gonna take forever. My backup discs have like 20,000+ files I've archived over the years. I have a habit of downloading and storing everything I see.....
And Google searches aren't bringing anything up either.

EDIT:
Here's the list.

I don't know what the "Eb"s and the "Gb"s and stuff mean though.
Code:
20             AND Eb, Gb
21      !      AND Ev,Gv
22      "      AND Gb, Eb
23      #      AND Gv, Ev
24      $      AND AL, Ib
25      %      AND eAX, Iv
26      &      ES:
27      '      DAA
28      (      SUB Eb, Gb
29      )      SUB Ev, Gv
2A      *      SUB Gb, Eb
2B      +      SUB Gv, Ev
2C      ,      SUB AL, Ib
2D      -      SUB eAX, Iv
2E      .      CS:
2F      /      DAS
30      0      XOR Eb, Gb
31      1      XOR Ev, Gv
32      2      XOR Gb, Eb
33      3      XOR Gv, Ev
34      4      XOR AL, Ib
35      5      XOR eAX, Iv
36      6      SS:
37      7      AAA
38      8      CMP Eb, Gb
39      9      CMP Ev, Gv
3A      :      CMP Gb, Eb
3B      ;      CMP Gv, Ev
3C      <      CMP AL, Ib
3D      =      CMP eAX, Iv
3E      >      DS:
3F      ?      AAS
40      @      INC eAX
41      A      INC eCX
42      B      INC eDX
43      C      INC eBX
44      D      INC eSP
45      E      INC eBP
46      F      INC eSI
47      G      INC eDI
48      H      DEC eAX
49      I      DEC eCX
4A      J      DEC eDX
4B      K      DEC eBX
4C      L      DEC eSP
4D      M      DEC sBP
4E      N      DEC eSI
4F      O      DEC eDI
50      P      PUSH eAX
51      Q      PUSH eCX
52      R      PUSH eDC
53      S      PUSH eBX
54      T      PUSH eSP
55      U      PUSH eBP
56      V      PUSH eSI
57      W      PUSH eDI
58      X      POP eAX
59      Y      POP eCX
5A      Z      POP eDX
5B      [      POP eBX
5C      \      POP eSP
5D      ]      POP eBP
5E      ^      POP eSI
5F      _      POP eDI
60      `      PUSHA
61      a      POPA
62      b      BOUND Gv, Ma
63      c      ARPL Ew, Rw
64      d      FS:
65      e      GS:
66      f      OPSIZE:
67      g      ADSIZE:
68      h      PUSH Iv
69      i      IMUL Gv, Ev, Iv
6A      j      PUSH Ib
6B      k      IMUL Gv, Ev, Ib
6C      l      INSB Yb, DX
6D      m      INSW/D Yv, DX
6E      n      OUTSB DX, Xb
6F      o      OUTSW/D DX, Xb
70      p      JO Jb
71      q      JNO Jb
72      r      JB Jb
73      s      JNB Jb
74      t      JZ Jb
75      u      JNZ Jb
76      v      JBE Jb
77      w      JNBE Jb
78      x      JS Jb
79      y      JNS Jb
7A      z      JP Jb
7B      {      JNP Jb
7C      |      JL Jb
7D      }      JNL Jb
7E      ~      JLE Jb
    


EDIT: I manually disassembled your code using that list, it worked very good. It comes out with the exact same output through FASM.(I know the org and js are wrong, but it still outputs the same thing)
Code:
use16
org 80h

push sp
pop bp
push 6666h
pop ax
sub ax, 6566h
push ax
pop sp
push 2421h
push 646ch
push 726fh
push 5720h
push 6f6ch
push 6c65h
xor ax, 7030h
sub ax, 286dh
push ax
sub ax, 3040h
xor ax, 394eh
push ax
xor ax, 2879h
push ax
sub ax, 7934h
sub ax, 2020h
sub ax, 6f6dh
push ax
sub ax, 4639h
push ax
push bp
pop sp
js -87
    

_________________
----> * <---- My star, won HERE
Post 12 Dec 2009, 20:05
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 294
Alphonso
windwakr wrote:
I have a habit of downloading and storing everything I see.....
Yep, I have that habit to. Sometimes I wonder if there's simply too much information on the Net.

windwakr wrote:
I don't know what the "Eb"s and the "Gb"s and stuff mean though.
I'm not sure either but at a guess I think

b=byte
v=word or dword
G=GP Register
I=Immediate
E=Everything, could be a register, or word or dword

windwakr wrote:
I manually disassembled your code using that list
That's a handy list to have, I used the One-byte Opcode Map, table A-2 in Intels Software Developer’s Manual 2B. The code is a bit 'rough and ready'.
Code:
format binary as 'txt'
org  82h

push sp
pop  bp
push 6666h
pop  ax
sub  ax,6666h - 100h   ;offset to end
push ax                ;ax =0100h
pop  sp                ;Stack pointer at 0100
push 2421h             ;start from the end of code and work back to start,
push 646ch             ;begining with 'hello world!'
push 726fh
push 5720h
push 6f6ch
push 6c65h
xor  ax,7030h          ;7130
sub  ax,286dh          ;48c3
push ax                ;48c3 ->
sub  ax,3040h          ;1883
xor  ax,394eh          ;21cd
push ax                ;21cd ->
xor  ax,2879h          ;09b4
push ax                ;09b4 ->
sub  ax,7934h          ;9080
sub  ax,2020h          ;7060
sub  ax,6f6dh          ;00f3
push ax                ;00f3 -> (message pointer)
sub  ax,4639h          ;baba
push ax                ;baba ->

push bp
pop  sp                ;restore original stack pointer
js   0ebh              ;jump to start of code (sign set from previous subtract)          


Which as you know basically pushes the following code from end to start (00FF to 00EB)

Code:
mov          dx,message
mov          ah,09
int          21
ret
message      db 'Hello World!$'     


We could just have 'Hello World!$' in the command line and point to it to make the command line smaller rather than pushing it but IMO somehow it would seem less 'magical'.

I can't help thinking I read something on comp.lang.asm.x86 along time ago that using ascii had been taken one or two steps further than the simple method above.
Post 13 Dec 2009, 08:48
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Alphonso wrote:
I can't help thinking I read something on comp.lang.asm.x86 along time ago that using ascii had been taken one or two steps further than the simple method above.
I think Terje Mathisen wrote something to automate the task? At any rate, malware authors have automated the process for getting shellcode past input validators.

_________________
Image - carpe noctem
Post 13 Dec 2009, 08:51
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2020, Tomasz Grysztar.

Powered by rwasa.