Awesome. My wife and kids asked me if you can write computer programs using just 1s and 0s and I told them that assembly was the lowest you could use, apparently mistakenly. Gonna have to try this out!
Of course you can, though on a modern system it's no longer possible to get code into the system by toggling electrical switches, which was the way computers were bootstrapped way back in the day.
But hand assembly isn't so far away from our experience. Woz famously wrote the original Apple ][ ROM by hand, including the BASIC interpreter (itself written in a "SWEET16" bytecode for which he hand-assembled the interpreter). I don't know what the tooling looked like for that; surely he wasn't hand-toggling this thing in every time. Presumably the monitor and casette interface were written first...
> Of course you can, though on a modern system it's no longer possible to get code into the system by toggling electrical switches, which was the way computers were bootstrapped way back in the day.
The easiest way to do this is to actually build a COM file and run it in a 32-bit version of Windows. COM has no header information at all and just starts running the x86 instructions after loading the file to 0100h.
Here's the DOS COM version. Copy this into HELLO.COM with a hex editor of your choice. It will run in dosbox (that's what I tested on) and should run in Windows COMMAND.COM on a 32-bit system.
b409 MOV AH, 09h ; OUTPUT string
ba0d01 MOV DX, 010Dh ; address of output buffer (remember we're loaded into 0100h)
cd21 INT 21h
b44c MOV AH, 4Ch ; TERMINATE with return code
b05d MOV AL, 5Dh ; return code
cd21 INT 21h
48454c4c4f20574f524c442124 "HELLO WORLD$" ; DOS strings are $ terminated
Back in the day we used to do this with the DEBUG.COM command. It's actually not that bad to do this. The thing that sucks is hand assembling and going back to fixup your addresses. It starts to get hairy if you need to flip around the CS and DS registers to shift in different segments.
As an exercise for the reader use the RET instruction instead of doing the return code stuff. If you want to really get into you you can look up how to read from the console and then display that string back to the user.
Wasn't there a Phrack Magazine article on typing in software from memory using debug.com? COM-files are funny. I seem to recall "echo d > restart.com" (or possibly "D") would give you a quick way to reboot a 3.11 machine (probably due to some crash or other, I never investigated why it worked - but a couple of other random characters would usually end up with a freeze...
You can cheat a little bit and use RET rather than INT 21h to save a few bytes.
The way this works is pretty clever - the COM loader places an INT 20h instruction (the old-style terminate function) at offset 0 in the PSP (the 100h-byte-long structure loaded right before the contents of the COM file). The loader also sets up the word above the initial stack pointer to 0 so that a RET will return to address 0 and execute the INT 20h.
I made this in 2001 and used it once in a job interview, I asked for access to a computer running XP, opened cmd.exe and debug.exe, entered the asm code (rdtsc is not supported by debug.exe but i remembered the 0f31 opcode) and blew someones mind.
Man, that's an extremely interesting and thorough exploration of the nitty-gritty of how programs work, and I found it totally satisfying to read. I've had many little questions I barely knew I had answered by reading this. Thanks very much for making it!
It's the HWLENADDR you're talking about? It's placed as the last byte in (not in: after) the string. Down in the final product the edx part ends up BA B1 80 04 08 (0x080480B1). It's a pointer to that '14' stored 13 bytes past the start of the string ("hello, world\n" is 13 bytes). The ecx points to (0x..A4), exactly 13 less than B1.
edit: You're probably thinking it should be placed as an immediate value. It's hard to say exactly how many bytes it'd take up that way, so using an address to an immediate value is a bit simpler.
I see where I was off by one byte (14 instead of 13) but that still doesn't explain things. The code doesn't load edx with 13 but instead loads it with 0x080480B1. If you wanted to fetch a byte from that address and load it into edx you'd need to use something like "movzx edx, byte ptr [0x080480B1]" but the code on the page just does "mov edx, 0x080480B1". In fact when you run this the code actually prints out not just "Hello, World\n" but also prints out the 0x0D length byte and then the entire rest of the page (all 0x00). Of course these aren't visible in a terminal.
I'm guessing the kernel tries to keep printing but it can't since the next page isn't mapped so it just gives up.
I agree. This is definitely a bug. The correct instruction is 8b 15 B1800408 or a MOV EDX, [0x080480B1]. I hope I got my register number correct on that.
The movzx/movsx is only needed when you're doing something like movzx EAX, BX. That way you're sure the CPU does what you expect when moving from a smaller register to a larger one. Man do I hate x86.
Yeah, you're right. The syscall does ask for a size_t. It now looks correct. The whole length-after-the-string part is edited out, and the value is indeed placed as immediate (as the syscall wants). Now the binary ends with "hello, world\n" and 14 is inserted directly (b9 a4 80 04 08, ba 0d 00 00 00).
Author here. And yes, that's a bug. I recently restored most of the pages on my website, and didn't check whether each page was the most recent version. When I first wrote the HOWTO, I for some reason was under the impression that the 'write' system call required the address of the length of the string to be loaded in EDX, which is obviously a bit silly. You're right that EDX should just hold the value 13. I'll change this as soon as I get the time.
26 comments
[ 2.9 ms ] story [ 68.5 ms ] threadBut hand assembly isn't so far away from our experience. Woz famously wrote the original Apple ][ ROM by hand, including the BASIC interpreter (itself written in a "SWEET16" bytecode for which he hand-assembled the interpreter). I don't know what the tooling looked like for that; surely he wasn't hand-toggling this thing in every time. Presumably the monitor and casette interface were written first...
Challenge accepted.
As an exercise for the reader use the RET instruction instead of doing the return code stuff. If you want to really get into you you can look up how to read from the console and then display that string back to the user.
The way this works is pretty clever - the COM loader places an INT 20h instruction (the old-style terminate function) at offset 0 in the PSP (the 100h-byte-long structure loaded right before the contents of the COM file). The loader also sets up the word above the initial stack pointer to 0 so that a RET will return to address 0 and execute the INT 20h.
All the same, very cool, this is a great experiment for anyone to work on :)
True, I needed 62 bytes to get the greeting in there.
edit: You're probably thinking it should be placed as an immediate value. It's hard to say exactly how many bytes it'd take up that way, so using an address to an immediate value is a bit simpler.
The movzx/movsx is only needed when you're doing something like movzx EAX, BX. That way you're sure the CPU does what you expect when moving from a smaller register to a larger one. Man do I hate x86.