need to determine the class of IP address by the 1-st byte only

Started by Zap, May 20, 2009, 09:26:49 AM

Previous topic - Next topic

Zap

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

Tedd

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?)
No snowflake in an avalanche feels responsible.

Zap

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 :)

Tedd

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..
No snowflake in an avalanche feels responsible.

dedndave

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