Assembly Code Snippet
  • Assembly Code Snippet
  • Assembly Program: 4-Digit Decimal Number Addition
  • ASCII Division and Display in x86 Assembly
  • ASCII Arithmetic Addition in x86 Assembly
  • BCD Addition and Display in Assembly
  • BCD Arithmetic and Display in Assembly
  • Decimal Multiplication and Display in x86 Assembly
  • Assembly Program: Determining Endianness and Displaying Messages
  • Assembly Program: Determining Even and Odd Numbers
  • Assembly Program: Displaying Messages using Syscalls
  • Assembly Program: Finding and Printing the Largest Word in a Sentence
  • Assembly Program: Finding the Largest of Three Numbers
  • Displaying "Hello World!" in x86 Assembly
  • Increment and Display Digits in Assembly
  • Input and Display User Name in Linux Assembly
  • Input and Display User Number in Linux Assembly
  • Introduction to Linux Assembly Programming with Messages
  • Assembly Program: Demonstrating Loop and Testing
  • Changing and Displaying Names in Assembly
  • Selection Sort from Minimum to Maximum
  • Assembly Program: Sentence Word Deletion
  • Sum and Read Input Numbers:
  • Two-Digit Sum and Square - Assembly Program
  • Calculate and Display Sum
  • Sorting Numerical Elements (BubbleSort)
    • Assembly Program: Sorting Numerical Elements
Powered by GitBook
On this page
  • source code
  • Code Explanation
  • Example
  • Usage

Was this helpful?

Edit on GitHub

Assembly Program: Determining Even and Odd Numbers

This assembly program determines whether a given number is even or odd using bitwise operations and displays the result. It utilizes Linux syscalls for I/O operations and program termination.

source code

; This assembly program determines whether a given number is even or odd using bitwise operations and displays the result.

section .text
   global _start            ; Must be declared for using GCC
	
_start:                     ; Tell linker entry point
   mov   ax, 8h             ; Load 8 into the ax register
   and   ax, 1              ; Perform bitwise AND with 1
   jz    evnn               ; Jump to 'evnn' if result is zero (even)
   mov   eax, 4             ; System call number (sys_write)
   mov   ebx, 1             ; File descriptor (stdout)
   mov   ecx, odd_msg       ; Message to write
   mov   edx, len2          ; Length of message
   int   0x80               ; Call kernel
   jmp   outprog            ; Jump to program exit

evnn:
   mov   ah, 09h            ; Set 'ah' register for displaying even message
   mov   eax, 4             ; System call number (sys_write)
   mov   ebx, 1             ; File descriptor (stdout)
   mov   ecx, even_msg      ; Message to write
   mov   edx, len1          ; Length of message
   int   0x80               ; Call kernel

outprog:
   mov   eax, 1             ; System call number (sys_exit)
   int   0x80               ; Call kernel

section .data
even_msg  db  'Even Number!' ; Message showing even number
len1  equ  $ - even_msg      ; Length of even_msg
   
odd_msg db  'Odd Number!'    ; Message showing odd number
len2  equ  $ - odd_msg       ; Length of odd_msg

Code Explanation

The provided assembly code performs the following steps:

  1. Checking Even or Odd:

    • Loads the number 8 into the ax register.

    • Performs a bitwise AND operation with 1 to check the least significant bit (LSB).

    • If the LSB is 0, it jumps to the evnn label, indicating an even number.

    • If the LSB is 1, it displays the message for an odd number.

  2. Displaying Even Number (evnn):

    • Sets the ah register to 09h, indicating the display of an even number.

    • Displays the message "Even Number!" using syscalls.

  3. Displaying Odd Number (outprog):

    • Displays the message "Odd Number!" using syscalls.

  4. Program Termination:

    • Uses syscall 1 (sys_exit) to exit the program.

Example

Suppose you run the program with the command-line argument ./evenodd 7.

Output:

Odd Number!

In this example, the program determines that the number 7 is odd and displays "Odd Number!".

Usage

  1. Ensure you are running this code on a Linux environment or emulator that supports the syscalls used in the code.

  2. Assemble the code using an appropriate assembler (e.g., NASM) and linker (e.g., LD).

  3. Run the assembled binary to execute the program. You can provide a number as a command-line argument.

  4. Observe the output, which will indicate whether the given number is even or odd.

Note: Assembly language can interact with system components. Make sure you understand the code before running it, especially when it involves system calls.


PreviousAssembly Program: Determining Endianness and Displaying MessagesNextAssembly Program: Displaying Messages using Syscalls

Last updated 1 year ago

Was this helpful?