MASM代码通过添加计算倍数和功率

时间:2017-05-28 21:55:54

标签: assembly x86 masm

这是我第一次发帖,如果它不完美就道歉。我正在研究一个程序,以确定一对整数的和,多和指数值。除非我在输入中放置0或1,然后程序似乎在CalculatePower过程中循环,否则我的代码运行正常。我的假设是这些值导致ECX计数器低于0,但我似乎无法理清如何修复它。它似乎运行得有点慢,所以我也想知道是否有人可能会提出一个提高效率的建议。谢谢!

 TITLE Two Integer Calculator          (example.asm)

 ; This program accepts two positive integers and calculates a sum, product, and power from the integers

 INCLUDE Irvine32.inc     ; Using procedure calls ReadInt, WriteInt, and WriteString

 .data

 ReadInt proto
 WriteInt proto
 WriteString proto

 prompt BYTE "Enter a positive integer: ",0   ; Text to prompt user for a positive integer
 sum BYTE "The sum is: ",0     ; Text preceeding the sum result
 product BYTE 0dh, 0ah, "The product is: ",0  ; Text preceeding the product result
 power BYTE 0dh, 0ah, "The power result is: ",0    ; Text preceeding the power value result
 spacer BYTE 0dh,0ah, " ",0dh,0ah,0      ; Takes the place of procedure call Crlf to place space between program and wait message
 integer DWORD ? ; The user-entered integer

 .code

 main PROC
 ; Asks for and receives the first integer from the user, moves it to EBX
 call GetInteger
 mov ebx,eax


 ; Receives the second integer from the user and moves it into the 'integer' variable
 call GetInteger
 mov integer,eax


 ; Adds the values in the EAX and EBX registers together, then displays the sum
 call AddNumbers
 mov edx,OFFSET sum
 call WriteString
 call WriteInt


 ; Reloads the original user-entered integer to the EAX register, multiplies the EAX and EBX reigsters, then displays the product
 mov eax,integer
 call MultiplyNumbers
 mov edx,OFFSET product
 call WriteString
 call WriteInt


 ; Reloads the original user-entered integer to the EAX register, then calculates the value stored in EAX to the power of the value in the EBX register
 mov eax,integer
 call CalculatePower
 mov edx,OFFSET power
 call WriteString
 call WriteInt

 ; Inserts a line of separation between program and the wait message
 mov edx,OFFSET spacer
 call WriteString

 exit
 main ENDP




 ;-----------------------------------------------------------
 GetInteger PROC
 ; Prompts the user to enter an integer, assuming that the
 ; user will enter valid, positive integers. Stores the
 ; user-entered integer in EAX
 ; Receives: 
 ; Returns: EAX
 ;-----------------------------------------------------------

      mov edx, OFFSET prompt ; Prompts the user to enter an (assumed positive) integer
      call WriteString    ; Displays the string prompt to enter a positive integer
      call ReadInt   ; Stores the user-input integer in EAX

      ret
 GetInteger ENDP     ; End of UDP GetInteger




 ;-----------------------------------------------------------
 AddNumbers PROC
 ; Accepts two integers in EAX and EBX then adds the two
 ; integers together, storing the sum in EAX.
 ; Receives: EAX, EBX
 ; Returns: EAX
 ;-----------------------------------------------------------

      add eax,ebx    ; adds the two integers together, sum is stored in EAX

      ret
 AddNumbers ENDP




 ;-----------------------------------------------------------
 MultiplyNumbers PROC USES ecx
 ; Accepts two integers in EAX and EBX. Uses the AddNumbers
 ; procedure to multiply the two numbers, then stores the
 ; product in EAX.
 ; Receives: EAX, EBX
 ; Returns: EAX
 ;-----------------------------------------------------------

      mov ecx,eax
      dec ecx
      mov eax,ebx

      multiply:
           call AddNumbers
           loop multiply

      ret
 MultiplyNumbers ENDP




 ;-----------------------------------------------------------
 CalculatePower PROC
 ; Accepts two integers in EAX and EBX, then uses the
 ; MultiplyNumbers procedure to calculate the integer in EAX
 ; to the power of the integer in EBX. The result is stored
 ; in the EAX register.
 ; Receives: EAX, EBX, ECX
 ; Returns: EAX
 ;-----------------------------------------------------------

      mov ecx,eax
      dec ecx
      mov eax,ebx

      exponent:
           call MultiplyNumbers
           loop exponent

      ret
 CalculatePower ENDP

 END main

0 个答案:

没有答案