0
    section .data
        msg db "Menu: "
        msgLen equ $ -msg
        msg2 db "[1]Factorial"
        msgLen2 equ $ -msg2mov dx, 0
        msg3 db "[2]Power"
        msgLen3 equ $ -msg3
        msg4 db "[3]Exit"
        msgLen4 equ $ -msg4
        msg5 db "Enter number: "
        msgLen5 equ $ -msg5
        msg6 db "Enter two numbers: "
        msgLen6 equ $ -msg6
        line db "", 10

    section .bss
         choice resb 1
        num1 resw 1
        quo1 resw 1
        quo2 resw 1
        quo3 resw 1
        quo4 resw 1
        quo5 resw 1
        rem1 resw 1
        rem2 resw 1
        rem3 resw 1
        rem4 resw 1
        rem5 resw 1

    section .text
         global _start

    _start:
    do_while:
        mov eax, 4
        mov ebx, 1
        mov ecx, msg
        mov edx, msgLen
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, line
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, msg2
        mov edx, msgLen2
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, line
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, msg3
        mov edx, msgLen3
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, line
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, msg4
        mov edx, msgLen4
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, line
        mov edx, 1
        int 80h

        mov eax, 3
        mov ebx, 0
        mov ecx, choice
        mov edx, 2
        int 80h

        sub byte [choice], 30h

        cmp byte [choice], 1
        je menu1
        cmp byte [choice], 2
        je power
        cmp byte [choice], 3
        je exit
        jg do_while
        jl do_while


  menu1:
        mov eax, 4
        mov ebx, 1
        mov ecx, msg5
        mov edx, msgLen5
        int 80h

        mov eax, 3
        mov ebx, 0
        mov ecx, num1
        mov edx, 1
        int 80h

        sub word [num1], 30h
        sub esp, 4
        push word [num1]
        call fact
        pop word [num1]             ;40320

        mov al, [num1]              ;4032
        mov ah, 0
        mov bl, 10
        div bl

        mov byte [quo1], al
        mov byte [rem1], ah

        mov al, [quo1]              ;403
        mov ah, 0
        mov bl, 10
        div bl

        mov byte [quo2], al
        mov byte [rem2], ah

        mov al, [quo2]              ;40
        mov ah, 0
        mov bl, 10
        div bl

        mov byte [quo3], al
        mov byte [rem3], ah

        mov al, [quo3]              ;4              
        mov ah, 0
        mov bl, 10
        div bl

        mov byte [quo4], al
        mov byte [rem4], ah

        add word [quo4], 30h
        add word [rem4], 30h
        add word [rem3], 30h
        add word [rem2], 30h
        add word [rem1], 30h

        mov eax, 4
        mov ebx, 1
        mov ecx, quo4
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, rem4
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, rem3
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, rem2
        mov edx, 1
        int 80h

        mov eax, 4
        mov ebx, 1
        mov ecx, rem1
        mov edx, 1
        int 80h


        jmp do_while

  fact:
        mov ebp, esp
        mov cl, [ebp+4]
        mov [ebp+6], cl

        ;conditions

        mov ax, [ebp+4]
        dec word [ebp+6]
        mul word [ebp+6]
        mov [ebp+8], al 

  while:
        dec word [ebp+6]
        mov al, [ebp+8]
        mov dx, 0
        mul word [ebp+6]
        mov word [ebp+8], ax            ;al is byte size 

        cmp word [ebp+6], 1
        jne while
        ret 4



power:
mov eax, 1
mov ebx, 0
int 80h

exit:    
mov eax, 1
mov ebx, 0
int 80h

我正在循环内计算阶乘。5!结果到 120 但 6!结果为 208 字节大小最多为 255,而字大小最多为 65,655。我的代码中应该更改或更正什么?

这是我的代码。不要介意权力,退出标签,评论。请注意执行阶乘运算的事实标签。

4

1 回答 1

0

您需要在计算结果的所有地方使用 16 位寄存器:

  mov cx, [ebp+4]    ; cx instead of cl
  mov [ebp+6], cx    ; cx instead of cl

  ...

  mov [ebp+8], ax    ; ax instead of al

while:
   dec word [ebp+6]
   mov ax, [ebp+8]   ; ax instead of al
   mov dx, 0
   mul word [ebp+6]

该代码适用于我的这些更改(给 6 作为输入给我 720 作为输出)。

于 2013-08-06T12:10:34.227 回答