I've trouble with understanding the difference between assembly and binary. Just I need to understand what the relation is between linked binary and assembly.
Assembly is basically binary code written in a form that humans can read. The assembler then takes the assembly code and translates it line by line to the corresponding bit code.
Imagine that there is a table with a line for each possible assembly statement. Then on each line there is on the left the statement itself, and on the right the corresponding bits that the computer can understand
That being said assemblers also have extra functionality like macros etc. but the main functionality is that described above.
For programmers, Binary is just a numbering system. For example, base2
consists of some 0's and 1's. All computers work with these binary numbers (0 and 1). They perceive the instructions as a set of these numbers. They do not feel the human-generated code which is generally generated using a high-level programming language such as Python, Java, and etc.
It is obvious that machine instructions in computers are not really human-readable –most people can't figure out the operational difference between 100010001... and 010001000... by just looking at a binary or hex representation of the instruction bytes. These instructions are just Machine Codes.
For instance, the machine code for loading a value into a register in x86-16 architecture takes this instruction as a HEX code: 8B 0E 34 12
where 8B
means mov r16, r/m16
and 0E
specifies which register the destination is (in this case CX), and which memory/source register with a 2-bit addressing mode field and 3-bit base register (in this specific case, there is no register, just a 16-bit absolute displacement).
P.S. Just to be clear, HEX code is used to represent the Machine Code. Actually, it is easy to translate it to binary "10001011000011100011010000010010" and this is what you have mentioned as binary. HEX is just a text serialization format for binary numbers like a string of ASCII 0 and 1, but more compact.
Assembly is more high-level than Machine Code and makes such binary/HEX instructions readable for human. For example, the machine code 8B 0E 34 12
would be decoded / disassembled to MOV CX, [1234H]
.
The assembly tag wiki starts off by more or less answering this question. You should read it.
An assembler assembles human-readable assembly-language into bytes of a binary file. The asm source can specify bytes directly, in hex or whatever. In x86 NASM syntax, you can use a db 0x30
statement to assemble that byte into the current output position.
You can also use mnemonics for machine instructions. e.g. add eax, [rdi + rdx*4]
to ask an Intel-syntax x86 assembler to emit the bytes that encode that instruction. The assembler then figures out the shortest (or only) way to encode that instruction into machine code, and puts those bytes in the output.
There are further complications, for example modern object file formats have multiple sections (like .text
and .data
), and you can select which section your bytes will be assembled into. So you can keep constants near the code that uses them without actually mixing code and data in the final binary.
For example, see this godbolt link. In the right-hand pane, you can see the binary and the corresponding asm source.
Binary is not olny used as a number system to represent a 'number', but also can represent some objects and used as char. take a example number like '2', when you see it as a number, it is number,you can add it, and maybe someone's id 2, and you call him no.2, but you don't caculate it because it in fact is a char....
binary and assembly are one to one match, which means what you are writing in assembly is actually binary.
for example, before we have assembly, you want to add one and one, you may need to:
1.load 1 to accumulator
2.add 1 with the one in accumulator
3.store it in an address
but you can only use brinary insctrction to represent that .... what can you do? only thing you can is to use conbination of 0 and 1 to represent the things you need to do. lets just think 0001 means load, 0010 means add, 0011 as store, so you might write something like:
0001 000000001
0010 000000001
0011 000000101(000000101 is a location where you store the stuffs in
accumulator)
and that is quite mess, so clever you come out a good idea, which is use readable words to reperesent the instrction like this:
0001 -> load
0010 -> add
0011 -> store
so you can write it in assembly...
load 1
add 1
store 5
which is easily understand assembly!(of course you can change the number into hex form for abbreviation~)
you can see, when you translate it , 0001 is actually not a number, 00000001 is. so 0001 is just a notation, and assembly is used to replace the cahr type notaion for better read. 00000001 is really a number, and you can writer it on any other form, but coincidently for decimal is 1, for hex is 1 too:)
© 2022 - 2024 — McMap. All rights reserved.