Thallios
Thallios

Reputation: 43

Resource for Binary Programming

I have searched all over the internet for programming in Binary and the only responses people seem willing to give are: "Why would you want to program in Binary?" "It's stupid, use assembly." "There's nothing you could possibly get out of it, there's no point in learning."

If you are one of these people, I appreciate your opinion, but it does not answer my question.

While I don't intend on actually trying to program in Binary my reasons for knowing are:

  1. I have an interest in knowing the Binary instructions behind the assembly commands.

  2. I am designing an experiment where random binary instructions are generated. I want them to be valid binary commands and I want to be able to interpret what those commands would do if run.

With that said, does anyone know any resource where one could learn Binary programming?

Upvotes: 4

Views: 239

Answers (5)

BlackBear
BlackBear

Reputation: 22979

Great answers. I'd just like to add a simple script for who's using linux which shows the binary representation of any instruction. You need a copy of NASM (but you can easily edit it so that it uses GAS or any other assembler) and objdump:

echo "$1" > testProgram.asm
nasm testProgram.asm -o testProgram.out -f elf -g
chmod 744 testProgram.out
objdump ./testProgram.out -d -M intel | grep ' 0:'
rm testProgram.out testProgram.asm

Here are some examples:

blackbear@blackbear-laptop:~$ ./viewOpcode.sh "add ecx, 5"
   0:   81 c1 05 00 00 00       add    ecx,0x5
blackbear@blackbear-laptop:~$ ./viewOpcode.sh "int 0x80"
   0:   cd 80                   int    0x80
blackbear@blackbear-laptop:~$ ./viewOpcode.sh "fmul st0, st1"
   0:   d8 c9                   fmul   st,st(1)
blackbear@blackbear-laptop:~$ ./viewOpcode.sh "andps xmm0, xmm1"
   0:   0f 54 c1                andps  xmm0,xmm1
blackbear@blackbear-laptop:~$ ./viewOpcode.sh "movntq [edi], mm0"
   0:   0f e7 07                movntq QWORD PTR [edi],mm0
blackbear@blackbear-laptop:~$ 

Upvotes: 1

shawty
shawty

Reputation: 5829

Ok, as an old hack who's been doing this since the days of binary :-)

Let me try to make this a bit more readable.

Binary as you describe it is whats known as machine code.

To take this a step further, CPU's are hard coded to respond to certain instructions, as an example (An please be aware I dont have any references to hand at the moment)

The value A9 in 6502 machine code means LDA (on other architectures this may mean something different)

so, if you where working on a 6502 CPU, then the sequence A920 would mean load the accumulator with the hex value 0f 0x20.

Depending on the CPU in question and how the instruction set is encoded, different bits in the number will cause the CPU (which is pure logic at heart) to perform different operations.

And, depending on the manufacturers specifications, different bit positions specify what each operation is.

For example, in an arm processor, bits 30 & 31 are the branch specifier, where as in a 6502 they are the zero page memory indicator.

Essentially, binary instructions are specific to the CPU in question and generally are not portable to another CPU (or for that matter any other intelligent silicon device) as a result porting and writing software is a generally very difficult task unless you have very deep knowledge of the chips your programming.

Unless your working for a company like intel, or a chip producer then there's really no need to know this stuff these days. However if your a speed junkie that like to hit the metal and squeeze every last drop of performance then you can still get the tools to do this kind of programming.

Upvotes: 1

JAW1025
JAW1025

Reputation: 676

Here's what I did to learn binary: 1. open command prompt and type "debug" as the command.

  1. type "a" after the '-'.
  2. start programming in assembly (make sure that the commands you use are pretty generic, ex. moving registers. only doing one command.)
  3. type nothing and press enter when done.
  4. type 'd' to see what the binaries are, and cross reference several programs.

please note that to program in actual binary you'll need a hex editor. Here's the one I use:

http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm

another idea that I do to learn more complex languages (like c++) is do the same as assembly but open it up in the hex editor.

Upvotes: 2

Carl Norum
Carl Norum

Reputation: 224844

In general there is a one-to-one mapping between assembly instructions and machine code (what you're calling binary). You can find these mappings in the instruction set architecture documentation for whatever machine you care about. Popular examples:

  1. Intel Software Developer Manuals, especially volumes 2A and 2B.
  2. ARM Architecture Reference Manual
  3. THe MIPS Instruction Set
  4. 8051 Instruction Set Manual

You'll be able to find a similar document for whichever architecture you want to work with. You can use the information in manuals like these to either to decode the machine instructions for a given program and identify how they were generated from the assembly source, or perhaps to hand-assemble your own program if you'd like.

Upvotes: 6

Adam Zalcman
Adam Zalcman

Reputation: 27233

Instruction set manuals are very helpful and assume certain degree of knowledge of computer hardware architecture. Here are some example documents:

ARM: http://simplemachines.it/doc/arm_inst.pdf

Intel: http://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-software-developer-vol-1-2a-2b-3a-3b-manual.pdf

SPARC V9: http://www.sparc.com/standards/SPARCV9.pdf

Upvotes: 1

Related Questions