Malaika 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Assembly language

Submitted by: Malaika (27)


Nawera (17)
Submitted to: Mam Faiza
Topic: Program Control Instruction
Submitted date: 20-september-2024
Program control instruction
Program control instructions are the machine codes that are used by machine or
in assembly language by user to command the processor act accordingly.

These instructions are various types.

There are used in assembly language by user also.

But in high level language, used code is translated into machine code and thus
instructions are passed to instruct the processor do the task.

A Program Control Instruction changes address value in the PC and hence


the normal flow of execution. Change in PC causes a break in the execution of
instruction.

Types of program control instructions:

 Compare instruction
 Conditional branch instruction
 Unconditional branch instruction
 Subroutines
 Halting instructions
 Interrupts instruction

CMP instruction:
The CMP instruction compares two operands. It is generally
used in conditional execution. This instruction basically subtracts one operand
from the other for comparing whether the operands are equal or not. It does not
disturb the destination or source operands. It is used along with the conditional
jump instruction for decision making.

Syntax:
CMP destination, source
JUMP:
Jump is an instruction to control the program flow.

JUMP

Unconditional jump Conditional jump

Conditional jump:
This is performed by a set of jump instructions j<condition>
depending upon the condition. The conditional instructions transfer the control
by breaking the sequential flow and they do it by changing the offset value in IP.
“Jump to label when condition occurs”.

Syntax:
Opcode label
Opcode:
Instructions Description
JE , JZ Jump if equal, Jump if zero.

JNE , JNZ Jump if not equal, Jump if not zero.

JL , JB Jump if less, Jump if below.

JLE , JBE Jump if less or equal, Jump if below or equal.

JG , JA Jump if greater, Jump if above.

JGE , JAE Jump if greater or equal, Jump if above or equal.

JC , JNC Jump if carry, Jump if no carry.

Example:
.model small

.stack 100h

.data

.code

Main proc

L1:

Mov, ah, 1

Int 21h

Mov dl, 3

Cmp al,dl
JE L1

Mov ah, 4ch

Int 21h

Main end

End main

Unconditional jump:
Control is transferred unconditionally to the target label
which points to the address of target instruction. “Jump to label without any
condition”.

Syntax:
JMP label

Example:
.model small

.stack 100h

.data

.code

Main proc
L1:

Mov dl, ‘a’

Mov ah, 2

Int 21h

Jmp L1

Mov ah 4ch

Int 21 h

Main endp

End main

Unconditional jump

Intrasegment jump Intersegment jump

Far

Short Near

JMP-Unconditional jump:
 The general form of the jmp instruction is jump label.
 No flags are affected by this instruction.
 The short jump is a two-byte instruction that allows jumps or branches to
memory locations within +127 and -128 bytes from the address following
jump.
 A near jump is three-byte instruction that allows a branch or jump within
+32 and -32 bytes (+32768B to -32768B) from the instruction in the
current code segment.
 Far jump is a 5-bytes instruction that allows a jump to any memory location
within the real memory location.

Short jump:
In programming, a "short jump" is like a small leap from one point
in your code to another. It's called a "relative jump" because it doesn't go to a
fixed address; instead, it moves a certain number of steps forward or backward
from where the program currently is.

 Opcode (EB): This is the command that tells the program to make a
jump.
 Disp (Distance): After the opcode, there's a number that tells the
program how far to jump. This number is 8 bits long, which means it, can
represent a small distance in the code.
 Opcode:

EB Disp (8 bit)
For example, if the program is at one line and you use a short jump to go 4 steps
ahead, the program will skip the next few lines and continue from the new
position.

The advantage of short jumps is that they are flexible. You can move them around
without worrying about changing the distance, making the code easier to
manage.

 The short jump instruction appears in figure below.


Short jump

Near jump:
A "near jump" in programming is like making a bigger move in your
code compared to a short jump. It allows you to jump a much greater distance
forward or backward.

 Distance: A near jump can move you as far as 32,768 steps forward or
backward from where you are in the code. This means it can cover a larger
part of the code compared to a short jump.
 Instruction Size: The near jump instruction has 3 parts (3 bytes):
The first part is the command to "jump."
The next two parts tell the program how far to jump. These two parts
are split into a low and a high number so that the program can handle
bigger jumps.
Opcode:
E9 Disp (low) Disp (high)
Near jump

Far jump:
A "far jump" in programming is like moving to a totally different part
of the program, even in another section of memory.

 Changing Segments: When you do a far jump, the program switches to a


new code area. It updates two things:

- Code Segment (CS): This tells the program where the new section of code is
located.

- Instruction Pointer (IP): This shows where to start running in that new
section.

 Instruction Size: A far jump has 5 parts (5 bytes):

- The first part tells the program to jump.

- The next four parts give the new CS and IP locations so the program knows
where to go.

Example: When a far jump happens, the program gets the new CS and IP values
from the instruction itself. This allows it to continue from a completely different
spot in memory.

Opcode:
EA IP (low) IP (high) CS (low) CS (high)

Far jump

You might also like