0% found this document useful (0 votes)
9 views10 pages

2022 Long

The document discusses control transfer mechanisms in assembly language, categorizing them into unconditional transfers (using the JMP instruction) and conditional transfers (using various conditional jump instructions). It explains the role of status flags in the x86 architecture, which indicate the outcomes of operations and influence control flow. Additionally, it covers x86 processor modes of operation, including Protected Mode, Virtual-8086 Mode, Real-Address Mode, and System Management Mode, detailing their features and uses.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

2022 Long

The document discusses control transfer mechanisms in assembly language, categorizing them into unconditional transfers (using the JMP instruction) and conditional transfers (using various conditional jump instructions). It explains the role of status flags in the x86 architecture, which indicate the outcomes of operations and influence control flow. Additionally, it covers x86 processor modes of operation, including Protected Mode, Virtual-8086 Mode, Real-Address Mode, and System Management Mode, detailing their features and uses.

Uploaded by

Muhammad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

2022

1. Discuss conditional and unconditional transfer

In assembly language programming, control transfer or branching is a mechanism to alter the


flow of program execution. Normally, the CPU loads and executes instructions sequentially, but
at times it may be required to jump to a different part of the program. These transfers of control
are primarily classified into two types: unconditional transfers and conditional transfers.

1. Unconditional Transfer

An unconditional transfer occurs when the program jumps to a new location regardless of any
conditions. The most common instruction used for an unconditional transfer in assembly
language is the JMP (Jump) instruction.

The JMP instruction allows control to be transferred to a specified memory address or label.
When executed, the program counter (or instruction pointer) is updated to this new address, and
the execution continues from that point onward. This type of transfer is always executed, without
checking any conditions.

Syntax of JMP instruction:

JMP destination

Here, destination refers to the label or address to which control should transfer.

Example:

JMP start ; Jump to the label 'start'

An important feature of the JMP instruction is that it can be used to create loops. Since it always
causes a jump to the destination, it can be used to continuously loop back to the same point in the
program. For example, an infinite loop can be created using:

top:
; some instructions
JMP top ; jump back to the top to repeat

In this scenario, the loop will run indefinitely unless there is an external condition or instruction
to break the loop. Since JMP is unconditional, it doesn't depend on any condition and will
always jump to the destination.
2. Conditional Transfer

In contrast to the unconditional transfer, a conditional transfer occurs only if certain conditions
are met. This type of transfer is useful when implementing decision-making structures in
programs, such as if statements and loops. Conditional transfers are based on the evaluation of
certain flags in the CPU status register, such as Zero (ZF), Carry (CF), Sign (SF), etc.

Conditional jump instructions are used to check the state of these flags after arithmetic,
comparison, or logical operations, and the jump is taken only if the condition is true.

There are various conditional jump instructions available for different situations, such as JE
(Jump if Equal), JNE (Jump if Not Equal), JL (Jump if Less), JG (Jump if Greater), JZ (Jump if
Zero), and many others.

Syntax of a conditional jump instruction:

Jcond destination

Where Jcond refers to the specific condition (like JE, JNE, JL, etc.), and destination is the
address or label to jump to if the condition is satisfied.

For example, the JE (Jump if Equal) instruction will jump to a label if the Zero flag is set,
meaning the two compared values are equal.

Example:

cmp eax, 5 ; compare the value in EAX with 5


je equal ; jump to 'equal' if EAX equals 5

In this example, if the value in EAX is equal to 5, the JE instruction will transfer control to the
equal label. If EAX is not equal to 5, the program continues executing the instructions following
the cmp and je.

Types of Conditional Jumps:

1. Equality Based Jumps: These jumps occur based on whether two values are equal or
not. Examples include:
o JE (Jump if Equal): Jumps when the Zero flag is set.
o JNE (Jump if Not Equal): Jumps when the Zero flag is not set.
2. Signed/Unsigned Comparisons: These jumps evaluate signed or unsigned comparisons.
For example:
o JL (Jump if Less): Jumps if the first operand is less than the second operand
(signed comparison).
o JG (Jump if Greater): Jumps if the first operand is greater than the second
operand (signed comparison).
3. Zero or Non-zero Flag: Some instructions test the Zero flag specifically:
o JZ (Jump if Zero): Jumps when the Zero flag is set, i.e., the result of the
previous operation was zero.
o JNZ (Jump if Not Zero): Jumps when the Zero flag is not set.

3. Examples of Conditional Transfers

 JE (Jump if Equal):

cmp eax, 5 ; compare the value in EAX with 5


je equal ; jump to 'equal' if EAX equals 5

 JL (Jump if Less):

cmp ax, 6 ; compare AX with 6


jl less ; jump to 'less' if AX is less than 6

 JG (Jump if Greater):

cmp ax, 4 ; compare AX with 4


jg greater ; jump to 'greater' if AX is greater than 4

4. LOOP Instruction

Another conditional transfer is the LOOP instruction. It is used to repeat a block of statements a
specific number of times. The LOOP instruction uses the ECX register as a counter, which is
automatically decremented each time the loop is executed. When ECX reaches zero, the loop
ends.

Syntax of LOOP instruction:

LOOP destination

The LOOP instruction decrements ECX and checks if it is zero. If ECX is not zero, the jump to
the specified label (destination) is taken. Otherwise, execution continues from the next
instruction.

Example:

mov ecx, 5 ; set the loop counter to 5


top:
inc ax ; increment AX register
loop top ; decrement ECX and jump to top if ECX is not zero

This loop will run 5 times, incrementing AX each time. The LOOP instruction is commonly
used in scenarios where a specific number of iterations is needed.
2. Discuss status flags with help of example.

In the x86 architecture, the status flags are used to indicate the outcome of various arithmetic
and logical operations performed by the CPU. These flags are stored in the EFLAGS register
(or simply FLAGS register in the 16-bit mode). They are crucial for control flow in assembly
programs, as they can influence conditional jumps and other instructions based on the results of
operations.

List of Key Status Flags:

1. Carry Flag (CF):


o Description: The Carry Flag is set when an unsigned arithmetic operation results
in a carry out of the most significant bit (i.e., the result is too large to fit in the
destination operand). It is cleared if there is no carry or borrow.
o Example:

mov al, 255 ; AL = 0xFF (255 in decimal)


add al, 1 ; AL = 0x100 (carry occurs)
; CF is set because the result doesn't fit in an 8-bit register.

oUse Case: The CF is used in unsigned arithmetic to handle carries or borrows.


2. Overflow Flag (OF):
o Description: The Overflow Flag is set when a signed arithmetic operation
overflows, meaning the result is too large or too small to fit in the destination
operand. This can happen when adding or subtracting signed numbers.
o Example:

mov al, 127 ; AL = 0x7F (127 in decimal)


add al, 1 ; AL = 0x80 (-128 in signed form)
; OF is set because adding 1 to 127 causes an overflow in signed
numbers.

o Use Case: The OF is primarily used in signed arithmetic to indicate overflow


conditions.
3. Sign Flag (SF):
o Description: The Sign Flag is set if the result of the operation is negative. It
reflects the most significant bit (MSB) of the result, where 1 represents a negative
number (in two's complement representation).
o Example:

mov al, -5 ; AL = 0xFB (signed -5)


add al, -2 ; AL = 0xF9 (signed -7)
; SF is set because the result of AL is negative.

oUse Case: SF is useful for determining if a result is negative after arithmetic


operations.
4. Zero Flag (ZF):
o Description: The Zero Flag is set if the result of an arithmetic or logical operation
is zero. If the result is non-zero, the ZF is cleared.
o Example:

mov al, 0 ; AL = 0
add al, 0 ; AL = 0 (still zero)
; ZF is set because the result of the addition is zero.

o
Use Case: ZF is frequently used in conditional jump instructions like JZ (jump if
zero).
5. Auxiliary Carry Flag (AC):
o Description: The Auxiliary Carry Flag is set when a carry occurs from bit 3 to bit
4 during an arithmetic operation. This flag is mainly used in BCD (Binary-
Coded Decimal) operations.
o Example:

mov al, 0x99 ; AL = 0x99


add al, 0x01 ; AL = 0x9A
; AC is set because there was a carry from bit 3 to bit 4.

o
Use Case: AC is used in BCD arithmetic to handle carry between 8-bit groups
(decimal digits).
6. Parity Flag (PF):
o Description: The Parity Flag is set if the least-significant byte of the result
contains an even number of 1 bits. If the number of 1 bits is odd, PF is cleared.
o Example:

mov al, 0x02 ; AL = 0x02 (binary 00000010)


; PF is set because there are two 1's in the result (even
number).

o Use Case: PF is generally used for error-checking in communication systems


where data corruption might cause a change in the parity of the byte.

3. Write an assembly program to print table of 2.

.386

.model flat,stdcall

.stack 4096

INCLUDE Irvine 32.inc


.data

Arr1 DWORD 10 DUP(?) ; Reserve space for storing 10 results

.code

Main PROC

MOV ecx, 10 ; Set loop counter to 10 (to repeat 10 times)

MOV esi, offset Arr1 ; ESI points to the start of Arr1

MOV ebx, 1 ; Set EBX to 1 (starting multiplier)

L1:

MOV eax, 2 ; Load 2 into EAX (we are working with multiples of 2)

MUL eax, ebx ; Multiply EAX by EBX (EAX = 2 * EBX)

MOV DWORD PTR [esi], eax ; Store the result in the current Arr1 element

ADD esi, 4 ; Move to the next DWORD (4 bytes) in Arr1

INC ebx ; Increment the multiplier (1 -> 2 -> 3 -> ...)

loop L1 ; Decrement ECX and repeat if ECX != 0

mov ecx, 10

mov esi, offset Arr1

L2:

mov eax, [esi] ; Load value from array

call WriteInt ; Print the value

call Crlf ; Print newline

add esi, 4 ; Move to next DWORD

loop L2 ; Repeat 10 times

exit

main ENDP

END main
4. x86 processors have three primary modes of operation:
protected mode, real-address mode, and system management
mode. A sub-mode, named virtual-8086, is a special case of
protected mode. You are supposed to discuss these modes.

x86 processors have three primary modes of operation: Protected Mode, Real-Address Mode,
and System Management Mode (SMM). Additionally, there is a sub-mode called Virtual-8086
Mode which is a special case of Protected Mode. Here's a detailed explanation of each mode:

1. Protected Mode

 Definition: Protected mode is the default operating mode of modern x86 processors
(especially from Intel and AMD). In this mode, the processor can access all of its
features, including advanced memory management and protection.
 Key Features:
o Memory Segmentation: In protected mode, the memory is divided into
segments, and each program runs in its own separate memory area. This
segmentation ensures that programs cannot access memory outside of their
assigned segments, providing a layer of protection and preventing programs from
overwriting each other’s memory.
o Multitasking Support: The processor can manage multiple tasks or programs
simultaneously without one program interfering with another. This is important
for modern operating systems like Windows and Linux, which rely on protected
mode for multitasking.
o Access to 32-bit Address Space: Unlike Real-Address Mode, which limits
addressing to 20 bits, Protected Mode enables 32-bit addresses, which
significantly increases the amount of available memory.
 Example Use: Modern operating systems, like Windows XP, use Protected Mode to
execute programs. It provides a stable environment by isolating running programs.

2. Virtual-8086 Mode (V86 Mode)

 Definition: Virtual-8086 mode is a sub-mode of Protected Mode. It allows x86


processors to execute legacy software designed for the Intel 8086 processor, such as MS-
DOS applications, within a protected, multitasking environment.
 Key Features:
o Real-Mode Software in Protected Mode: Virtual-8086 mode allows programs
designed for the older 16-bit real-address mode (like MS-DOS programs) to run
on modern 32-bit systems without compromising the safety of other tasks running
in the system.
o Safety and Isolation: The major advantage of Virtual-8086 mode is that it allows
legacy software to run without causing harm to the system. If an MS-DOS
program attempts to access system memory or hardware directly (like it did in
real-address mode), it cannot affect other programs, and the OS can isolate it.
o Multitasking Capability: Windows XP, for example, could run several Virtual-
8086 sessions simultaneously, allowing multiple MS-DOS programs to run at the
same time in separate, isolated environments.
 Example Use: Virtual-8086 mode is primarily used for backward compatibility, allowing
older DOS programs to run on modern systems while being protected from crashing or
corrupting the system.

3. Real-Address Mode

 Definition: Real-Address Mode is the mode in which the Intel 8086 processor operated,
and it is the most basic mode of the x86 processor. It provides direct access to memory
and hardware devices but without any memory protection or segmentation.
 Key Features:
o Direct Access to Hardware: In Real-Address Mode, programs can directly
access the system’s memory and hardware devices, allowing low-level system
operations.
o Limited Memory Access: This mode uses a 20-bit address bus, which limits the
maximum addressable memory to 1 MB.
o Lack of Memory Protection: Since there is no memory protection or
multitasking, programs running in Real-Address Mode can overwrite each other’s
memory. This can lead to system crashes and instability, as one faulty program
can crash the entire system.
 Example Use: Real-Address Mode is typically used in older operating systems like MS-
DOS or in compatibility modes for running older software on newer systems, such as
Windows 98.

4. System Management Mode (SMM)

 Definition: System Management Mode is a special, low-level mode designed primarily


for system management tasks such as power management, hardware control, and system
security. This mode is not typically used for running standard applications but rather for
maintaining the system’s operation.
 Key Features:
o Out-of-Band Operations: SMM runs independently of the operating system and
application programs. It allows manufacturers to implement specialized functions
that operate outside of the normal execution flow.
o System Management Functions: Typical uses of SMM include system-level
functions such as hardware event handling, thermal management (e.g., controlling
fan speeds), and power management (e.g., putting the system into low-power
states).
o Security: SMM can also be used for implementing secure system operations, such
as protecting BIOS code from tampering.
 Example Use: SMM is primarily used by hardware manufacturers to implement power-
saving features or system security mechanisms like the Trusted Platform Module (TPM)

5. Fill in the requested register values on the right side of the


following instruction sequence

myBytes BYTE 10h,20h,30h,40h

myWords WORD 8Ah,3Bh,72h,44h,66h

myDoubles DWORD 1,2,3,4,5

myPointer DWORD myDoubles

mov esi,OFFSET myBytes

mov al,[esi] ; a. AL = 10h

mov al,[esi+3] ; b. AL = 40h

mov esi,OFFSET myWords + 2

mov ax,[esi] ; c. AX = 003Bh


mov edi,8

mov edx,[myDoubles + edi] ; d. EDX = 3

mov edx,myDoubles[edi] ; e. EDX = 3

mov ebx,myPointer

mov eax,[ebx+4] ; f. EAX = 2

You might also like