Is it possible to program in binary?

Binary

Binary Problem Overview


The title really says it all. A friend of mine told me he knows someone who can program in binary. I've never heard of someone programming in binary and a few quick Google searches didn't return anything useful. So I figured I'd turn to the SO community. Does anyone have any info on programming in binary and if possible maybe a quick Hello World example. Thanks in advance.

Binary Solutions


Solution 1 - Binary

Of course. It's more commonly called machine code. It's basically assembly language without the mnemonic devices. Someone who knows assembly very well could program in machine code with additional effort, referring to opcode listings (e.g. x86) as needed.

Would I do it? No. Even assembly is only useful in rare circumstances, and there's no reason (beside demonstrating your skills) to reject the assembler's help.

Since you asked about hello world, you should check out this article. He shows how he wrote, then optimized, an x86 ELF program to output it. It was originally written in nasm then modified in a hex editor.

Solution 2 - Binary

It's very much possible to memorize machine code equivalent of assembly instructions. Actually, when writing code in assembly language, one often happens to see hex code through machine code monitors, disassemblers, assembly listings, etc. As a result, over time some instructions can be memorized in their hex form without any extra effort.

Apple II 6502 ROM Monitor

  • In the picture an 6502 ROM monitor is seen, where hex code and assembly mnemonics are shown side by side.

Second skill you're going to need is to transform hex code into binary which is quite easy with a trick I'll explain in a bit.

Think of the following instructions:

OPCODE       HEX
LDA #imm     0xA9 imm
STA adr      0x85 adr
STA (adr),Y  0x91 adr
LDY #imm     0xA0 imm

With above opcodes memorized only, we can write the following machine code using only pen and paper:

0xA9 0x00 
0x85 0x01 
0xA9 0x02
0x85 0x02
0xA0 0x00
0xA9 0x01 
0x91 0x01

Actually the above is the following assembly code in mnemonic form:

LDA #00
STA $01
LDA #02
STA $02
LDY #00
LDA #01
STA ($01), Y
  • The above code puts a white pixel at the top-left corner of screen in 6502asm.com assembler/emulator, go ahead and try it out!

Now the trick for converting hexadecimals into binary and vice versa is to work it out only for nibbles (4-bit values).

First, remember how to convert binary into decimal. Every time you see 1, multiply that by its binary power. E.g. 101 would be 4 + 0 + 1 = 5. It can be visualized like this:

1   1   1   1 --> binary points
|   |   |   |               
v   v   v   v               
8 + 4 + 2 + 1
|   |   |   +---> 2^0 * 1   Ex: 13 is 8 + 4 + 0 + 1
|   |   +-------> 2^1 * 1             1   1   0   1 -> 1101 (0xD)
|   +-----------> 2^2 * 1   Ex:  7 is 0 + 4 + 2 + 1
+---------------> 2^3 * 1             0   1   1   1 -> 0111 (0x7)

With this in mind, and well practiced, the following should be possible:

LDA #00     -> 0xA9 0x00 -> 1010 1001 0000 0000
STA $01     -> 0x85 0x01 -> 1000 0101 0000 0001
LDA #02     -> 0xA9 0x02 -> 1010 1001 0000 0010
STA $02     -> 0x85 0x02 -> 1000 0101 0000 0010
LDY #00     -> 0xA0 0x00 -> 1010 0000 0000 0000
LDA #01     -> 0xA9 0x01 -> 1010 1001 0000 0001
STA ($01),Y -> 0x91 0x01 -> 1001 0001 0000 0001

With some retro computing spirit, motivation and fun, we could actually have written the entire code in binary without writing down the intermediate steps.

On a related note, Paul Allen coded a boot loader for Altair 8800 with pen and paper on an airplane, and possibly had to translate it to binary also with pen and paper: https://www.youtube.com/watch?v=2wEyqJnhec8

Solution 3 - Binary

There isn't much call for it any more, but it has been done. There was a time when code could be entered into a system in binary from the front console. It was error prone.

I used to have a very short uudecoe program encoded in ASCII which could be prefixed to a UUEncoded file. The resulting file would be self-extracting and could be emailed around. I would expect the machine code was hand done. I can't find it, and don't have a use for it even if I could.

Solution 4 - Binary

Well of course you can write the binary for the machine code and then enter the machine code via your hex key pad into your computer. I have put together a computer based on the TMS1100.

A simple program to display 5 on the hex LED would be 0001000 0000101 0000001 written in binary converted to machine code that would be 8 5 1 . This program would then run and display 5 on the LED.

You could follow this procedure for far more complex programs using the TMS1100 and I guess programming in binary.

Actually, I think this is very satisfying and rewarding if you are interested in mathematics and programming.

Solution 5 - Binary

For the brave of heart: you can try getting a MikeOS floppy image and running the monitor.bin program. It allows you to enter hexadecimal opcodes by hand and execute them. For example (as stated on the docs), entering the following instructions: BE0790 E8FD6F C3 4D00$ will produce a single M on the screen. Hexadecimal code

Solution 6 - Binary

There are some esoteric programming languages. They are used as experiments, and are rather impractical, but one, called BrainF**k (yes, it is actually a real thing) uses eight different characters to modify byte values. Those kind of languages are about as close as you can get.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam PView Question on Stackoverflow
Solution 1 - BinaryMatthew FlaschenView Answer on Stackoverflow
Solution 2 - Binaryneuro_sysView Answer on Stackoverflow
Solution 3 - BinaryBillThorView Answer on Stackoverflow
Solution 4 - BinaryWinston WillowView Answer on Stackoverflow
Solution 5 - BinaryZebrastormView Answer on Stackoverflow
Solution 6 - BinaryAnonymousView Answer on Stackoverflow