Assembly question: About displaying even nos

Posted on 2004-11-21
Last Modified: 2008-03-04
I need to enter 10 positive integers and display only the even nos from the 10 nos entered.
I cant use .IF,.WHILE,.REPEAT directives
Question by:Randaferns
    LVL 11

    Expert Comment

    So what is the problem ? Please post your code that doesn't work.

    Author Comment

    I was wondering if anyone could help i just cant do it..i mean taking the integers in is this is alli could new to assembly

    TITLE Even nos       (Evennos.asm)


    prompt BYTE "Enter 10 positive integers: ",0
    buffer  BYTE 50 dup(0)

    main PROC
    ; listing the evens nos from the postive integers added.:
    ; Ask the user to input 10 postive integers:
          call Clrscr                    ; clear screen
          mov  edx,OFFSET prompt            ; "Enter 10 postive integers: "
          call WriteString

    main ENDP
    END main

    LVL 11

    Expert Comment

    It is against E-E policy to do homeworks for students. However, there is no any reason not to help you to find the solution.
    So what is the problem ?
    You do not understand the algorithm how to check if number is even ?
    The simplest way in assembler to 'and' it with 01h and to check if the result is 0 or 1. In case of 1 the number is odd...

    Author Comment

    well..thanks dimitry..
    obviously was not expecting the entire thing..just needed help with the algorithm
    i have a basic idea that i can use compare to MOD 2 and check..but if u could help me out a bit with the algorithm, it would be really nice..

    LVL 11

    Accepted Solution

    Usually in C people are doing:
    if( (num % 2) == 0 )
      printf("Even number: %d\n",num);

    However, % - MOD operation is VERY heavy and in case of '% 2' is obsolete.
    You need to do:
    if( (num & 0x1) == 0 )
      printf("Even number: %d\n",num);

    Why it is Ok ?
    In binary presentation even number will be like 'bbbbbbbbb....bbb0' and odd will be 'bbbbbbbbb...bbb1'.
    So checking last bit for 1 gives you ability to distinguish between even and odd number.
    Now, in x86 assembler there is test command that will do 'and' without changing the number itself.

    So assuming that 'si' contains pointer to array element then you need to do:
      tst [si], 01h
      jnz oddNum
      ... print .. [si] number

    Author Comment

    Hi dimitry
    thanks that was really helpful..
    I'll use the test instruction and check

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now