The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ecube on November 29, 2007, 07:04:58 AM

Title: recv byte count
Post by: ecube on November 29, 2007, 07:04:58 AM
I want to log a days worth of recieved bytes via the function recv, I don't think dword will hold it, how should I approach this?
Title: Re: recv byte count
Post by: jj2007 on November 29, 2007, 09:32:29 AM
Either use qwords, or count kBytes...
Title: Re: recv byte count
Post by: Shantanu Gadgil on November 29, 2007, 09:43:36 AM
I too think qwords should do ... for now  :wink ( ffff ffff ffff ffff [hex] == 18,446,744,073,709,551,615 [dec])

I think that many bytes per day would be enough. (Although, "enough" is never really enough!!!  :bdg )

Ok getting too philosophical ... maybe!

I think the following code might be useful:

mov     eax, dwBytesDownloadedLow
mov     edx, dwBytesDownloadedHigh
;============================
add     eax, dwBytesReceived   ;from the recv function
adc     edx, 0
;============================
mov     dwBytesDownloadedLow, eax
mov     dwBytesDownloadedHigh, edx


Then use the 64-bit pair of High:Low to output the number in "string" or KB or MB or whatever!

HTH,
Shantanu
Title: Re: recv byte count
Post by: Tedd on November 29, 2007, 12:54:46 PM

.data?
count_lo    DWORD ?
count_hi    DWORD ?

.code
      :
    mov eax,bytes_recvd
    add count_lo,eax
    adc count_hi,0
      :


You could keep adding 'adc' as many times as you like, but I don't think you'll get a QWORD of bytes in a day (~ 194TB/s for 24hours!!)
..or week :lol
Title: Re: recv byte count
Post by: ecube on December 31, 2008, 10:48:28 PM
you said use a qword but you're examples you're still using a 2 dwords to represent a qword and just filling 1 of the dwords with the count. doesn't the defeat the purpose?
Title: Re: recv byte count
Post by: Tedd on January 01, 2009, 01:53:07 PM
The magic part is the ADC :wink

recv gives a 32-bit count of the bytes received in that call.
You take that value and add it onto count_lo - that only requires 32 bits until the total overflows...
When an addition overflows, the carry flag is set (the maximum an addition can overflow is only one extra bit.)
Then you add-with-carry onto count_hi. If there was no bit to carry this will do nothing (count_hi+0+0), but when the carry-flag is set the extra bit will be added on (count_hi+0+1) -- which, in effect, makes count_hi:count_lo behave as the 64-bit value you want.
And you can chain together as many dwords as you want, just by following with extra adc instructions.
Title: Re: recv byte count
Post by: ecube on January 01, 2009, 05:41:04 PM
that's pretty neat :) thanks for explaining it to me tedd, I never seen anyone intentionally overflow something, but if works, great :D
Title: Re: recv byte count
Post by: Tedd on January 02, 2009, 08:09:27 PM
That is how the instructions are designed to work :wink
You can do a similar thing for 'long' subtraction, using SUB/SBB