Writing a program in binary

Status
Not open for further replies.
Do you even have any idea what you are talking about?
I'm sorry I left out an explanation. I know it's only 25 bytes long, but machine code is very efficient. It is a valid Ms-Dos 16 bit program that will run from command prompt.
He does somewhat. If you write and save the hex file properly it can run in some compilers. The command prompt part won't do anything. The binary will do absolutely nothing too.
Thank you for not flaming me right away. But you made some mistakes. It dose not need to "run in [a] compiler" it is a program not a C++ source file. If saved properly it will run. Your rite about the binary its useless. It's there to show Aetherh4cker what the program looks like in binary.

It is not impossible to program in machine code. Assembly is basically short
hand for machine code. There's an opcode (operation code) for almost all assembly instructions i.e. JMP = EBh = 11101011b.
Here's the most basic asm. program that can output hello world on the console.
Code:
#MAKE_COM# 
;tells the assembler to make a com file.

ORG  100H
;tells the assembler the program will be loaded into ram at 100h.
;this way the assembler knows the address of the first command so it can find
;the addresses of other commands when doing a non-relative jump. 

JMP PRINT
;executing a data sting, 'hello world$', would cause the program to crash.
;so we jump to PRINT: to skip it.

msg DB 'Hello world$'
;tells the assembler to fill the next 12 bytes of ram with the string 'hello world$'

PRINT:  LEA DX, msg
;The 'PRINT:' is not a command its just telling the assembler where I wanted to jump to.
;This tells the assembler to store the location of the first byte of the string 'msg' we defined earlier into register DX.
;There is no opcode for LEA. LEA is an command used in assembly to save the programmer a lot of time.
;I defined 'msg' earlier.
;When the program is assembled the assembler will know the actual memory location of the start of 'msg',
;and use the MOV command to move it into register DX.

MOV AH, 9
;This command stores 9 in register AH.
INT 21h
;this calls interrupt 21.
;Interrupt 21 is a Dos interrupt.
;Dos checks what we stored in register AH to know what we want it to do.
;Command 9 tells the Dos to read from the memory starting at the address stored in register DX.
;And output it to the console until it hits a $.
;if I had left the $ off hello world you would see a scary matrix looking effect of the contents of all of your ram being dumped out on the screen.
;after Dos is done its job your program begins executing at where it left off.
 
MOV AH, 4Ch
;This stores 4Ch into Register AH.

INT 21h
;This time its command 4Ch. This tells Dos to quit your program and return to Command interpreter. (command.com)

Here is the program turned into machine code.  

0100h  EBh ; JMP     // JMP is a relative jump command you tell it to jump X many lines.
0101h  0Ch ; PRINT   // 0Ch = 12d so it will jump 12 lines.
0102h  48h ; 'H'     // 1
0103h  65h ; 'e'     // 2
0104h  6Ch ; 'l'     // 3
0105h  6Ch ; 'l'     // 4
0106h  6Fh ; 'o'     // 5
0109h  20h ; ' '     // 6
010Ah  77h ; 'w'     // 7
010Bh  6fh ; 'o'     // 8
010Ch  72h ; 'r'     // 9
010Dh  6ch ; 'l'     // 10
010Eh  64h ; 'd'     // 11
010Fh  24h ; '$'     // 12
0110h  BAh ; MOV DX  //There is a MOV opcode for every register.
0111h  02h ; 0102h   //register DX is 16 bits so it takes 2 bytes to fill the register with 0102h.
0112h  01h ;
0113h  B4h ; MOV AH  //Register AH is 1 byte so there is only bye needed after this command.
0114h  09h ; 09h
0115h  CDh ; INT     //This is the command for Interrupt.
0116h  21h ; 21h     //Dos will read DX, 0102, to know where the string it needs to output is.
0117h  B4h ; MOV AH
0118h  4Ch ; 4Ch
0119h  CDh ; INT
011ah  21h ; 21h
In your favorite hex editor make a new file.
newhexzf9.jpg

Then copy the hex code exactly as shown into editor.
hexdoneii7.jpg

Then select save as and save it as 'hello.com'.
saveasxr7.jpg

Finally open a command prompt, Cd to where you saved it, and type 'hello.com'
helloea3.jpg

Need anymore proof that its possible?
If you think this is hard try programming in binary with actual switches like on the altair 8080. Nothing but toggles and lights. :nerd:

I didn't mind you doubting me as I was a first time poster. And how often do you see a 25 byte program?

Wait heres a 2 byte program!

Code:
START JMP START:

0100h EBh
0101h FEh

Hehe, if you run that one it just loops forever making your processor run at 100% :laughing:
2bytes99cp1.jpg

If your feeling :confused: dont be :( your not a super :nerd:!
 
I don't believe in flaming people. Doesn't serve a purpose. What you did does work perfectly but only when using a hex editor. You need to be more clear as to what the steps are. If you use a hex capable compiler and write the hex code in only notepad it will work. That's what I was saying.
 
Wow that's cool. Not how I remember it either. Surely though, there has to be some level of interpretation by dos. How does it know to put "Hello world" on the screen for example as opposed to the speakers or serial port? Or is that what INT 21h does?
Baez I have not tried his program but I think the code on his first post is the raw binary for the program there is no need for an additional compiler, hex or otherwise. I think he wrote the assembly in a text editor and then used an assembler to "compile" it into just 25 bytes of machine code.
 
Yes the hex is machine code no need to compile it. If you know enough about asm. you can hand compile it but you need to figure out the memory locations of all the commands before you can code the jumps. If I were to want to add even one letter to the string it would mess it up. I.e. If I changed hello world to hello mark I would have to change 0101h to 0bh because you only have to skip 11 lines. when you use assembly the assembler finds the location of PRINT: and will adjust the code for you. Asm is not too hard to learn. I just started a class in computer science in high school. I find it easier to start programming in dos because you have full control of the system. In the class I'm taking, you learn about how the computer works before you learn how to make it work. I got a book on asm, and I just started reading about the dos graphic interrupts. I think I'll have something that looks like pong in a week or two, hopefully.

Anyone want to nominate my 2 byte program for smallest useless program ever? I think I may have broke the world record :laughing:

P.S. No I didn't use an assembler for that I used a table of common asm. commands and there hex equivalents. Remember there there's for the most part a 1 to 1 relationship between hex opcodes and asm. The only ones that have no opcodes are the commands that handle memory locations for you so when you do a jump or load a value from memory you can use a mnemonic instead of laying out every command and byte of data and figuring out there memory locations.

P.S.S. As for your question about interrupts. Int 21 is like a subroutine. its another program wrote in assembly loaded by dos. You don't need interrupts to write programs but it would be like writing hello world in C++ without #include <stdio.h> :p
 
the closest so far, is "brainfck" aka "brain****" (google it) don't bother 0's and 1' try brainfck, if you can do brainfck, then dear god you are a genius lol, by far the hardest "simple" programming language i've ever encountered
 
Status
Not open for further replies.
Back
Top Bottom