无法获得最大的公约数

时间:2014-04-04 18:54:15

标签: masm32

这是我到目前为止,它将显示分数,但没有别的:

INCLUDELIB c:\masm32\lib\user32.lib
INCLUDELIB c:\masm32\lib\kernel32.lib

.data
  str1 BYTE "Enter the numerator: ",0       
  str2 BYTE "Enter the denominator: ",0     
  str3 BYTE "/",0                           
  str4 BYTE "The fraction you entered is: ",0             
  str5 BYTE "Simplest form of your fraction: ",0  ;msg9
  str6 BYTE "You can't store a 0 in the denominator",0dh, 0ah, 0  
  str7 BYTE " ",0dh, 0ah, 0    
  num1 SDWORD 0 ;n1                 
  num2 SDWORD 0 ;n2
  tempN SDWORD 0 
  tempD SDWORD 0
  temp3 SDWORD 0
  tempR SDWORD 0    

.code

main PROC 
  call Clrscr
  mov eax,0
  mov ebx,0
  mov ecx,0             
  call fractionPrompt
  call gcd
  call displayFraction
  exit
main ENDP

fractionPrompt PROC
  mov edx, OFFSET str1  ;prompt for numerator
 J1:
  call writestring
  call readDec    
  mov num1,eax
  jmp J2

 J2:
  mov edx,OFFSET str2   ;Prompt for denominator
  call writestring
  call readDec
  mov num2,eax
  cmp eax,0
  je reenter
  jne OP1 

 reenter:
  mov edx,OFFSET str7
  call writestring
  mov edx,OFFSET str6
  call writestring

 OP1:
  mov edx, OFFSET str7
  call writestring
  mov edx,OFFSET str4
  call writestring    
  mov eax,num1
  call writeDec
  mov tempN,eax    
  mov edx, OFFSET str3
  call writestring         ;/    
  mov eax,num2 
  mov tempD,eax  
  call writeDec
  ret
fractionPrompt ENDP

gcd PROC
  mov eax,tempN    
  cdq
  mov ebx,tempD
  mov edx,0
  div ebx        
  cmp edx,0 
  jne J1   
  je J2

 J1:
  mov eax,tempD            
  mov edx,0   
  dec temp3 
  div temp3
  mov temp3,edx 
  cmp edx,0         
  je J2    
  loop J1        

 J2:
  mov eax,tempN
  neg eax
  cdq
  mov edx,0           
  div temp3
  cmp edx,0
  je J3
  dec temp3
  loop J2    

 J3: 
  mov eax,tempN
  cdq
  div temp3
  mov tempN,eax
  call writeDec
  mov eax,tempD
  cdq
  div temp3
  mov tempD,eax
  call writeDec 
  ret
gcd ENDP

displayFraction PROC
  mov edx,OFFSET str5
  call writestring
  mov tempN,eax
  call writeDec
  mov edx, OFFSET str3
  call writestring    
  mov ebx,tempD
  call writeDec
  ret
displayFraction ENDP

END main

1 个答案:

答案 0 :(得分:0)

你的代码是如此错误,分别不值得纠正,我试过并失败了。看看这个工作示例(Masm32 Version 11):

include \masm32\include\masm32rt.inc

.data
    num     SDWORD 15   ; example numerator
    denom   SDWORD 12   ; example denominator
    gcd     SDWORD 0

.code

get_gcd PROC arg1:DWORD, arg2:DWORD
    mov eax, arg1
    mov ecx, arg2
    test ecx, ecx
    jz _Ret

    @@:
    cdq
    idiv ecx
    test edx, edx
    jz _Ret
    mov eax, ecx
    mov ecx, edx
    jmp @B

    _Ret:
    mov eax, ecx

    cdq             ; EAX = abs(EAX)
    add eax, edx
    xor eax, edx

    ret
get_gcd ENDP

main PROC
    printf ("Fraction: %i / %i\n",num,denom)
    invoke get_gcd, num, denom
    mov gcd, eax
    printf ("GCD: %i\n",eax)

    mov eax, num
    cdq
    idiv [gcd]
    mov num, eax

    mov eax, denom
    cdq
    idiv [gcd]
    mov denom, eax

    printf ("Reduced: %i / %i\n",num,denom)

    invoke ExitProcess, 0
main ENDP

END main