News:

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

Integer Overflow

Started by jckl, February 19, 2006, 12:47:56 PM

Previous topic - Next topic

jckl

When i get to div dnum1 i get an integer overflow. How would i go about fixing this?


TZI TIME_ZONE_INFORMATION <>
time SYSTEMTIME <>
dnum0 dd 86400
dnum1 dd 3600
dnum2 dd 60

DateNum dd ?
SecsPast dd ?

FromUnixToWin proc
   invoke GetTimeZoneInformation, ADDR TZI
   mov eax, TZI.Bias
   imul eax, 60
   sub SecsPast, eax
   mov eax, TZI.DaylightBias
   imul eax, 60
   sub SecsPast, eax
   mov eax, SecsPast
   div dnum0
   add eax, 2440588
   mov DateNum, eax
;   Invoke FUDCon
   mov eax, SecsPast
   div dnum0
   mov SecsPast, edx
   mov eax, SecsPast
   div dnum1
   INVOKE BaseAscii, eax, time.wHour, 0, 10, 0, 0, 1
   mov eax, SecsPast
   div dnum1
   mov SecsPast, edx
   mov eax, SecsPast
   div dnum2
   INVOKE BaseAscii, eax, time.wMinute, 0, 10, 0, 0, 1
   mov eax, SecsPast
   div dnum2
   mov SecsPast, edx
   INVOKE BaseAscii, eax, time.wSecond, 0, 10, 0, 0, 1
   ret
FromUnixToWin endp



Also i have another function which reads from a file. This file can be 20mbs or 1gb+. I wanted to know if there is a faster way to read the data than what i have.

GetNames proc
   invoke _lopen, ADDR file_name, OF_READ
   cmp eax, -1
   je error

   mov f_handle, eax

   invoke _llseek, f_handle, 8, 0
   invoke _lread, f_handle, ADDR ItemCount, 4
   invoke _llseek, f_handle, 16, 0
   invoke _lread, f_handle, ADDR value, 4
   add value, 4

   mov crt,0
   .while TRUE
      inc crt

      invoke _llseek, f_handle, value, 0
      invoke _lread, f_handle, addr tmpNum, 4
      INVOKE BaseAscii, tmpNum, addr filestart, 0, 10, 0, 0, 1
      add value, 4
      invoke _llseek, f_handle, value, 0
      invoke _lread, f_handle, addr tmpNum, 4
      INVOKE BaseAscii, tmpNum, addr pffFileSize, 0, 10, 0, 0, 1
      add value, 4
      invoke _llseek, f_handle, value, 0
      invoke _lread, f_handle, addr SecsPast, 4
      Invoke FromUnixToWin

      add value, 4
      invoke _llseek, f_handle, value, 0
      invoke _lread, f_handle, ADDR buffer, 16

      .if DeletedFiles == 1
         invoke InsertItem
      .else

        invoke StrCmp, ADDR buffer, ADDR DeadSpace
     
        .if eax!=0

           invoke InsertItem
       
        .endif
      .endif
      add value, 24

      mov eax, ItemCount
      cmp crt, eax
      je Done

    .endw
  Done:
    invoke _lclose, f_handle
    ret
  error: ; error
    invoke MessageBox, NULL, ADDR err_msg, NULL, MB_OK+MB_ICONERROR
    ret
GetNames endp

BaseAscii PROC InPut:DWORD, OutPut, Len, Base, Comma, Fill, TermA
   LOCAL LBuff[32]: BYTE

   pushad
   xor esi, esi
   mov eax, InPut
   mov ebx, OutPut
   mov byte ptr [ebx], '0'
   .while (eax)
     xor edx, edx
     div Base                   ; Base 10, 16, 8, 2
     add dl, 30h                ; Convert to dec ASCII
     mov LBuff[esi], dl
     inc esi
   .endw
   xor edi, edi
   mov ecx, esi
   .if Len > ecx  && Fill == 1
     xor eax, eax
     .while (eax < Len)
       mov byte ptr [ebx+eax], '0'
       inc eax
     .endw
     sub Len, ecx
     add edi, Len
   .endif
   .while (ecx)
     mov al, byte ptr LBuff[esi-1]
     mov byte ptr [ebx+edi], al
     inc edi
     dec esi
     dec ecx
   .endw
   .if TermA
     mov byte ptr [ebx+edi], 0
   .endif
   popad
       
   ret
BaseAscii ENDP

StrLen PROC SRC:DWORD
   mov ecx,-1
   mov edi,SRC
   mov al,0
   repnz scasb
   mov eax,ecx
   not eax
   dec eax
   ret
StrLen ENDP

StrCmp PROC SRC:DWORD,DST:DWORD
   INVOKE StrLen,SRC
   inc eax
   mov ecx,eax
   mov esi,SRC
   mov edi,DST
   repz cmpsb
   mov eax,1
   jnz Next    ;zf set or not from repz
   dec eax
   Next:
   ret
StrCmp endp


Mirno

The division is 64 bit (although the result must be 32 bit otherwise you see an overflow), with the upper 32 bits being held in edx.
Also remember that the modulus of the division is held in edx after the operation....

So your first division will probably be returning with some value other than zero in edx, which in the second division is treated as the upper 32 bits of a 64 bit integer value...

The fix is to "xor edx, edx" before any of the divisions you perform (at least where you cannot guarantee the value in edx to be zero, or part of your number).

Mirno

jckl

i had just realized that before reading your reply. Off to test now thanks.

Mark Jones

Hi Jckl, where did you get the StrLen and StrCmp routines from? Try these, they may be faster. They are part of a Timing Testbed application I'm building. Look for a beta in the Laboratory next week or so.


.code
a LABEL DWORD                       ; align procs to this offset

    ; routine by Mark Jones 2005
    db 32-(($-a) AND 31) dup (0CCh) ; ALIGN 32
szCompMJ1 proc uses esi edi szDest:DWORD,szSource:DWORD
    mov esi,szSource
    mov edi,szDest
@@:
    mov al,byte ptr[esi]            ; fetch bytes
    inc esi                         ; prevent stall
    mov ah,byte ptr[edi]
    inc edi
    test al,ah                      ; null?
    jz @good
    cmp al,ah
    jnz @bad                        ; match?
    jmp @B
@good:
    mov eax,0                       ; result as eax
    ret
@bad:
    mov eax,1
    ret
szCompMJ1 endp



; code possibly by Hutch
align 4
szLen5 proc src:DWORD

    mov     ecx, [esp+1*4]
    test    ecx, 3
    jz      @max8

@bucle:
    mov     al, [ecx]
    add     ecx, 1
    test    al, al
    jz      @lb1

    test    ecx, 3
    jnz     @bucle
align 4
@max8:
    mov     eax, [ecx]
    mov     edx, 7EFEFEFFh
    add     edx, eax
    xor     eax, 0FFFFFFFFh
    xor     eax, edx
    add     ecx, 4
    test    eax, 81010100h
    jz      @max8

    mov     eax, [ecx-4]
    test    al, al
    jz      @lb4

    test    ah, ah
    jz      @lb3

    test    eax, 0FF0000h
    jz      @lb2

    test    eax, 0FF000000h
    jnz     @max8

@lb1:
    lea     eax, [ecx-1]
    mov     ecx, [esp+1*4]
    sub     eax, ecx
    ret     1*4

@lb2:
    lea     eax, [ecx-2]
    mov     ecx, [esp+1*4]
    sub     eax, ecx
    ret     1*4

@lb3:
    lea     eax, [ecx-3]
    mov     ecx, [esp+1*4]
    sub     eax, ecx
    ret     1*4

@lb4:
    lea     eax, [ecx-4]
    mov     ecx, [esp+1*4]
    sub     eax, ecx
    ret     1*4

szLen5 endp
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08