The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Zap on May 20, 2009, 09:26:49 AM

Title: need to determine the class of IP address by the 1-st byte only
Post by: Zap on May 20, 2009, 09:26:49 AM
hi,guys,
subj,

i understand it should be smth like this:

dIP BYTE 92     
---
.if  (dIP & 11000000y )
  MsgBox "Class C"
.elseif (dIP & 00000000y )
  MsgBox "Class A"
.elseif  (dIP & 10000000y )
  MsgBox "Class B"
.endif

but it doesnt work properly.
Thanx
Title: Re: need to determine the class of IP address by the 1-st byte only
Post by: Tedd on May 20, 2009, 10:26:52 AM
Consider what actually happens when you have dIP=170 (10101010) -- step through the code by hand and see where you end up.
Then understand why it's matching where it shouldn't, and how to fix it :wink
(Do you understand what '&' does?)
Title: Re: need to determine the class of IP address by the 1-st byte only
Post by: Zap on May 20, 2009, 11:17:34 AM
I do,but i cant arrange ifs properly to get the result needed.However i arrange them the result is "Class C" .
I thought maybe there`s some other ways to solve the problem.
Also,i`m just starting to study asm,so it may look funny to u,sorry :)
Title: Re: need to determine the class of IP address by the 1-st byte only
Post by: Tedd on May 20, 2009, 11:25:43 AM
No, it looks fine, but you're matching based on whether any bits are still set after you've ANDed some of the top two.
So, for example, 10101010 & 11000000 = 10000000 -- which means you'd match this as a Class C, because some bits are set.
A better way to do it like this is to AND dIP with 11000000 first, and then check which of the class prefixes it's equal to.
Alternatively, just check if it's >= 11000000, elseif >= 10000000, elseif >= 01000000, else..
Title: Re: need to determine the class of IP address by the 1-st byte only
Post by: dedndave on May 20, 2009, 03:08:39 PM
i wanted to point out that bytes are different in different forms
example 170.100.200.200
in raw form (as the internet handles it), there are 4 bytes - the first byte is 170 decimal (the first part of the IPA)
in string form, there are 15 bytes - the first byte is 49 decimal (the ascii "1" character)
make sure you aren't comparing apples and oranges

also, 00000000 & anything is always false