flat assembler
Message board for the users of flat assembler.

Index > Windows > regarding convert win32 C macro FD_SET (network) to fasm

Author
Thread Post new topic Reply to topic
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
hi, i just did the fasm FD_SET based on the C macro, could anyone verify for

me, is the below proc FD_SET correct or not?
thanks a lot.

Code:

fdsize  equ     64 ; or any number to test

struct fd_set
        fd_count        dd ?
        fd_array        rd fdsize
ends

proc FD_SET fd,set
        push eax ebx ecx edx
        mov  ebx,[fd]           ; ebx = fd
        mov  edx,[set]
        push edx
        mov  eax,[edx]          ; eax = fd_count
        add  edx,4                      ; edx = starting of fd_array
        mov  ecx,0

        @@:
                cmp  ecx,eax
                jge  @f
                cmp  [edx],ebx
                je   .finish
                add  edx,4
                inc  ecx
                jmp  @b
        @@:

        cmp  ecx,eax
        jne  .finish
        cmp  ecx,fdsize
        jg   .finish
        push ebx
        pop  dword [edx]
        pop  edx
        inc  dword [edx]
        .finish:

        pop  edx ecx ebx eax
        ret
endp

;from winsock2.h
;
;typedef struct fd_set {
;        u_int fd_count;               /* how many are SET? */
;        SOCKET  fd_array[FD_SETSIZE];   /* an array of SOCKETs */
;} fd_set;
;
;
;
;#define FD_SET(fd, set) do { \
;    u_int __i; \
;    for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
;        if (((fd_set FAR *)(set))->fd_array[__i] == (fd)) { \
;;            break; \
;        } \
;    } \
;    if (__i == ((fd_set FAR *)(set))->fd_count) { \
;        if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
;            ((fd_set FAR *)(set))->fd_array[__i] = (fd); \
;            ((fd_set FAR *)(set))->fd_count++; \
;        } \
;    } \
;} while(0)
    
Post 18 Mar 2006, 15:13
View user's profile Send private message Visit poster's website Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov
its a proc variant of FD_SET macro from winsock2.h
Code:
fdsize  =  64
proc FD_SET fd,set; fd is unsigned, set is offset fd_set struct
        mov     edx,[set]
        mov     ecx,[fd]
        xor     eax,eax
        jmp     .while
.do:
        cmp     ecx,[edx+4*(eax+1)]
        je      .endw
        inc     eax
.while:
        cmp     eax,[edx]
        jb      .do
.endw:
        cmp     eax,[edx]
        jne     @f
        cmp     [edx],fdsize
        jae     @f
        mov     [edx+4*(eax+1)],ecx
        inc     [edx]
@@:
        ret
endp    

_________________
regards
Post 19 Mar 2006, 20:39
View user's profile Send private message Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
oh,thanks nikolay petrov,
btw, the FD_SET i posted works on my application Smile

here is the FD_CLR,
Code:
proc FD_CLR fd,set
        push eax ebx ecx edx
        mov  ebx,[fd]                   ; ebx = fd
        mov  edx,[set]
        mov  eax,[edx]                  ; eax = fd_count
        add  edx,4                              ; so that EDX reffer to fd_array, (skip the fd_count)
        mov  ecx,0
        @@:
                cmp  ecx,eax
                jge  .finish
                cmp  ebx,[edx]
                je   .relocate
                inc  ecx
                jmp  @b

        .relocate:
                cmp  ecx,eax
                je   .reduce
                push dword [edx+4]
                pop  dword [edx]
                inc  ecx
                add  edx,4
                jmp  .relocate
        
        .reduce:                                ; fd_count--
                dec  [set]

        .finish:
        pop  edx ecx ebx eax
                        ret
endp
    


tested and work correctly.
Post 20 Mar 2006, 07:00
View user's profile Send private message Visit poster's website Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov
i don't think. the proc wasn't wrote very correctly, if we see the macro.
for example fd and fd_array is unsigned, but you checked signed;
dec [set] -??? its equ dec[ebp+12], but [ebp+12]=[set] and will be no effect into set.fd_count, when proc finish.
next is a not bad solution
Code:
proc FD_CLR fd,set
        mov     edx,[set]
        mov     ecx,[fd]
        xor     eax,eax
        jmp     .in_for
.for:
        cmp     ecx,[edx+4*(eax+1)]
        jne     .continue
        jmp     .while
.do:
        mov     ecx,dword ptr [edx+4*(eax+2)]
        mov     [edx+4*(eax+1)],ecx
        inc     eax
.while:
        mov     ecx,[edx]
        dec     ecx
        cmp     eax,ecx
        jb      .do
        dec     [edx]
        ret 
.continue:
        inc     eax
.in_for:
        cmp     eax,[edx]
        jb      .for
        ret
endp    

or
Code:
proc FD_CLR fd,set

        mov     edx,[set]
        mov     ecx,[fd]
        xor     eax,eax
        jmp     .while
.do:
        cmp     ecx,[edx+4*(eax+1)]
        je      .endw
        inc     eax
.while:
        cmp     eax,[edx]
        jb      .do
        ret
.endw:
        push    ebx
        mov     ebx,[edx]
        dec     ebx
        jmp     .while2
.do2:
        mov     ecx,[edx+4*(eax+2)]
        mov     [edx+4*(eax+1)],ecx
        inc     eax
.while2:
        cmp     eax,ebx
        jb      .do2
        dec     [edx]
        pop     ebx
        ret
endp    

by the way, if it isn't a secret why do you copy winsock macros?

_________________
regards
Post 20 Mar 2006, 11:25
View user's profile Send private message Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
oh yeah,
my mistake Embarassed
Post 20 Mar 2006, 23:29
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.