Machine language to assembly language and Assembly language to machine language

Machine language to assembly language and Assembly language to machine language

Microprocessor  Machine language to assembly language and Assembly language to machine language


How Machine language to assembly language
Ans:
 

  1. To translate machine language instructions into assembly language, you need to examine the binary or hexadecimal representation of the instruction and understand the instruction format and its components. Here's a general approach to translate machine language into assembly language:
 
  1. Identify the opcode: The opcode represents the operation to be performed. It determines the type of instruction and the operation being carried out.
 
  1. Determine the instruction format: Different instructions have different formats, specifying the operands, addressing modes, and other relevant information. Understand the format of the instruction you're working with.
 
  1. Analyze the instruction components: Determine the various parts of the instruction, such as registers, addressing modes, displacement values, immediate data, and any other relevant fields.
 
  1. Convert the instruction components into assembly language notation: Translate the instruction components into their corresponding assembly language representation, following the syntax and conventions of the specific assembly language you are working with.
 
  1. Combine the components and write the assembly language instruction: Put together the translated components and write the assembly language instruction using the appropriate syntax.

How Assembly language to machine language?
Ans:
To translate assembly language instructions into machine language, you need to understand the assembly language syntax and the corresponding instruction set architecture (ISA). Here's a general approach to translate assembly language into machine language:
  1. Identify the opcode: Determine the opcode for the specific instruction. The opcode represents the operation to be performed.
 
  1. Determine the instruction format: Different instructions have different formats, specifying the operands, addressing modes, and other relevant information. Understand the format of the instruction you're working with.
 
  1. Analyze the instruction components: Break down the assembly language instruction into its components, such as registers, addressing modes, displacement values, immediate data, and any other relevant fields.
 
  1. Convert the instruction components into machine language representation: Translate each component of the instruction into its binary or hexadecimal representation, following the format and conventions of the specific ISA you are working with.
 
  1. Combine the components and obtain the machine language representation: Concatenate the binary or hexadecimal representations of the instruction components to obtain the machine language representation of the instruction.
Difference between machine language and assembly language
 
Machine Language Assembly Language
  • Binary code directly understood by computer hardware.
  • Mnemonic codes representing machine language instructions.
  • Lowest-level programming language.
  • Low-level programming language.
  • Difficult for humans to read and understand.
  • More readable and understandable.
  • No translation required.
  • Requires translation by an assembler.
  • Not portable between architectures.
  • Somewhat portable across similar architectures.
  • Highest control over hardware.
  • Balanced control over hardware and ease of programming.


Convert the following:
  1. 8B872D98H from machine language to assembly language
  2. 6667C7848B7FEBFFAC59ABEF98H from machine language to assembly language
  3. MOV BX,SS: [BP+DI] from assembly language to machine language.
  4. MOV [EBX+4*ECX], 79AB88BEHfrom assembly language to machine language.
 
  • 8B 87 2D98 H
Byte 1  (8BH)

 
1 0 0 0 1 0 1 1

Opcode (100010) =   MOV
D (1) =  destination operand will be REG- register
W (1) = data size is word or 16 bit
Byte 2 ( 87H)
10 000 111

MOD (10) = 16-bit signed displacement
REG (000) = AX

R/M (111) = [BX]


Displacement = 2D98H( displacement low byte =2DH & high byte=98H)

Assembly code:   MOV   AX, [ BX + 982DH ]
How can it happen?
Byte 1 (8B):
  • Binary representation: 10001011
  • Opcode (first 6 bits): 100010 (MOV instruction)
  • D bit (bit 6): 1 (destination operand is a register)
  • W bit (bit 7): 1 (data size is a word or 16 bits)
Byte 2 (87H):
  • Binary representation: 10000111
  • Mod (first 2 bits): 10 (16-bit signed displacement)
  • REG (next 3 bits): 000 (AX register)
  • R/M (last 3 bits): 111 ([BX] addressing mode)
Displacement:
  • The displacement is formed by the next two bytes after the opcode and addressing mode bytes, which are "2D98H" in this case.
  • The displacement value is 2D98H, where the low byte is 2DH and the high byte is 98H.
Putting it all together, the correct translation is:
MOV AX, [BX+982DH]
This instruction moves the 16-bit value located at the memory address computed by adding the value in the BX register with the displacement 982DH into the AX register.
 
  • MOV [EBX+4*ECX], 79AB88BEH

Byte 1
Opocode(MOV) = 1100011
W=1(data size is 32 bit)
(1100 0111)=C7 H
Byte 2
MOD =00 (no displacement)
REG = 000 ( not used)
R/M = 100 ( uses scaled indexbyte)
(0000 0100)= 04 H

Byte 3 (scaled index byte)   [Base + SS * Index]
SS = 10 (multiply by 4 as 32 bit data)
Index (ECX)=001
Base (EBX)=011

(1000 1011)= 8B H
Immediate data lower word =88BEH
Immediate data higher word =79ABH

Machine code: 6766 C7 04 8B 88BE79ABH

How it happen?
Byte 1:
  • Opcode (first 7 bits): 1100011 (MOV instruction with a 32-bit operand)
  • W bit (bit 8): 1 (data size is 32 bits)
  • Binary representation: 11000111
  • Hex representation: C7H
Byte 2:
  • Mod (first 2 bits): 00 (no displacement)
  • REG (next 3 bits): 000 (not used in this instruction)
  • R/M (last 3 bits): 100 (scaled index addressing mode)
  • Binary representation: 00000100
  • Hex representation: 04H
Byte 3 (scaled index byte):
 
  • SS (first 2 bits): 10 (multiply by 4 as it is a 32-bit data)
  • Index (next 3 bits): 001 (ECX)
  • Base (last 3 bits): 011 (EBX)
  • Binary representation: 10001011
  • Hex representation: 8BH
Immediate Data:
 
  • The immediate data is the value "79AB88BEH" which needs to be split into two words.
  • The lower word is "88BEH" and the higher word is "79ABH".
Putting it all together, the correct translation is:
  • 6766 C7 04 8B 88BE79ABH
This is the machine code representation of the assembly instruction "MOV [EBX+4*ECX], 79AB88BEH". The instruction stores the 32-bit value "79AB88BEH" into the memory location calculated by adding the value in the EBX register with the scaled index value obtained by multiplying the value in the ECX register by 4.
 
  • MOV BX, SS: [BP+DI]
    Byte 1
Opcode (MOV) =   100010
D= 1 (destination is REG-register)
W=1 ( data is word size)

Byte 2
MOD = 00 (no displacement)
REG (register) = 011
R/M (memory)DS:[BP+DI] = 011
 
1000 1011
 
0001 1011


Machine Code:    8B1B H

 
  • 66 67 C7848B7FEBFFAC59ABEF98H
Byte-1(C7H)
1 1 0 0 0 1 1 1

Opcode (1100011) = MOV (immediate value into memory)
W=1 (32 bit data size)

Byte-2 (84H)
1 0 0 0 0 1 0 0

MOD= 10 (32 bit displacement)
REG (000)= not used
R/M (100) = uses scaled index byte

Byte 3(8BH) (scaled index byte)   [Base + SS * Index]
SS = 10 (multiply by 4 as 32 bit data)
Index (ECX)=001
Base (EBX)=011

Displacement lower word=7FEBH
Displacement higher word=FFACH
Immediate data lower word=59ABH
Immediate data higher word=EF98H

Assembly code:   MOV   [EBX + 4* ECX + FFAC7FEBH] ,  EF9859ABH
  1. MOV   [EBX + 4* ECX + FFAC7FEBH] ,  EFXX59ABH
  2. 66 67 C7 84 8B ABXX513488BE79ABH