GreatestCommonDivisor<GCD>

VSBrown VSBrown is offline Offline Feb 21st, 2005, 2:27 am |
0
A test program that will call the PROC findGCD, passing in the integer values input by the user and then it will display the results to the screen.
Quick reply to this message  
Assembly Syntax
  1. TITLE: GreatestCommonDivisor<GCD> (GCD.asm)
  2.  
  3. ;------------------------------------------------------------
  4. ; Name:
  5. ;
  6. ; Filename: GCD.asm
  7. ;
  8. ; Project #: 5
  9. ;
  10. ; Completed: March 9, 2003
  11. ;
  12. ; Description: My fifth assembly program. A test program that
  13. ; will call the PROC findGCD, passing in the integer
  14. ; values input by the user and then it will diplay
  15. ; the results to the screen.
  16. ; From Assembly Language For Intel-Based Computers,
  17. ; 4th Ed. by Kip R. Irvine. Problem #6 on pg. 257.
  18. ;
  19. ; Reference: Assembly Language For Intel-Based Computers,
  20. ; 4th Ed. by Kip R. Irvine
  21. ;------------------------------------------------------------
  22. INCLUDE Irvine32.inc
  23.  
  24. .data
  25. int1 sdword ?
  26. int2 sdword ?
  27. caseTable byte '1' ;lookup value
  28. dword enterInt ;address of lookup value
  29. entrySize = ($ - caseTable )
  30. byte '2'
  31. dword endTest
  32. numberOfEntries = 2
  33. msgIntro byte "This is Your Name's fifth assembly program. A test program that",0dh,0ah
  34. byte "will call the PROC findGCD, passing in the integer values input by",0dh,0ah
  35. byte "the user and then it will display the results to the screen.",0dh,0ah,0
  36. msgSelMn byte "---------------------------------",0dh,0ah
  37. byte "Select 1 to Run Prog or 2 to Quit ",0dh,0ah
  38. byte "---------------------------------",0dh,0ah
  39. byte "Enter Selection: ",0
  40. msgInt1 byte "Enter first integer: ",0
  41. msgInt2 byte "Enter second integer: ",0
  42. msgAns byte "Greatest common divisor is: ",0
  43. msgEnd byte "GoodBye",0dh,0ah,0
  44.  
  45. .code
  46. main PROC
  47. ;///////Intro Message/////////////////////////////////
  48. mov edx,OFFSET msgIntro ;intro message into edx
  49. call WriteString ;display msgIntro
  50. call Crlf ;endl
  51. call WaitMsg ;pause message
  52. call Clrscr ;clear screen
  53. call Menu ;menu procedure
  54.  
  55. ExitProg::exit ;global label to exit
  56. main ENDP
  57.  
  58. ;------------------------------------------------
  59. Menu PROC
  60. ;
  61. ; Receives: Nothing
  62. ; Returns: Nothing
  63. ;------------------------------------------------
  64. mov edx,OFFSET msgSelMn ;ask user for input
  65. call WriteString ;display msgSelMn
  66. call ReadChar ;read one character
  67. call Clrscr
  68. mov ebx,OFFSET caseTable;point EBX to the table
  69. mov ecx,numberOfEntries ;loop counter
  70.  
  71. L1:
  72. cmp al,[ebx] ;match found?
  73. jne L2 ;no: continue
  74. call NEAR PTR [ebx + 1] ;yes: call the procedure
  75. call WriteString ;display message
  76. call Crlf ;endl
  77. call Clrscr ;clear screen
  78. jmp L3 ;exit the search
  79.  
  80. L2:
  81. add ebx,5 ;point to the next entry
  82. loop L1 ;repeat until ECX = 0
  83.  
  84. L3:
  85. jmp Menu ;run Menu again
  86.  
  87. Menu ENDP
  88.  
  89. ;------------------------------------------------
  90. enterInt PROC
  91. ;
  92. ; Finds the GCD
  93. ; Receives: EAX = 1, EBX = 1
  94. ; Returns: EAX = GCD
  95. ;------------------------------------------------
  96. push edx
  97.  
  98. mov edx,OFFSET msgInt1 ;ask user for input
  99. call WriteString ;display msgInt1
  100. call ReadInt ;read integer
  101. mov int1,eax
  102. mov edx,OFFSET msgInt2 ;ask user for input
  103. call WriteString ;display msgInt2
  104. call ReadInt ;read integer
  105. mov int2,eax
  106.  
  107. mov eax,int1
  108. mov ebx,int2
  109. call findGCD
  110. mov edx,OFFSET msgAns
  111. call WriteString
  112. call WriteDec
  113. call Crlf
  114.  
  115. call WaitMsg
  116. pop edx
  117.  
  118. enterInt ENDP
  119.  
  120. ;------------------------------------------------
  121. findGCD PROC
  122. ;
  123. ; Finds the GCD
  124. ; Receives: EAX = 1, EBX = 1
  125. ; Returns: EAX = GCD
  126. ;------------------------------------------------
  127. push ebx
  128. push edx
  129.  
  130. or eax,eax ;explicitly set sign flag
  131. .IF SIGN? ;in the event of a neg int
  132. neg eax
  133. .ENDIF
  134. or ebx,ebx
  135. .IF SIGN?
  136. neg ebx
  137. .ENDIF
  138.  
  139. L1:
  140. mov edx,0
  141. div ebx ;divide int1 by int2
  142. cmp edx,0 ;does remainder = 0 ?
  143. je L2 ;yes: quit
  144. mov eax,ebx ;no: prepare for
  145. mov ebx,edx ;next iteration
  146. jmp L1
  147.  
  148. L2:
  149. mov eax,ebx ;EAX = GCD
  150.  
  151. pop edx
  152. pop ebx
  153. ret
  154.  
  155. findGCD ENDP
  156.  
  157. ;------------------------------------------------
  158. endTest PROC
  159. ;
  160. ; Receives: Nothing
  161. ; Returns: EDX = offset of message
  162. ; Sets CF = 1 to signal end of program
  163. ;------------------------------------------------
  164. mov edx,OFFSET msgEnd ;msgEnd into edx
  165. call WriteString ;display message
  166. call Crlf ;endl
  167. stc ;CF = 1
  168. jc ExitProg ;if CF=1 then jump to Global ExitProg
  169.  
  170. endTest ENDP
  171.  
  172. END main

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC