你怎么乘字长的投入并获得其字长产品堆叠? [英] how do you multiply word-size inputs and get its word-size product in stack?

查看:180
本文介绍了你怎么乘字长的投入并获得其字长产品堆叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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只,而单词的大小是高达65655。
我应该在我的code被改变或修正?

I am computing for factorial within a loop. 5! results to 120 but 6! results to 208 Byte-size is upto 255 only while word-size is upto 65,655. What should be changed or corrected in my code?

有我的code。不介意的权力,退出的标签,注释。只是心中其中执行阶乘操作的事实标签。

There's my code. dont mind the power, exit label, comments. just mind the fact label where factorial operation is executed.

推荐答案

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

You need to use 16-bit registers in all places where you're calculating the result:

  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]

在code工作对我来说这些变化(给6作为输入给了我720作为输出)。

The code works for me with those changes (giving 6 as input gave me 720 as output).

这篇关于你怎么乘字长的投入并获得其字长产品堆叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆