简体   繁体   English

大会中的单词大小mul

[英]Word size mul in Assembly

I'm making a subroutine for a factorial operation. 我正在为一个因子操作做一个子程序。 All of my local variables are in word size since I'm using stack for pass-by-value. 我的所有局部变量都是字大小的,因为我使用堆栈来传递值。

[ebp+4] is the counter/decrementing multiplier
[ebp-2] is the multiplicand

Currently, I have this code block like this: 目前,我有这样的代码块:

while1:

    cmp byte[ebp+4], 1
    je exit1
    dec word[ebp+4]
    mov ax, [ebp-2]
    mov dx, 0
    mul byte[ebp+4]
    mov word[ebp-2], ax

    jmp while1

And it has these following input and output results: 它具有以下输入和输出结果:

1 -> 02561
2 -> 2
3 -> 6
4 -> 24
5 -> 120
6 -> 208
7 -> 176
8 -> 128

It's only correct from 2 to 5. I realized that I'm making a byte-sized multiplication because of mul byte[ebp+4] , thus, hindering the result of 6! 从2到5只是正确的。我意识到由于mul byte[ebp+4] ,我正在进行字节大小的乘法,因此阻碍了6的结果! = 720. So I changed mul byte[ebp+4] to mul word[ebp+4] : = 720.所以我将mul byte[ebp+4]改为mul word[ebp+4]

while1:

    cmp byte[ebp+4], 1
    je exit1
    dec word[ebp+4]
    mov ax, [ebp-2]
    mov dx, 0
    mul word[ebp+4]
    mov word[ebp-2], ax

    jmp while1

It then has these following input and output results: 然后它具有以下输入和输出结果:

1 -> 02561
2 -> 07682
3 -> 28166
4 -> 62488
5 -> 46200
6 -> 60112
7 -> 35760
8 -> 15744

What am I doing wrong? 我究竟做错了什么? I already cleared in the dx before the mul operation, but why is it not functioning properly? mul操作之前我已经在dx中清除了,但为什么它不能正常工作?

Note: I have the correct printing routine of a word-size variable to 5 characters in ASCII for stdout. 注意:对于stdout,我有一个字大小变量的正确打印例程,ASCII格式为5个字符。 Also, I have a special case of 0! 另外,我有一个0的特例! done in cmp before the iteration code block. 在迭代代码块之前在cmp中完成。

Im using Ubuntu 13 x86 and NASM. 我使用Ubuntu 13 x86和NASM。


update. 更新。 here's the code block right on top of the while1 : 这是在while1顶部的代码块:

fact: 

    mov ebp, esp
    sub esp, 2
    mov ax, [ebp+4]
    mov word[ebp-2], ax

    ; checks if num is zero
    cmp byte[ebp+4], 0
    je one

If cmp word[ebp+4], 1 isn't working, that indicates that what's at word[ebp+4] may not be what you expect or what it should be. 如果cmp word[ebp+4], 1不起作用,则表示单词[ebp + 4]中的内容可能不是您所期望的或它应该是什么。 The upper byte of the word is evidently not 0 . 该字的高字节显然不是0 Using the byte compare instead is covering up an issue somewhere. 使用字节比较代替了某个地方的问题。 So you'll need to examine what's happening with the word at that location. 因此,您需要检查该位置的单词发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM