🏁
T9
/
5️⃣
Sem 5
/
Practicals
Practicals
/
MI
MI
/
Lab 7

Lab 7

Lab 7 : Programming based on Bit Manipulation
Objective
To familiar with bit manipulation methods.
Prelab
Read Instructions related to Logical and shift the operands.
Bit Manipulation instructions
Instructions
Operands
Description
AND
• REG, memory • memory, REG • REG, REG • memory,immediate • REG, immediate
Logical AND between all bits of two operands. Result is stored in operand1. These rules apply: 1 AND 1 = 1; 1 AND 0 = 0 0 AND 1 = 0; 0 AND 0 = 0
OR
• REG, memory • memory, REG • REG, REG • memory, immediate • REG, immediate
Logical OR between all bits of two operands. Result is stored in first operand. These rules apply: 1 OR 1 = 1; 1 OR 0 = 1 0 OR 1 = 1; 0 OR 0 = 0
XOR
• REG, memory • memory, REG • REG, REG • memory, immediate • REG, immediate
Logical XOR (Exclusive OR) between all bits of two operands. Result is stored in first operand. These rules apply: 1 XOR 1 = 0; 1 XOR 0 = 1 0 XOR 1 = 1; 0 XOR 0 = 0
SHL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Shift Left. Shift operand1 Left. The number of shifts is set by operand2. Algorithm: • Shift all bits left, the bit that goes off is set to CF.  Zero bit is inserted to the right-most position.
SHR
• memory, immediate • REG, immediate • memory, CL • REG, CL
Shift Right. Shift operand1 Right. The number of shifts is set by operand2. Algorithm: • Shift all bits right, the bit that goes off is set to CF.  • Zero bit is inserted to the left-most position.
ROL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate Left. Rotate operand1 left. The number of rotates is set by operand2. Algorithm: Shift all bits left, the bit that goes off is set to CF and the same bit is inserted to the right-most position.
RCL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate operand1 left through Carry Flag. The number of rotates is set by operand2. Algorithm: Shift all bits left, the bit that goes off is set to CF and previous value of CF is inserted to the right-most position.  Example: STC                        ; set carry (CF=1). MOV AL, 1Ch       ; AL = 00011100b RCL AL, 1             ; AL = 00111001b, CF=0. RET OF=0 if first operand keeps original sign.
ROR
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate Right. Rotate operand1 right. The number of rotates is set by operand2. Algorithm: Shift all bits right, the bit that goes off is set to CF and the same bit is inserted to the left-most position.
Exercises
1) Write a program to check if given data is positive or negative.
section .data
    prompt db "Enter a number: ", 0
    positive_msg db "The number is positive.", 0
    negative_msg db "The number is negative.", 0
    zero_msg db "The number is zero.", 0

section .bss
    num resb 4

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov eax, [num]
    sub eax, '0'

    ; Check if positive, negative, or zero
    cmp eax, 0
    jg positive
    jl negative
    je zero

positive:
    mov ecx, positive_msg
    mov edx, 23
    jmp print

negative:
    mov ecx, negative_msg
    mov edx, 23
    jmp print

zero:
    mov ecx, zero_msg
    mov edx, 19

print:
    mov eax, 4
    mov ebx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80
2) Write a program to check if given data is odd or even.
section .data
    prompt db "Enter a number: ", 0
    odd_msg db "The number is odd.", 0
    even_msg db "The number is even.", 0

section .bss
    num resb 4

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov eax, [num]
    sub eax, '0'

    ; Check if odd or even
    test al, 1
    jz even

odd:
    mov ecx, odd_msg
    mov edx, 18
    jmp print

even:
    mov ecx, even_msg
    mov edx, 19

print:
    mov eax, 4
    mov ebx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80
3) Write a program to count the number of 1’s and 0’s in given data.
section .data
    prompt db "Enter a number: ", 0
    ones_msg db "Number of 1's: ", 0
    zeros_msg db "Number of 0's: ", 0

section .bss
    num resb 4
    ones_count resb 1
    zeros_count resb 1

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov al, [num]
    sub al, '0'

    ; Initialize counters
    mov byte [ones_count], 0
    mov byte [zeros_count], 0
    mov cl, 8  ; Loop counter (8 bits)

count_loop:
    shr al, 1  ; Shift right to check least significant bit
    jc increment_ones
    inc byte [zeros_count]
    jmp continue

increment_ones:
    inc byte [ones_count]

continue:
    dec cl
    jnz count_loop

    ; Print results
    mov eax, 4
    mov ebx, 1
    mov ecx, ones_msg
    mov edx, 16
    int 0x80

    mov al, [ones_count]
    add al, '0'
    mov [num], al
    mov eax, 4
    mov ebx, 1
    mov ecx, num
    mov edx, 1
    int 0x80

    mov eax, 4
    mov ebx, 1
    mov ecx, zeros_msg
    mov edx, 17
    int 0x80

    mov al, [zeros_count]
    add al, '0'
    mov [num], al
    mov eax, 4
    mov ebx, 1
    mov ecx, num
    mov edx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80
🏁
T9
/
5️⃣
Sem 5
/
Practicals
Practicals
/
MI
MI
/
Lab 7

Lab 7

Lab 7 : Programming based on Bit Manipulation
Objective
To familiar with bit manipulation methods.
Prelab
Read Instructions related to Logical and shift the operands.
Bit Manipulation instructions
Instructions
Operands
Description
AND
• REG, memory • memory, REG • REG, REG • memory,immediate • REG, immediate
Logical AND between all bits of two operands. Result is stored in operand1. These rules apply: 1 AND 1 = 1; 1 AND 0 = 0 0 AND 1 = 0; 0 AND 0 = 0
OR
• REG, memory • memory, REG • REG, REG • memory, immediate • REG, immediate
Logical OR between all bits of two operands. Result is stored in first operand. These rules apply: 1 OR 1 = 1; 1 OR 0 = 1 0 OR 1 = 1; 0 OR 0 = 0
XOR
• REG, memory • memory, REG • REG, REG • memory, immediate • REG, immediate
Logical XOR (Exclusive OR) between all bits of two operands. Result is stored in first operand. These rules apply: 1 XOR 1 = 0; 1 XOR 0 = 1 0 XOR 1 = 1; 0 XOR 0 = 0
SHL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Shift Left. Shift operand1 Left. The number of shifts is set by operand2. Algorithm: • Shift all bits left, the bit that goes off is set to CF.  Zero bit is inserted to the right-most position.
SHR
• memory, immediate • REG, immediate • memory, CL • REG, CL
Shift Right. Shift operand1 Right. The number of shifts is set by operand2. Algorithm: • Shift all bits right, the bit that goes off is set to CF.  • Zero bit is inserted to the left-most position.
ROL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate Left. Rotate operand1 left. The number of rotates is set by operand2. Algorithm: Shift all bits left, the bit that goes off is set to CF and the same bit is inserted to the right-most position.
RCL
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate operand1 left through Carry Flag. The number of rotates is set by operand2. Algorithm: Shift all bits left, the bit that goes off is set to CF and previous value of CF is inserted to the right-most position.  Example: STC                        ; set carry (CF=1). MOV AL, 1Ch       ; AL = 00011100b RCL AL, 1             ; AL = 00111001b, CF=0. RET OF=0 if first operand keeps original sign.
ROR
• memory, immediate • REG, immediate • memory, CL • REG, CL
Rotate Right. Rotate operand1 right. The number of rotates is set by operand2. Algorithm: Shift all bits right, the bit that goes off is set to CF and the same bit is inserted to the left-most position.
Exercises
1) Write a program to check if given data is positive or negative.
section .data
    prompt db "Enter a number: ", 0
    positive_msg db "The number is positive.", 0
    negative_msg db "The number is negative.", 0
    zero_msg db "The number is zero.", 0

section .bss
    num resb 4

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov eax, [num]
    sub eax, '0'

    ; Check if positive, negative, or zero
    cmp eax, 0
    jg positive
    jl negative
    je zero

positive:
    mov ecx, positive_msg
    mov edx, 23
    jmp print

negative:
    mov ecx, negative_msg
    mov edx, 23
    jmp print

zero:
    mov ecx, zero_msg
    mov edx, 19

print:
    mov eax, 4
    mov ebx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80
2) Write a program to check if given data is odd or even.
section .data
    prompt db "Enter a number: ", 0
    odd_msg db "The number is odd.", 0
    even_msg db "The number is even.", 0

section .bss
    num resb 4

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov eax, [num]
    sub eax, '0'

    ; Check if odd or even
    test al, 1
    jz even

odd:
    mov ecx, odd_msg
    mov edx, 18
    jmp print

even:
    mov ecx, even_msg
    mov edx, 19

print:
    mov eax, 4
    mov ebx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80
3) Write a program to count the number of 1’s and 0’s in given data.
section .data
    prompt db "Enter a number: ", 0
    ones_msg db "Number of 1's: ", 0
    zeros_msg db "Number of 0's: ", 0

section .bss
    num resb 4
    ones_count resb 1
    zeros_count resb 1

section .text
    global _start

_start:
    ; Print prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 18
    int 0x80

    ; Read input
    mov eax, 3
    mov ebx, 0
    mov ecx, num
    mov edx, 4
    int 0x80

    ; Convert ASCII to integer
    mov al, [num]
    sub al, '0'

    ; Initialize counters
    mov byte [ones_count], 0
    mov byte [zeros_count], 0
    mov cl, 8  ; Loop counter (8 bits)

count_loop:
    shr al, 1  ; Shift right to check least significant bit
    jc increment_ones
    inc byte [zeros_count]
    jmp continue

increment_ones:
    inc byte [ones_count]

continue:
    dec cl
    jnz count_loop

    ; Print results
    mov eax, 4
    mov ebx, 1
    mov ecx, ones_msg
    mov edx, 16
    int 0x80

    mov al, [ones_count]
    add al, '0'
    mov [num], al
    mov eax, 4
    mov ebx, 1
    mov ecx, num
    mov edx, 1
    int 0x80

    mov eax, 4
    mov ebx, 1
    mov ecx, zeros_msg
    mov edx, 17
    int 0x80

    mov al, [zeros_count]
    add al, '0'
    mov [num], al
    mov eax, 4
    mov ebx, 1
    mov ecx, num
    mov edx, 1
    int 0x80

    ; Exit program
    mov eax, 1
    xor ebx, ebx
    int 0x80