News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

jumping in .if statements

Started by dicky96, January 15, 2006, 06:42:46 PM

Previous topic - Next topic

dicky96

I hope you guys are not getting fed up with me  :P

This is the first time I've tried to jmp to a label.  What I would like to know is why this code (which is my first attempt) will not assemble:

WsCheckUserIp:
   invoke gethostbyname, addr UserIP       
   .if eax!=NULL
      mov eax,[eax+12]                     
      mov eax,[eax]                        
      mov eax,[eax]                 
      mov [NoIpNameIP],eax          
      invoke send,[hSock],addr GetReq, sizeof GetReq,0   
recvloop:      invoke recv,[hSock],addr RecvBuffer,sizeof RecvBuffer,0
      .if eax==SOCKET_ERROR
         mov eax,2            
         
ret                        

      .endif
      .if   eax==0
         ret
      .endif
      jmp recvloop:
   .else                               ;<< I get an error on this line "argument expected"
      mov eax,1   
      ret
   .endif   


Where as this code (my second attempt) does assemble

WsCheckUserIp:
   invoke gethostbyname, addr UserIP       
   .if eax!=NULL
      mov eax,[eax+12]                     
      mov eax,[eax]                        
      mov eax,[eax]              
      mov [NoIpNameIP],eax       
      invoke send,[hSock],addr GetReq, sizeof GetReq,0   
      .while eax!=0
         invoke recv,[hSock],addr RecvBuffer,sizeof RecvBuffer,0
         .if eax==SOCKET_ERROR
            mov eax,2                                                                     ret                              .endif
      .endw
      ret   
   .else
      mov eax,1                  
      ret
   .endif   

Apart from the fact my second attempt looks more elegant, I think these two version should both amount to the same thing really.  I seem to have confused the assmebler by jumping to a lable in a .if .elseif statement.  What are the rules about this?  When can I jump to a lable and when can't I ?

TIA (again)
dicky

OOps the forum seems wo have mangled the text formatting on my second example a bit - hope it is still readable

G`HOST

By the first look of it
jmp recvloop:     ;<< Try "jmp recvloop" without ":" and without doublequotes  :bg

zooba

Quote from: dicky96 on January 15, 2006, 06:42:46 PM
OOps the forum seems wo have mangled the text formatting on my second example a bit - hope it is still readable

Put your code snippets inside [ code ] [ /code ] brackets.

Then it will look like this :U

dicky96

thanks zooba - I always wondered how u guys posted code so neatly

thanks G'Host - probably is something as silly as that - I just did not understand the error message as I have still to learn some of the correct syntax.

Now if we can go off on a tangent..... I do see the good use of all these .else .while & invoke "hi level" type constucts when dealing with windows procediures and all that stuff - but I also kinda feel the urge to go off and do some real assembler check flag and jmp to label if true/false etc type programming too.  In win32asm is there any sort of concencus on which type of programming structure should be used and when and where each is best applied??

Hope u guys don't mind I keep asking these dumb questions, I kinda feel like I take up all your time just lately


dicky

Ian_B

Quote from: dicky96 on January 15, 2006, 11:08:43 PM
I do see the good use of all these .else .while & invoke "hi level" type constucts when dealing with windows procediures and all that stuff - but I also kinda feel the urge to go off and do some real assembler check flag and jmp to label if true/false etc type programming too.  In win32asm is there any sort of concencus on which type of programming structure should be used and when and where each is best applied??

The best answer is surely whenever it works best for you. If it's code that you or someone else will need to continue to support, or that you are sharing and many people will want to be able to unpick easily, then it definitely might need the more obvious visual structure the high-level .IF/.ELSE type structure gives you. It might also depend on the complexity/nesting of the tests you are making, it hardly seems worth it for a few simple inline tests. For instance, I use the .IF/.ELSE structure in my main app message loops, because of the very large number of possible messages and it's really clear to see what's being tested. (But also because it started as an easy cut'n paste from a tutorial, he says sheepishly...  :red  :lol)

IanB

raymond

dicky

Don't shy away from these "hi-level" constructs (.if, .while, etc.). Along with proper indents, they make your code a lot easier to read and possibly debug later on, specially on larger projects. They are simply making a comparison and taking jumps based on the result.

Furthermore, it reduces the number of labels you need to create (and remember later how you spelled them), which labels must all be different. If you need reminders about what a section of code does, use comments.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

zooba

Quote from: raymond on January 16, 2006, 04:35:58 AM
which labels must all be different.

Only within the scope of a procedure. Unless labels are indicated with double ':' (ie. '::') they are unavailable outside the procedure.

Ian_B

Quote from: zooba on January 16, 2006, 04:40:49 AM
Quote from: raymond on January 16, 2006, 04:35:58 AM
which labels must all be different.

Only within the scope of a procedure. Unless labels are indicated with double ':' (ie. '::') they are unavailable outside the procedure.

Or you are using OPTION: NOSCOPED

IanB