Assembly Language: Understanding Machine Code Basics

  • Thread starter Thread starter vead
  • Start date Start date
  • Tags Tags
    Assembly
AI Thread Summary
The discussion focuses on the fundamentals of machine code and assembly language, explaining how basic logic gates (AND, OR, NOT) are used to perform arithmetic operations like addition, which can be expanded to multiplication and division. It highlights the transition from machine language, which is difficult for humans to understand, to assembly language, which simplifies programming with more readable instructions. The conversation emphasizes that an assembler converts assembly code into machine code, allowing for easier programming while maintaining efficiency. Participants express confusion about how the assembler translates symbolic names into machine code, clarifying that the assembler replaces these variables with actual register numbers. The thread concludes with insights on how different CPU architectures may encode instructions differently, aiding in understanding the conversion process.
vead
Messages
92
Reaction score
0
hello friends

look at this old post by mr. job
Basically you start off with some logic gates, like the AND, NOT and OR, for example.
Each gate receives one or more binary inputs (each either 0 or 1). The AND outputs a 1 if both its inputs are a 1 and a 0 otherwise. The OR outputs 1 if either input is a 1 and 0 otherwise. The NOT outputs a 1 if the input is 0, and 0 if it is 1.
With these gates you build components for adding numbers. Addition of binary numbers is the same as addition of regular (base 10) numbers, for example 6+2 is, using 16-bit numbers:

00000110
+00000010
------------
00001000

We can implement addition using the basic logic gates, to form an 8-bit adder, which adds two 8-bit numbers. The adder starts at the rightmost bits of both inputs and moves left, computing the sum and any carryovers for the next column, just like we do manually.
Once we have 8-bit adders, or X-bit adders, we can stack them, or cascade them for summing numbers of 16, 32, 64 bits, etc.
With addition implemented you can now think about implementing multiplication, and then division, there are some popular & clever algorithms for doing multiplication and division using basic gates.
Some other operations you might want to do are shifts, i.e. shift all bits right or left:
shift 001 left = 010
So you develop a collection of operations all acting on 1 or more inputs:
Division
Multiplication
Addition
Subtraction
Logical AND
Logical NOT
Logical OR
Logical XOR
Shift Left
Shift Right
...

Then once you have these operations in place you want to use them. So you want to bring inputs in and perform operations on them. The CPU has a number of registers which store inputs and outputs. For bringing in input from main memory and storing it in the registers, so that you perform operations on them, you add an operation from moving bits from main memory into the registers, that's another operation.
Now that we have so many operations, we want to tell the CPU what operation to do. So, we encode the operations in some bit pattern, for example:
0001 Perform NOT
0010 Perform AND
0011 Perform OR
0100 Perform Addition
0101 Perform Subtraction
0110 Perform Multiplication
0111 Perform Division
1000 Move bits from memory into registers
...
...etc

Now, suppose you want the CPU to perform addition of 6 and 2, you pass it in the operation code (0100), the number 6 (0110) and the number 2 (0010), so something like:
0100 0110 0010
Now we can start talking about a sequence of operations:
0100 0110 0010
01010 010 0010
0100 0110 0110
0010 1000 0010
1000 1010 0010
...
This Machine Langauge is hard to work with so we develop assembly languages, which compiles to Machine Language, so for adding two numbers maybe now we can do:
ADD $R1 2

its very great explanation by mr. job


I have little doubt
I understood how machine code generate but I did not understand how does assembly code make from machine code

I tried to understand with below example
machine language is hard to understand so we developed assembly language
Mov (1000)
6 (0110)
mov A, # 6 ( assembly language )
1000 0110 ( machine language )
another
Mov (1000)
2 (0010)
mov R1#2 ( assembly language )
1000 0010 ( machine language )
8=6+2
Add A, R1
0100 0110 0010

machine language

1000 0110
1000 0010
0100 0110 0010

assembly language
mov A, #6
mov R1,#2
add A,R1

I know assembly code convert into machine code but for low level understanding how we developed assembly language to understand machine language
 
Technology news on Phys.org
vead said:
hello friends

look at this old post by mr. job


its very great explanation by mr. job


I have little doubt
I understood how machine code generate but I did not understand how does assembly code make from machine code

I tried to understand with below example
machine language is hard to understand so we developed assembly language
Mov (1000)
6 (0110)
mov A, # 6 ( assembly language )
1000 0110 ( machine language )
another
Mov (1000)
2 (0010)
mov R1#2 ( assembly language )
1000 0010 ( machine language )
8=6+2
Add A, R1
0100 0110 0010

machine language

1000 0110
1000 0010
0100 0110 0010

assembly language
mov A, #6
mov R1,#2
add A,R1

I know assembly code convert into machine code but for low level understanding how we developed assembly language to understand machine language

Are you asking a question here? It's hard to tell without punctuation such as a '?' at the end. If you're asking 'how [did] we develop assembly language to understand machine language?', the answer is, we didn't develop assembly language to understand machine language - we developed assembly language to make programming simpler. Each assembly language instruction is converted by the assembler into the corresponding machine code.

Assembly language is somewhat easier for humans to comprehend, because the instructions (such as MOV, ADD, and so on) are reasonably close to words in English.
 
Mark44 said:
. Each assembly language instruction is converted by the assembler into the corresponding machine code.

.

I know assembler convert assembly code into machine code
I think you are talking i this way

suppose we want to add two 8 bit number

example 6+ 2 =8


suppose we have binary sequence
Mov (1000)
Add (0100)
6(00000110)
2(00000010)
8(00001000)

we write assembly program
assembly language
mov A, #6
mov R1,#2
add A,R1

assembler convert assembly code into machine code
machine language
1000 0110
1000 0010
0100 0110 0010

we can burn this machine code into the memory of micro controller where control unit tell the cpu what to do. and cpu perform arithmetic and logic operation

instruction

Mov
shift
add
multiply
subtract

registers
accumulator
R1,R2...
address register
data register


assembly statement
mov A, # 6 ( assembly language )
1000 0110 ( machine language )
6(0110) indicate data value that will be store in data register
(1000) indicate the mov operation
but how does assembler know that the value 0110 is need to store in A register
assemble convert symbol A into machine code
A101 and # 10

just for example
mov A, # 6
1000 101 10 0110
here assembly statement is converted into machine language
 
Last edited:
vead said:
assembly statement
mov A, # 6 ( assembly language )
1000 0110 ( machine language )
6(0110) indicate data value that will be store in data register
(1000) indicate the mov operation
but how does assembler know that the value 0110 is need to store in A register
I think you are missing a step in converting assembly code to machine code. Most assembly languages allow variable names like 'A'. So 'A' might be a symbolic name for a register that is not yet assigned. That makes writing assembly code much easier. The assembler does a step where it replaces the variable names with some physical register numbers or memory locations. Then the machine code will use the actual register numbers.
 
FactChecker said:
I think you are missing a step in converting assembly code to machine code. Most assembly languages allow variable names like 'A'. .

which step are you talking

In my example A indicate accumulator

can you tell me example where we write instruction for data and address
 
vead said:
assembly statement
mov A, # 6 ( assembly language )
1000 0110 ( machine language )
6(0110) indicate data value that will be store in data register
(1000) indicate the mov operation
but how does assembler know that the value 0110 is need to store in A register
assemble convert symbol A into machine code
A101 and # 10

just for example
mov A, # 6
1000 101 10 0110
here assembly statement is converted into machine language
I think that what you're missing is that the destination register is usually encoded in the machine code itself. I don't know what CPU you're using, so can't say for sure in your case. For the Intel CPUs, for which I'm more familiar, the machine code for a move to AL is 0xA0 and for a move to AX, it is 0xA1. Moves to different registers or to memory have different machine code.

In your example, the machine code 1000 probably means "move to accumulator". Hope that helps.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top