• Advertisement
Sign in to follow this  

Hello World in 28 bytes

This topic is 4805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm taking an Assembly language (x86) class this semester, and the professor said he wrote a Hello World program in 28 bytes. I'm wondering exactly how he got the exe so small. My programs are never smaller than 5kb!

Share this post


Link to post
Share on other sites
Advertisement
have you tried it writing in DOS assembler and as a .com ? this should do the trick... there are many tutorials on Hello World in 386 asm on the internet. try google.

regards,
m.

Share this post


Link to post
Share on other sites
Keep in mind once you compile and link your asm's the program gets byte-padded so it's appropriate to the OS. When he said he did it in so few bytes, I'm willing to bet that he meant his source code was only that big. Remember, before the byte-padding comes, each instruction maps to a machine instruction.*

* - don't burn me about different instruction sets. =) I know that some RISC psuedo ops and CISC instructions span out to two or more instructions when they get translated.

Share this post


Link to post
Share on other sites
It's really not that hard. COM programs don't require any header or padding.
Here's my version (21 bytes). Just paste this into a DOS prompt.

debug
a 100
mov ah,09
mov dx,109
int 21h
int 20h
db "Hello World$"

rcx
15
n hello.com
w
q

Share this post


Link to post
Share on other sites
Depends on how we're defining a 'program'. And did your professor say the program was actually in assembler? For instance, this batch file is even shorter than the (correct) 21-byte program above:

<<<
@ECHO Hello, world!
>>>


The above is only 19 bytes. You can even tell your professor you've "optimized" his version. [grin]

Share this post


Link to post
Share on other sites
In m4:

Hello, world.

Only 14 or 15 bytes (depending upon how long a line break is).

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
It's really not that hard. COM programs don't require any header or padding.
Here's my version (21 bytes). Just paste this into a DOS prompt.

debug
a 100
mov ah,09
mov dx,109
int 21h
int 20h
db "Hello World$"

rcx
15
n hello.com
w
q



I don't know ASM, but that was FUN! I wasn't aware that you just type it into the prompt!

Share this post


Link to post
Share on other sites
Would the instruction size of the computer make this easier? Ie, could you do it on an 8-bit machine?

edit: never mind, the example above does it in 21 bytes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
Quote:
Original post by doynax
It's really not that hard. COM programs don't require any header or padding.
Here's my version (21 bytes). Just paste this into a DOS prompt.

debug
a 100
mov ah,09
mov dx,109
int 21h
int 20h
db "Hello World$"

rcx
15
n hello.com
w
q



I don't know ASM, but that was FUN! I wasn't aware that you just type it into the prompt!


Agreed (Although I know asm a bit) that was new to me!

Cool stuff!!!

/MindWipe

Share this post


Link to post
Share on other sites
Nice! I didn't now that 'int 20h' is sufficient to exit.
My version would like like this:

debug
a 100
mov ah, 09
mov dx, 10b
int 21h
mov ah, 4c
int 21h
db "Hello World!$"

rcx
18
n hello.com
w
q
hello.com

Resulting in 24 bytes. I don't get how the prof could get 28 bytes...

Share this post


Link to post
Share on other sites
This is depressing ....

I used to think VB was a cool language ....
Then I though I should learn something C based, so I started C# ...
But the .NET framework was too slow, so now C++ .....
But now I have just found out how easy it is to code stuff like this right into the command prompt ! bah, it just keeps betting better :)

Share this post


Link to post
Share on other sites
20 bytes if you use ret instead of int 20h to exit ;-)

cheers
sam

Share this post


Link to post
Share on other sites
Quote:
Original post by xyuri
But now I have just found out how easy it is to code stuff like this right into the command prompt ! bah, it just keeps betting better :)


Note that Assembly sure isnt the fastest code in the world, and that some (or rather most) programmers litterally run if you ask them to code assembly. assembly is only good for routines that are called 100ths of time a second (not so much, but u get the point) you can make games with assembly however (Rollercoaster tycoon was writen in it) but i guess u rather learn C (or c++) to keep you from getting mad from assebly :)

Share this post


Link to post
Share on other sites
Quote:
Original post by xyuri
But now I have just found out how easy it is to code stuff like this right into the command prompt ! bah, it just keeps betting better :)


Note that Assembly sure isnt the fastest code in the world, and that some (or rather most) programmers litterally run if you ask them to code assembly. assembly is only good for routines that are called 100ths of time a second (not so much, but u get the point) you can make games with assembly however (Rollercoaster tycoon was writen in it) but i guess u rather learn C (or c++) to keep you from getting mad from assembly :)

also a prob: assembly is processor dependent... what u write fot a pentioum, isnt guaranteed to wotk on, lets say, athlon

Share this post


Link to post
Share on other sites
Just for the record for budding asm programmers: the asm code snippets above aren't really being entered directly at the command prompt. If you'll notice, the first line given is 'debug', which fires up the debugger. Subsequent asm code and other instructions are then passed to debug, which has a small capability for editing and writing assembly language programs. In these examples, debug is writing the program to the file hello.com, which is then being executed after the 'q' command exits debug. It's not the best way for creating extensive assembly, but it is sufficient for testing out small things such as this. For large asm apps you'll want an actual assembler which takes text files as input much like any other kind of compiler or language translator. These more powerful asm tools (such as MASM) include more powerful macros and methods for structuring applications and dealing with data.

Assembly! Yays!! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement