0

所以我正在尝试编写一个非递归的阶乘过程使用循环指令,该参数通过运行时堆栈传递。

我还需要主 PROC 中的指令序列来调用阶乘过程。任何人都想帮助我完成这部分,这是我到目前为止所拥有的。

.IF eax == 0 || eax == 1  ;special cases
          mov eax, 1              ;factorial == 1
          jmp L2              ;quit procedure
        .ELSEIF eax > 12      ;n is too large
          mov edx, OFFSET msgError
          call Crlf
          call WriteString
          jmp L2              ;quit procedure
        .ENDIF

        mov ecx, eax              ;ecx = counter

            L1:
        dec ecx                   ;ecx = n - 1
        mul ecx                   ;eax = n * (n - 1)

        cmp ecx, 1            ;is counter > 1?
        ja L1                     ;true? then continue

        L2:
        ret

    nonRecurFact ENDP
4

1 回答 1

0

我做了一个使用 LOOP 指令的非递归阶乘过程。参数通过堆栈发送,但您可以移除堆栈并使用全局变量 my_factor。这个小程序在 EMU8086 中运行:

.stack 100h
.data
my_factor dw 5
.code          
;INITIATE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax

  push my_factor  
  call non_recursive_factorial

;FINISH.  
  mov  ax,4c00h
  int  21h           

proc non_recursive_factorial
  pop  ax ;RETRIEVE ADDRESS TO RETURN TO CALL.
  pop  my_factor  ;RETRIEVE PARAMETER.
  push ax ;PUT BACK ADDRESS TO RETURN TO CALL.
  mov  ax,my_factor
  mov  cx,my_factor
  dec  cx            
while:                                  
  mul  cx ;AX*CX = DX:AX.
  loop while
  ret
endp

在过程结束时,结果在 AX 中。然后你可以显示它或其他东西。

于 2015-03-17T22:58:00.080 回答