View Single Post
Old 01-02-2009, 06:19 AM   #21 (permalink)
0x123
 
Newb Techie

Join Date: Dec 2008

Posts: 3

0x123 is on a distinguished road

Cool Re: Writing a program in binary

Quote:
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.
Quote:
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.

Then copy the hex code exactly as shown into editor.

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

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

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:

If your feeling dont be your not a super :nerd:!

Last edited by 0x123; 01-02-2009 at 12:16 PM. Reason: typo.
0x123 is offline