how do you multiply word-size inputs and get its word-size product in stack?

感情迁移 提交于 2019-12-11 15:48:04

问题


    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

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?

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


回答1:


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]

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



来源:https://stackoverflow.com/questions/18078695/how-do-you-multiply-word-size-inputs-and-get-its-word-size-product-in-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!