News:

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

Hello, i need help with this code

Started by marcelo6297, September 09, 2010, 06:55:24 PM

Previous topic - Next topic

marcelo6297

Hello I need help with this code gives me error and not know why, I'm trying to find the greatest common divisor of two numbers using Euclid's algorithm


  invoke GetDlgItemInt,hWin,Edit1,0,1
  mov variable1,eax
  invoke GetDlgItemInt,hWin,Edit2,0,1
  mov variable2,eax
  invoke mcdProc, variable1,variable2
  invoke dwtoa, variable2, addr cadena1
  invoke MessageBox,NULL, addr cadena1,addr Titulo ,MB_OK+MB_ICONINFORMATION


mcdProc proc var1, var2
         mov eax, var1
         mov ebx, var2
              .while TRUE
                    .if(ebx > 0)
                       div ebx
                       mov eax, ebx
                       mov ebx,edx
                     .else
                        ret
                     .endif
               .endw
         Ret
mcdProc EndP

It works with some numbers like 45 - 5 or 36 -6 but other that they MCD is 1 not work the programs crashes

clive

What register is the answer from mcdProc in when it exits?

Where are you storing it so it prints with the dwtoa()? because it is not stored in variable2

You should preserve EBX across a function call (Intel/Microsoft ABI)

Do you have exit code after the MessageBox() call? Or does it just drop into mcdProc and crash?
It could be a random act of randomness. Those happen a lot as well.

marcelo6297

hello this is the all code, the result of the mcdProc is stored in the ebx register, after exit it my containt the result of the mcdProc,
this is all the code


.386
.model flat, stdcall  ;32
option casemap:none

include windows.inc
include masm32.inc
include user32.inc
include kernel32.inc
include Comctl32.inc
include gdi32.inc

include shell32.inc
;------------------------------------------------------------------
;Incluido para la conversion a String
;include \masm32\include\masm32rt.inc
;------------------------------------------------------------------
includelib masm32.lib
includelib kernel32.lib
includelib user32.lib
includelib Comctl32.lib
includelib shell32.lib
includelib gdi32.lib

DlgProc PROTO :HWND,:UINT,:WPARAM,:LPARAM
mcmProc  PROTO : DWORD,:DWORD
mcdProc  PROTO : DWORD,:DWORD

.const
TRUE          EQU    1
FALSE         EQU    0
IDD_DLG1001 equ 1001
Edit1 equ 1002
Edit2         equ   1003
Edit3     equ    1004
Button1 equ 1005
;#########################################################################

.data?

hInstance dd ?
variable1 DWORD ?
variable2 DWORD ?
variable3 DWORD ?
cadena1  db 1024 dup(?)
;#########################################################################

.data
Titulo  db "Examen Final",0
resultado db "El resultado es; ",0

.code

start:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_DLG1001,NULL,addr DlgProc,NULL
invoke ExitProcess,0


DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
    mov eax,uMsg
.if eax==WM_INITDIALOG
         
.elseif eax == WM_COMMAND
    mov edx,wParam
   .if edx==Button1
       ;Obtengo el primero Numero
       invoke GetDlgItemInt,hWin,Edit1,0,1
       mov variable1,eax
                invoke GetDlgItemInt,hWin,Edit2,0,1
                mov variable2,eax
                invoke mcdProc, variable1,variable2
                mov variable2,ebx        
        invoke dwtoa, variable2, addr cadena1
        invoke MessageBox,NULL, addr cadena1,addr Titulo ,MB_OK+MB_ICONINFORMATION
     .endif
.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp
mcmProc proc var1, var2
         mov eax, var1
         mov ebx, var2
         mov cx,1
         .while TRUE
             mul cx     
             div bx
             
              .if (dx == 0)
                   mov eax, var1
                   mul cx
                  .break
              .endif
              inc cx
              mov eax, var1
         .endw
         Ret
mcmProc EndP

mcdProc proc var1, var2
         mov eax, var1
         mov ebx, var2
              .while TRUE
                    .if(ebx > 0)
                       div ebx
                       mov eax, ebx
                       mov ebx,edx
                     .else
                        ret
                     .endif
               .endw
         Ret
mcdProc EndP
end start



this is all, it works with some number but when the mcd is 1 it crashes, i supoust it is by an division by zero in the loop.

KeepingRealBusy

Quote from: marcelo6297 on September 09, 2010, 06:55:24 PM
Hello I need help with this code gives me error and not know why, I'm trying to find the greatest common divisor of two numbers using Euclid's algorithm


  invoke GetDlgItemInt,hWin,Edit1,0,1
  mov variable1,eax
  invoke GetDlgItemInt,hWin,Edit2,0,1
  mov variable2,eax
  invoke mcdProc, variable1,variable2
  invoke dwtoa, variable2, addr cadena1
  invoke MessageBox,NULL, addr cadena1,addr Titulo ,MB_OK+MB_ICONINFORMATION


mcdProc proc var1, var2
         mov eax, var1
         mov ebx, var2
              .while TRUE
                    .if(ebx > 0)
                       div ebx
                       mov eax, ebx
                       mov ebx,edx
                     .else
                        ret
                     .endif
               .endw
         Ret
mcdProc EndP

It works with some numbers like 45 - 5 or 36 -6 but other that they MCD is 1 not work the programs crashes


Before you divide "div ebx" you must clear edx "xor edx,edx" because the "div ebx" divides eax:edx by ebx.


                    .if(ebx > 0)
                       XOR EDX,EDX
                       div ebx
                       mov eax, ebx
                       mov ebx,edx


Do NOT do this after the "mov ebx,edx" because you need to clear it also from the initial entry.

Dave

marcelo6297

Thanks for the Answer, it works fine!!!! :U

Marcelo


jj2007

Quote from: KeepingRealBusy on September 09, 2010, 10:02:42 PMBefore you divide "div ebx" you must clear edx "xor edx,edx" because the "div ebx" divides eax:edx by ebx.

Instead of xor edx, edx you should use cdq: one byte shorter, and cares for correct treatment of negative eax.