Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do assembly languages work?

I'm very curious how assembly languages work- I remain general because I'm not talking only about intel x86 assembly (although it's the only one I'm remotely familiar with). To be a bit more clear...

mov %eax,%ebx

How does the computer know what an instruction like "mov" does? How does it know that eax and ebx are registers? Do people write grammars for assembly languages? How do they write this? I imagine nothing is stopping someone from writing an assembly language that substitutes the mov instruction with something like dog or horse etc., (obviously this isn't semantic at all)

Sorry if this isn't too clear, but it's something I find a bit puzzling- I know it can't be magic, but I can't see how it works. I've looked up some stuff on wikipedia, but all it seems to say is it translates it down to machine code, well, what I'm asking is how that translation occurs I suppose.

Thoughts?

EDIT: I realize that this stuff is defined in reference manuals and things, I guess what I wish to know is how you tell your processor "Okay, when you see mov you're gonna do this". I also know that it's a sequence of probably a ton of logic gates..but there has to be some way for the processor to recognize is that mov is the symbol that means "use these logic gates"

like image 799
LainIwakura Avatar asked Jun 24 '11 05:06

LainIwakura


People also ask

How is assembly language executed?

An assembler is a program that reads assembly language commands and translates then into a sequence of binary instructions, addresses and data values that is called machine code. The machine code is stored in the computer's memory and can be executed by the computer at some later time.

How assembly language is created?

A program written in assembly language consists of a series of mnemonic processor instructions and meta-statements (known variously as declarative operations, directives, pseudo-instructions, pseudo-operations and pseudo-ops), comments and data.

Can humans understand assembly language?

Such an assembly program would be composed of many, many instructions, that together do something that seems very simple and basic to a human. This makes it hard for humans to read an assembly program.

Why is assembly language so powerful?

These days, assembly language makes it possible to manipulate hardware directly, address critical issues concerning performance and also provide access to special instructions for processors.


1 Answers

Your CPU doesn’t execute assembly. The assembler converts it into machine code. This process depends on both the particular assembly language and the target computer architecture. Generally those go hand in hand, but you might find different flavors of assembly language (nasm vs. AT&T, for example), which all translate into similar machine code.

A typical (MIPS) assembly instruction such as “And immediate”

andi $t, $s, imm

would become the 32-bit machine code word

0011 00ss ssst tttt iiii iiii iiii iiii

where s and t are numbers from 0–31 which name registers, and i is a 16-bit value. It’s this bit pattern that the CPU actually executes. The 001100 in the beginning is the opcode corresponding to the andi instruction, and the bit pattern that follows — 5-bit source register, 5-bit target register, 16-bit literal — varies depending on the instruction. When this instruction is placed into the CPU, it responds appropriately by decoding the opcode, selecting the registers to be read and written, and configuring the ALU to perform the necessary arithmetic.

like image 99
Josh Lee Avatar answered Oct 25 '22 11:10

Josh Lee