[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Assembly question: About displaying even nos

Posted on 2004-11-21
6
Medium Priority
?
302 Views
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
0
Comment
Question by:Randaferns
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 12639680
So what is the problem ? Please post your code that doesn't work.
0
 

Author Comment

by:Randaferns
ID: 12639885
I was wondering if anyone could help me..as i just cant do it..i mean taking the integers in is fine..like this is alli could get..am new to assembly

TITLE Even nos       (Evennos.asm)


INCLUDE Irvine32.inc

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


.code
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
      

      exit
main ENDP
END main



0
 
LVL 11

Expert Comment

by:dimitry
ID: 12639918
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...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Randaferns
ID: 12639950
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..

Thanks.
0
 
LVL 11

Accepted Solution

by:
dimitry earned 1500 total points
ID: 12641016
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
oddNum:
0
 

Author Comment

by:Randaferns
ID: 12646256
Hi dimitry
thanks that was really helpful..
I'll use the test instruction and check
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can you run Linux on a Windows system?  Yep.  Here's how.
This article explains how to move an Exchange 2013/2016 mailbox database and logs to a different drive.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Suggested Courses

612 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