Assembly Language: Understanding Machine Code Basics

  • Thread starter vead
  • Start date
  • Tags
    Assembly
In summary: If you are asking how we developed assembly language to understand machine language, the answer is that we developed assembly language to make programming simpler. Each assembly language instruction is converted by the assembler into the corresponding machine code.
  • #1
vead
92
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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
 
  • #6
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.
 

1. What is Assembly Language?

Assembly Language is a low-level programming language that is used to write programs that can be directly executed by a computer's processor. It is also known as Assembly Code or Machine Language and is considered the most basic form of programming language that is closest to the computer's native language.

2. How is Assembly Language different from high-level programming languages?

Assembly Language is different from high-level programming languages in that it uses mnemonic codes and symbols to represent machine instructions, rather than words or phrases. It also directly corresponds to the machine code, making it less abstract and more difficult to read and write compared to high-level languages. However, it offers more control and efficiency as it allows programmers to directly access and manipulate hardware resources.

3. What are the advantages of using Assembly Language?

The main advantage of using Assembly Language is its high level of control and efficiency. Since it directly corresponds to machine code, it allows programmers to have a deeper understanding of how the computer works and the ability to optimize their programs for specific hardware. It is also useful for developing low-level programs such as operating systems, device drivers, and real-time systems.

4. How do you write programs in Assembly Language?

To write programs in Assembly Language, you need to use a text editor or an Integrated Development Environment (IDE) that supports Assembly Language. The program is written using a combination of mnemonic codes and symbols that correspond to machine instructions. Once the program is written, it is then assembled by a software program called an assembler, which converts it into machine code that can be executed by the computer.

5. Is learning Assembly Language necessary for a career in computer science?

While learning Assembly Language is not a requirement for a career in computer science, it can be beneficial for those interested in low-level programming, computer architecture, or embedded systems. It can also help in developing a deeper understanding of how computers work, which can be useful in troubleshooting and optimizing software. However, high-level programming languages are more commonly used in the industry due to their ease of use and portability.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
1
Views
639
Replies
6
Views
1K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Replies
9
Views
29K
  • Programming and Computer Science
2
Replies
60
Views
16K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
Back
Top