Assembly language Programs Involving Bit manipulation instructions like checking

Programs Involving Bit manipulation instructions like checking.
 1.Write an ALP to separate odd and even numbers
2.Write an ALP to separate positive and negative numbers
 3.Write an ALP to find logical ones and zeros in a given data
 4.Write an ALP to find whether the given code belongs 2 out of 5 codes
or not.
5.Write an ALP to check bitwise palindrome or not Write an ALP to check
whether the given number is palindrome or not.




1.Write an ALP to separate odd and even numbers

code:
.MODEL SMALL   ; Specify the memory model to be used as SMALL, meaning that both data and code will fit within a single 64KB segment each

.DATA          ; Start of data segment

NUMBERS DB 10H, 15H, 22H, 33H, 40H   ; Define a byte array named NUMBERS containing hexadecimal values

EVEN_NUMS DB 5 DUP(?)    ; Reserve 5 bytes of uninitialized memory for EVEN_NUMS

ODD_NUMS DB 5 DUP(2)     ; Reserve 5 bytes initialized with the value 2 for ODD_NUMS

COUNT EQU 05H            ; Define a constant named COUNT with the value 05H (5 in decimal)

.CODE          ; Start of code segment

MAIN PROC      ; Start of the main procedure
    
    MOV AX, @DATA   ; Move the address of the data segment into AX
    MOV DS, AX      ; Move the contents of AX into the data segment register DS

    LEA SI, NUMBERS ; Load the effective address of NUMBERS into SI

    LEA DI, EVEN_NUMS ; Load the effective address of EVEN_NUMS into DI

    LEA BX, ODD_NUMS  ; Load the effective address of ODD_NUMS into BX

    MOV CL, COUNT     ; Move the value of COUNT (5) into the CL register, to use as a counter

SEPARATE:             ; Label for the start of the loop
    MOV AL, [SI]      ; Move the byte pointed to by SI into AL

    TEST AL, 1        ; Test if the least significant bit of AL is set (i.e., test if AL is odd)

    JZ STORE_EVEN     ; If the zero flag is set (AL is even), jump to STORE_EVEN
    MOV [BX], AL      ; Otherwise, move the value in AL to the address pointed to by BX
    INC BX            ; Increment BX to point to the next location in ODD_NUMS

    JMP NEXT_NUM      ; Jump to NEXT_NUM to process the next number

STORE_EVEN:           ; Label for storing even numbers
    MOV [DI], AL      ; Move the value in AL to the address pointed to by DI
    INC DI            ; Increment DI to point to the next location in EVEN_NUMS

NEXT_NUM:             ; Label for processing the next number
    INC SI            ; Increment SI to point to the next byte in NUMBERS
    DEC CL            ; Decrement the counter CL
    JNZ SEPARATE      ; If CL is not zero, jump to SEPARATE to process the next number

    MOV AH, 4CH       ; Move the terminate program function code (4CH) into AH

    INT 21H           ; Interrupt 21H to terminate the program

MAIN ENDP     ; End of the main procedure

END MAIN      ; End of the program


output:


explaination:

I'd be glad to explain the assembly code you provided:

Functionality:

This code sorts a byte array (NUMBERS) into two separate arrays (EVEN_NUMS and ODD_NUMS) based on the least significant bit (LSB) of each byte. Even numbers have an LSB of 0, while odd numbers have an LSB of 1.

Memory Model and Segments:

  • .MODEL SMALL: Specifies the "small" memory model for managing memory allocation.

Data Segment:

  • NUMBERS DB 10H, 15H, 22H, 33H, 40H: Defines a byte array named NUMBERS containing 5 hexadecimal values (replace with your actual data).
  • EVEN_NUMS DB 5 DUP (?): Defines a byte array named EVEN_NUMS with 5 empty bytes (represented by ?) to store even numbers.
  • ODD_NUMS DB 5 DUP (2): Defines a byte array named ODD_NUMS with 5 bytes initialized to 2 (arbitrary value, often used as a placeholder). This is likely a mistake, as it should be DUP (?) similar to EVEN_NUMS to store odd numbers.
  • COUNT EQU 05H: Defines a constant named COUNT with the value 5, representing the number of elements in the NUMBERS array.

Code Segment:

MAIN Procedure:

  • MOV AX, @DATA: Loads the address of the data segment into the AX register.
  • MOV DS, AX: Sets the data segment register (DS) to access the data segment.
  • LEA SI, NUMBERS: Loads the effective address of the NUMBERS array into the SI register for easier access.
  • LEA DI, EVEN_NUMS: Loads the effective address of the EVEN_NUMS array into the DI register for storing even numbers.
  • LEA BX, ODD_NUMS: Loads the effective address of the ODD_NUMS array into the BX register for storing odd numbers.
  • MOV CL, COUNT: Moves the value of COUNT (5) into the CL register to use as a loop counter.

SEPARATE Label (Sorting Loop):

  • MOV AL, [SI]: Moves the value of the current element in the NUMBERS array (pointed to by SI) into the AL register.
  • TEST AL, 1: Performs a bitwise AND operation between AL and 1 (isolates the LSB).
  • JZ STORE_EVEN: Jumps to STORE_EVEN if the zero flag (ZF) is set (LSB is 0, even number).

Storing Even Numbers:

  • STORE_EVEN: (Label for even numbers)
  • MOV [DI], AL: Moves the even number from AL to the memory location pointed to by DI (current position in EVEN_NUMS).
  • INC DI: Increments the DI register to point to the next position in EVEN_NUMS for the next even number.

Unconditional Jump (Mistake):

  • JMP NEXT_NUM: This jump instruction is likely a mistake. It unconditionally jumps to NEXT_NUM, regardless of whether the number is even or odd. This might have been intended as a conditional jump based on the parity (even/odd).

Common Correction (Assuming Conditional Jump):

  • Replace JMP NEXT_NUM with a conditional jump based on the zero flag (ZF) set by the TEST instruction:
    • JNZ NEXT_NUM (Jump if Not Zero - for odd numbers)

Storing Odd Numbers:

  • NEXT_NUM: (Label for both even and odd number processing)
  • MOV [BX], AL: Moves the number from AL (even or odd depending on the jump) to the memory location pointed to by BX (current position in ODD_NUMS). In the original code with an unconditional jump, all numbers would be stored here (incorrect).
  • INC BX: Increments the BX register to point to the next position in ODD_NUMS for the next number.

Loop and Termination:

  • INC SI: Increments the SI register to point to the next element in the NUMBERS array.
  • DEC CL: Decrements the loop counter (CL).
  • JNZ SEPARATE: Jumps back to SEPARATE if the counter is not zero (continues the loop for remaining elements).
  • MOV AH, 4CH: Loads the system call code for program termination into the AH register.
  • INT 21H: Triggers an interrupt (INT 21H)



2.Write an ALP to separate positive and negative numbers


code:
.MODEL SMALL
.STACK 100h
.DATA
nums DB -5, 7, -3, 8, -2, 0, 4, -1, 3, -6  ; Array of numbers
pos DB 10 DUP('?')                        ; Array to store positive numbers
neg DB 10 DUP('?')                        ; Array to store negative numbers
newline DB 0Dh, 0Ah, '$'                  ; Newline for printing

.CODE
main PROC
    MOV AX, @DATA
    MOV DS, AX
    MOV ES, AX

    LEA SI, nums         ; Load address of the nums array into SI
    LEA DI, pos          ; Load address of the pos array into DI
    LEA BX, neg          ; Load address of the neg array into BX
    MOV CX, 10           ; Set loop counter to the length of nums array

check_num:
    MOV AL, [SI]         ; Load the current number in nums into AL
    CMP AL, 0            ; Compare AL with 0
    JL negative          ; If number is less than 0, jump to negative
    JGE positive         ; If number is greater or equal to 0, jump to positive

positive:
    MOV [DI], AL         ; Move number to pos array
    INC DI               ; Increment pos array index
    JMP next_num         ; Jump to next_num to process the next number

negative:
    MOV [BX], AL         ; Move number to neg array
    INC BX               ; Increment neg array index

next_num:
    INC SI               ; Move to the next number in nums
    LOOP check_num       ; Loop back to check_num if CX is not 0

    MOV BYTE PTR [DI], '$' ; Null-terminate the pos array
    MOV BYTE PTR [BX], '$' ; Null-terminate the neg array

    ; Print positive numbers
    LEA DX, pos          ; Load address of pos into DX
    MOV AH, 09h          ; Set AH to 09h (print string function)
    INT 21h              ; Call DOS interrupt

    LEA DX, newline      ; Load address of newline into DX
    MOV AH, 09h          ; Set AH to 09h (print string function)
    INT 21h              ; Call DOS interrupt

    ; Print negative numbers
    LEA DX, neg          ; Load address of neg into DX
    MOV AH, 09h          ; Set AH to 09h (print string function)
    INT 21h              ; Call DOS interrupt

    LEA DX, newline      ; Load address of newline into DX
    MOV AH, 09h          ; Set AH to 09h (print string function)
    INT 21h              ; Call DOS interrupt

    MOV AX, 4C00h        ; Set AX to 4C00h (terminate program function)
    INT 21h              ; Call DOS interrupt to terminate the program

main ENDP
END main

output:





This assembly code sorts an array of numbers into positive and negative numbers and then prints them. Here's a detailed explanation:

Model and Stack:

  • .MODEL SMALL: Specifies the "small" memory model for managing memory allocation.
  • .STACK 100h: Allocates 100h (256 bytes) of memory for the stack, used for function calls and temporary storage.

Data Segment:

  • nums DB -5, 7, -3, 8, -2, 0, 4, -1, 3, -6: Defines a byte array named nums containing 10 signed numbers (replace with your actual data).
  • pos DB 10 DUP (?): Defines a byte array named pos with 10 empty bytes (represented by ?) to store positive numbers.
  • neg DB 10 DUP (?): Defines a byte array named neg with 10 empty bytes (represented by ?) to store negative numbers.
  • newline DB 0Dh, 0Ah, '$': Defines a byte array named newline containing a carriage return (0Dh), line feed (0Ah), and a dollar sign ($) for printing a new line.

Code Segment:

main Procedure:

  • MOV AX, @DATA: Loads the address of the data segment into the AX register.
  • MOV DS, AX: Sets the data segment register (DS) to access the data segment for nums.
  • MOV ES, AX: Sets the extra segment register (ES) to access the data segment for pos and neg (they could share the same segment as nums if data is small).
  • LEA SI, nums: Loads the effective address of the nums array into the SI register for easier access.
  • LEA DI, pos: Loads the effective address of the pos array into the DI register for storing positive numbers.
  • LEA BX, neg: Loads the effective address of the neg array into the BX register for storing negative numbers.
  • MOV CX, 10: Moves the length of the nums array (10) into the CX register as a loop counter.

check_num Label (Sorting Loop):

  • MOV AL, [SI]: Moves the current number from the nums array (pointed to by SI) into the AL register.
  • CMP AL, 0: Compares the value in AL with 0.
  • JL negative: Jumps to the negative label if the number is less than 0 (negative).
  • JGE positive: Jumps to the positive label if the number is greater than or equal to 0 (positive).

positive Label (Storing Positive Numbers):

  • MOV [DI], AL: Moves the positive number from AL to the memory location pointed to by DI (current position in pos).
  • INC DI: Increments the DI register to point to the next position in pos for the next positive number.
  • JMP next_num: Jumps to the next_num label to process the next number.

negative Label (Storing Negative Numbers):

  • MOV [BX], AL: Moves the negative number from AL to the memory location pointed to by BX (current position in neg).
  • INC BX: Increments the BX register to point to the next position in neg for the next negative number.

next_num Label:

  • INC SI: Increments the SI register to point to the next number in the nums array.
  • LOOP check_num: Decrements the loop counter (CX) and jumps back to check_num if not zero (continues the loop for remaining elements).

Null-Terminating Arrays (Optional):

  • MOV BYTE PTR [DI], '$': This line is optional and adds a dollar sign ($) at the end of the pos array to mark its termination (useful for some string printing functions).
  • MOV BYTE PTR [BX], '$': This line is optional and adds a dollar sign ($) at the end of the neg array, similar to pos.

Printing Positive and Negative Numbers:

  • LEA DX, pos: Loads the address of the pos array into the DX register (for printing).
  • MOV AH, 09h: Sets the AH register to 09h, which is the system call code for printing strings (DOS function).
  • INT 21h: Triggers an interrupt (INT 21h) to the operating system, calling the DOS function to print the pos array as a string



3.Write an ALP to find logical ones and zeros in a given data

code:
.MODEL SMALL
.STACK 100H
.DATA
NUM DB 10101101B         ; The binary number to analyze
COUNT_1 DB 0             ; Counter for number of 1's
COUNT_0 DB 0             ; Counter for number of 0's
MSG1 DB 'NUMBER OF 1S:$' ; Message to display number of 1's
MSG2 DB 'NUMBER OF 0S:$' ; Message to display number of 0's

.CODE
START:
    MOV AX, @DATA        ; Load the address of the data segment into AX
    MOV DS, AX           ; Move the address into the data segment register DS

    MOV AL, NUM          ; Load the binary number into AL
    MOV CL, 8            ; Set the loop counter to 8 (number of bits)
    MOV COUNT_1, 0       ; Initialize the counter for 1's to 0
    MOV COUNT_0, 0       ; Initialize the counter for 0's to 0

NEXT_BIT:
    ROR AL, 1            ; Rotate the bits in AL right by 1 position
    JC IS_ONE            ; If the carry flag is set, the bit was a 1

IS_ZERO:
    INC COUNT_0          ; Increment the counter for 0's
    JMP CHECK_COUNT      ; Jump to CHECK_COUNT

IS_ONE:
    INC COUNT_1          ; Increment the counter for 1's

CHECK_COUNT:
    DEC CL               ; Decrement the loop counter
    JNZ NEXT_BIT         ; If the counter is not zero, repeat the loop

    ; Display the number of 1's
    LEA DX, MSG1         ; Load the address of MSG1 into DX
    MOV AH, 09H          ; Set AH to 09H (print string function)
    INT 21H              ; Call DOS interrupt to print the message

    MOV AL, COUNT_1      ; Load the count of 1's into AL
    ADD AL, 30H          ; Convert the binary number to ASCII
    MOV DL, AL           ; Move the ASCII character into DL
    MOV AH, 02H          ; Set AH to 02H (print character function)
    INT 21H              ; Call DOS interrupt to print the character

    ; Print a newline
    MOV AH, 02H
    MOV DL, 0DH
    INT 21H
    MOV DL, 0AH
    INT 21H

    ; Display the number of 0's
    LEA DX, MSG2         ; Load the address of MSG2 into DX
    MOV AH, 09H          ; Set AH to 09H (print string function)
    INT 21H              ; Call DOS interrupt to print the message

    MOV AL, COUNT_0      ; Load the count of 0's into AL
    ADD AL, 30H          ; Convert the binary number to ASCII
    MOV DL, AL           ; Move the ASCII character into DL
    MOV AH, 02H          ; Set AH to 02H (print character function)
    INT 21H              ; Call DOS interrupt to print the character

    ; Terminate the program
    MOV AH, 4CH          ; Set AH to 4CH (terminate program function)
    INT 21H              ; Call DOS interrupt to terminate the program

END START

Output:

explaination:

This assembly code analyzes a binary number and counts the occurrences of 1's and 0's in the number. Here's a breakdown:

Model and Stack:

  • .MODEL SMALL: Specifies the "small" memory model for memory management.
  • .STACK 100H: Allocates 100h (256 bytes) of memory for the stack, used for function calls and temporary storage.

Data Segment:

  • NUM DB 10101101B: Defines a byte containing the binary number 10101101 (replace with your actual binary number).
  • COUNT_1 DB 0: Defines a byte variable named COUNT_1 initialized to 0 to store the number of 1's found.
  • COUNT_0 DB 0: Defines a byte variable named COUNT_0 initialized to 0 to store the number of 0's found.
  • MSG1 DB 'NUMBER OF 1S:$': Defines a byte array containing a message to display for the number of 1's.
  • MSG2 DB 'NUMBER OF 0S:$': Defines a byte array containing a message to display for the number of 0's.

Code Segment:

START Label:

  • MOV AX, @DATA: Loads the address of the data segment into the AX register.
  • MOV DS, AX: Sets the data segment register (DS) to access the data segment for variables.
  • MOV AL, NUM: Loads the binary number from NUM into the AL register.
  • MOV CL, 8: Initializes a counter (CL) with the value 8, representing the number of bits in the binary number.
  • MOV COUNT_1, 0: Initializes the COUNT_1 variable to 0.
  • MOV COUNT_0, 0: Initializes the COUNT_0 variable to 0.

NEXT_BIT Label (Looping through Bits):

  • ROR AL, 1: Performs a right rotate operation on the value in AL by 1 bit position. This effectively shifts the bits in AL to the right, with the least significant bit (LSB) being moved to the carry flag (CF).
  • JC IS_ONE: Jumps to IS_ONE if the carry flag (CF) is set (meaning the LSB was a 1).

IS_ZERO Label (Handling 0 Bit):

  • This label is not directly jumped to in this code. If the ROR operation didn't set the carry flag (meaning the LSB was a 0), execution would continue here by default.
  • INC COUNT_0: Increments the COUNT_0 variable to count the 0 bit.
  • JMP CHECK_COUNT: Jumps to CHECK_COUNT to check the loop counter.

IS_ONE Label (Handling 1 Bit):

  • INC COUNT_1: Increments the COUNT_1 variable to count the 1 bit.

CHECK_COUNT Label:

  • DEC CL: Decrements the loop counter (CL).
  • JNZ NEXT_BIT: Jumps back to NEXT_BIT if the loop counter is not zero (continues the loop for remaining bits).

Printing Results:

  • LEA DX, MSG1: Loads the address of the MSG1 message into the DX register for printing.

  • MOV AH, 09H: Sets the AH register to 09h, which is the system call code for printing strings (DOS function).

  • INT 21H: Triggers an interrupt (INT 21h) to the operating system, calling the DOS function to print the MSG1 message.

  • Similar steps are followed to print the count of 1's (converted to ASCII), a newline, the MSG2 message, and the count of 0's (converted to ASCII).

Program Termination:

  • MOV AH, 4CH: Sets the AH register to 4CH, which is the system call code for program termination (DOS function).
  • INT 21H: Triggers an interrupt (INT 21h) to the operating system, terminating the program.

In summary:

This assembly code iterates through each bit in the binary number using bitwise right rotations. It keeps track of the number of 1's and 0's encountered and then prints the counts along with messages using DOS functions.



4.Write an ALP to find whether the given code belongs 2 out of 5 codes

or not.
code:
.MODEL SMALL
.STACK 100H
.DATA
N DB ?               ; Variable to store the input number
MSG2 DB 0AH, 0DH, 'VALID CODE$', 0AH, 0DH ; Message for valid code
MSG3 DB 0AH, 0DH, 'INVALID CODE$', 0AH, 0DH ; Message for invalid code

.CODE
MAIN PROC
    MOV AX, @DATA   ; Load the address of the data segment into AX
    MOV DS, AX      ; Move the address into the data segment register DS

    MOV AH, 01H     ; Set AH to 01H (input a character)
    INT 21H         ; Call DOS interrupt to input a character
    SUB AL, '0'     ; Convert ASCII to numeric value
    MOV N, AL       ; Store the input number in N

    MOV AL, N       ; Load the input number into AL
    AND AL, 0E0H    ; Mask the upper 3 bits (isolate the first 3 bits)
    JNZ NOT_CODE    ; If the result is not zero, jump to NOT_CODE

    MOV BL, 0       ; Clear BL (counter for set bits)
    MOV AL, N       ; Load the input number into AL
    MOV CX, 0005H   ; Set the loop counter to 5 (5 bits to check)

BACK:
    ROR AL, 1       ; Rotate the bits in AL right by 1 position
    JNC SKIP        ; If the carry flag is not set, skip incrementing BL
    INC BL          ; Increment BL (if a set bit is found)
SKIP:
    DEC CX          ; Decrement the loop counter
    JNZ BACK        ; If the counter is not zero, repeat the loop

    CMP BL, 02H     ; Compare BL with 2 (we need exactly 2 set bits)
    JNZ NOT_CODE    ; If the count is not 2, jump to NOT_CODE

    ; Display valid code message
    MOV DX, OFFSET MSG2  ; Load the address of MSG2 into DX
    MOV AH, 09H          ; Set AH to 09H (print string function)
    INT 21H              ; Call DOS interrupt to print the message
    JMP EXIT             ; Jump to exit

NOT_CODE:
    ; Display invalid code message
    MOV DX, OFFSET MSG3  ; Load the address of MSG3 into DX
    MOV AH, 09H          ; Set AH to 09H (print string function)
    INT 21H              ; Call DOS interrupt to print the message

EXIT:
    MOV AH, 4CH      ; Set AH to 4CH (terminate program function)
    INT 21H          ; Call DOS interrupt to terminate the program

MAIN ENDP
END MAIN

output:


explaination:

This assembly code checks if a user-entered 5-bit code is valid based on a specific criterion: exactly two bits must be set (1) and the remaining three bits must be cleared (0). Here's a detailed explanation:

Model and Stack:

  • .MODEL SMALL: Specifies the "small" memory model for memory management.
  • .STACK 100H: Allocates 100h (256 bytes) of memory for the stack, used for function calls and temporary storage.

Data Segment:

  • N DB ?: Defines a byte variable named N to store the user's input number (initially empty).
  • MSG2 DB 0AH, 0DH, 'VALID CODE$', 0AH, 0DH: Defines a byte array containing a message to display for a valid code.
  • MSG3 DB 0AH, 0DH, 'INVALID CODE$', 0AH, 0DH: Defines a byte array containing a message to display for an invalid code.

Code Segment:

MAIN Procedure:

  • MOV AX, @DATA: Loads the address of the data segment into the AX register.

  • MOV DS, AX: Sets the data segment register (DS) to access the data segment for variables.

  • User Input:

    • MOV AH, 01H: Sets the AH register to 01h, which is the system call code for getting a character from the keyboard (DOS function).
    • INT 21H: Triggers an interrupt (INT 21h) to the operating system, calling the DOS function to read a character.
    • SUB AL, '0': Converts the ASCII character (assumed to be a number key) to a numeric value by subtracting the ASCII code of '0'. This assumes the user enters a number between 0 and 9.
    • MOV N, AL: Stores the converted number in the N variable.
  • Checking Top 3 Bits (Validation Step 1):

    • MOV AL, N: Loads the input number into the AL register.
    • AND AL, 0E0H: Performs a bitwise AND operation with a mask 0E0H (11100000B) to isolate the upper 3 bits of the input number.
    • JNZ NOT_CODE: Jumps to NOT_CODE (invalid code) if the result of the AND operation is not zero (meaning at least one of the top 3 bits is set). This ensures the top 3 bits are all cleared (0).
  • Counting Set Bits (Validation Step 2):

    • MOV BL, 0: Initializes a byte variable BL to 0 as a counter for set bits.
    • MOV AL, N: Loads the input number into AL again.
    • MOV CX, 0005H: Sets a loop counter CX to 5, representing the 5 bits to be checked in the input number.
  • BACK Label (Looping through Bits):

    • ROR AL, 1: Performs a right rotate operation on the value in AL by 1 bit position. This effectively shifts the bits to the right, making it easier to check for set bits.
    • JNC SKIP: Jumps to SKIP if the carry flag (CF) is not set (meaning the LSB was a 0).
    • INC BL: If the CF is set (LSB was a 1), increments the BL counter for set bits.
  • SKIP Label:

    • DEC CX: Decrements the loop counter.
    • JNZ BACK: Jumps back to BACK if the loop counter is not zero (continues the loop for remaining bits).
  • Checking for Exactly Two Set Bits:

    • CMP BL, 02H: Compares the counter BL with 2. We need exactly two bits to be set.
    • JNZ NOT_CODE: Jumps to NOT_CODE (invalid code) if the count is not equal to 2.
  • Valid Code Message:

    • MOV DX, OFFSET MSG2: Loads the address of the MSG2 message into the DX register for printing.
    • MOV AH, 09H: Sets the AH register to 09h, which is the system call code for printing strings (DOS function).
    • INT 21H: Triggers an interrupt (INT 21h) to the operating system, calling the DOS function to print the "VALID CODE" message.
    • JMP EXIT: Jumps to EXIT to terminate the program.




5.Write an ALP to check bitwise palindrome or not Write an ALP to check
whether the given number is palindrome or not.


code:

.MODEL SMALL
.DATA
NUMBER DB 09h          ; The number to check for being a palindrome in binary form
IS_PALINDROME DB ?     ; Variable to store the result (1 if palindrome, 0 if not)

.CODE
MAIN PROC
    MOV AX, @DATA      ; Load the address of the data segment into AX
    MOV DS, AX         ; Move the address into the data segment register DS

    MOV AL, NUMBER     ; Load the number into AL
    MOV BL, AL         ; Copy the number into BL
    MOV CL, 8          ; Set the loop counter to 8 (number of bits)
    XOR DL, DL         ; Clear DL (will hold the reversed bits)

REVERSE_BITS:
    RCR BL, 1          ; Rotate bits of BL right through the carry flag
    RCL DL, 1          ; Rotate bits of DL left through the carry flag
    DEC CL             ; Decrement the loop counter
    JNZ REVERSE_BITS   ; If CL is not zero, repeat the loop

    CMP AL, DL         ; Compare the original number with the reversed bits
    JNE NOT_PALINDROME ; If they are not equal, jump to NOT_PALINDROME

    MOV IS_PALINDROME, 1 ; If equal, set IS_PALINDROME to 1 (true)
    JMP END_PROG       ; Jump to end of the program

NOT_PALINDROME:
    MOV IS_PALINDROME, 0 ; If not equal, set IS_PALINDROME to 0 (false)

END_PROG:
    MOV AH, 4CH        ; Set AH to 4CH (terminate program function)
    INT 21H            ; Call DOS interrupt to terminate the program

MAIN ENDP
END MAIN

OUTPUT:


EXPLAINATION:

This assembly code checks if a binary number is a palindrome. A palindrome is a number or phrase that reads the same backward as forward. In this case, it checks if the binary representation of the number in NUMBER is the same when the bits are reversed.

Here's a breakdown of the code:

Model and Stack:

  • .MODEL SMALL: Specifies the "small" memory model for memory management.

Data Segment:

  • NUMBER DB 09h: Defines a byte variable named NUMBER containing the binary number to be checked (replace 09h with your actual binary number).
  • IS_PALINDROME DB ?: Defines a byte variable named IS_PALINDROME to store the result (1 if the number is a palindrome, 0 otherwise).

Code Segment:

MAIN Procedure:

  • MOV AX, @DATA: Loads the address of the data segment into the AX register.

  • MOV DS, AX: Sets the data segment register (DS) to access the data segment for variables.

  • Setting Up:

    • MOV AL, NUMBER: Loads the binary number from NUMBER into the AL register.
    • MOV BL, AL: Copies the number into the BL register for manipulation.
    • MOV CL, 8: Sets a loop counter CL to 8, representing the number of bits in the byte (assuming an 8-bit binary number).
    • XOR DL, DL: Clears the DL register (will hold the reversed bits).
  • REVERSE_BITS Label (Reversing Bits):

    • RCR BL, 1: Performs a right rotate with carry operation on BL. This shifts the bits of BL right by 1 position, and the least significant bit (LSB) is moved to the carry flag (CF).
    • RCL DL, 1: Performs a rotate with carry left operation on DL. The carry flag (CF) set from the previous instruction is used to shift the most significant bit (MSB) of DL into its LSB position. This effectively builds the reversed bit pattern in DL.
    • DEC CL: Decrements the loop counter.
    • JNZ REVERSE_BITS: Jumps back to REVERSE_BITS if the loop counter is not zero (continues reversing bits for all 8 positions).
  • Checking for Palindrome:

    • CMP AL, DL: Compares the original number in AL with the reversed bits in DL.
    • JNE NOT_PALINDROME: Jumps to NOT_PALINDROME if the original number and reversed bits are not equal (meaning not a palindrome).
  • Setting Result:

    • MOV IS_PALINDROME, 1: If the comparison is equal, sets the IS_PALINDROME variable to 1 (true).
  • Ending the Program (Success):

    • JMP END_PROG: Jumps to the program termination section.
  • NOT_PALINDROME Label (Number Not a Palindrome):

    • MOV IS_PALINDROME, 0: If the comparison failed, sets the IS_PALINDROME variable to 0 (false).
  • END_PROG Label (Program Termination):

    • MOV AH, 4CH: Sets the AH register to 4CH, which is the system call code for program termination (DOS function).
    • INT 21H: Triggers an interrupt (INT 21h) to the operating system, calling the DOS function to terminate the program.

MAIN ENDP: Marks the end of the MAIN procedure.END MAIN: Marks the end of the entire program.





Post a Comment

0 Comments