In assembly language, instructions are the core operations that a CPU performs. Each instruction represents a single operation, such as moving data, performing arithmetic, controlling program flow, or interacting with memory or I/O devices. These instructions are encoded in machine language and executed directly by the processor.
Assembly instructions are often specific to the processor architecture (e.g., x86, ARM, MIPS), and each architecture has its own set of supported instructions, known as its instruction set architecture (ISA).
In this explanation, we'll cover the types of assembly instructions, how they are structured, and some common examples.
An assembly instruction generally consists of:
Syntax:
opcode operand1, operand2, ...
For example:
MOV AX, 5 ; Move the value 5 into register AX
ADD BX, 3 ; Add 3 to the value in register BX
Instructions in assembly can be categorized into several broad types based on the operation they perform. Common categories include:
Data movement instructions are used to move data between registers, memory, and I/O devices.
MOV (Move)MOV destination, sourceExample:
MOV AX, 10 ; Move the immediate value 10 into the AX register
MOV BX, AX ; Move the value from AX into BX
PUSH and POPPUSH saves a value to the stack, and POP retrieves a value from the stack.PUSH operandPOP operandExample:
PUSH AX ; Push the value in AX onto the stack
POP BX ; Pop the value from the stack into BX
LEA (Load Effective Address)LEA destination, sourceExample:
LEA SI, var ; Load the address of 'var' into register SI
These instructions perform mathematical operations such as addition, subtraction, multiplication, and division.
ADD (Add)ADD destination, sourceExample:
ADD AX, BX ; Add the value in BX to AX
SUB (Subtract)SUB destination, sourceExample:
SUB AX, 5 ; Subtract 5 from the value in AX
MUL and IMUL (Multiply)MUL is for unsigned multiplication, and IMUL is for signed multiplication.MUL operandIMUL operandExample:
IMUL AX, BX ; Multiply AX by BX (signed multiplication)
DIV and IDIV (Divide)DIV is for unsigned division, and IDIV is for signed division.DIV operandIDIV operandExample:
IDIV BX ; Divide AX by BX (signed division, quotient in AL, remainder in AH)
These instructions perform logical operations, such as AND, OR, XOR, and NOT.
AND (Logical AND)AND destination, sourceExample:
AND AX, 1 ; Perform a bitwise AND between AX and 1
OR (Logical OR)OR destination, sourceExample:
OR AX, BX ; Perform a bitwise OR between AX and BX
XOR (Logical XOR)XOR destination, sourceExample:
XOR AX, AX ; Clear the AX register (AX = 0)
NOT (Logical NOT)NOT operandExample:
NOT AX ; Inverts the bits in AX
These instructions alter the flow of execution in a program. They are used for looping, branching, and handling conditional logic.
JMP (Jump)JMP labelExample:
JMP end ; Jump to the label 'end'
JE, JNE, JG, JL (Conditional Jumps)JE: Jump if equal (ZF=1)JNE: Jump if not equal (ZF=0)JG: Jump if greater (SF=OF)JL: Jump if less (SF≠OF)Example:
JE equalLabel ; Jump to 'equalLabel' if the zero flag is set (i.e., equality)
CALL and RET (Call and Return)CALL is used to call a function, and RET is used to return from a function.CALL functionRETExample:
CALL myFunction ; Call a function
RET ; Return from function
These instructions manage the stack, which is used for storing temporary data like function parameters, return addresses, and local variables.
PUSH and POPExample:
PUSH AX ; Push AX onto the stack
POP BX ; Pop a value from the stack into BX
CALL and RETCALL instruction pushes the return address onto the stack and then jumps to a function, while RET pops the return address from the stack and jumps back to the calling function.Example:
CALL MyFunction ; Push return address and call MyFunction
RET ; Pop return address and return from function
These instructions interact with input and output devices, such as reading from the keyboard or writing to the screen.
IN and OUTIN destination, portOUT port, sourceExample:
IN AL, 60h ; Input from port 60h (keyboard)
OUT 60h, AL ; Output to port 60h (keyboard)
These instructions control CPU-specific features, such as flags, segmentation, or system calls.
NOP (No Operation)Example:
NOP ; Do nothing
HLT (Halt)Example:
HLT ; Halt the program
Assembly instructions are the fundamental operations that control a computer’s behavior at the lowest level. They can perform data movement, arithmetic, logic, control flow, stack operations, and I/O operations. Understanding how to use these instructions is crucial for
Open this section to load past papers