在汇编语言程序中连续两次相乘 [英] multiply two consecutive times in assembly language program
问题描述
我正在使用8086仿真器以及DOSBOX和MASM.
I am using 8086 emulator and DOSBOX and MASM.
我知道当我们将8位乘以8位时,答案将是16位.
I know that when we multiply 8-bit with 8-bit, answer will be of 16-bit.
al*(8-bit)=ax
当我们将16位乘以16位时,答案将为32位.
And when we multiply 16-bit with 16-bit,answer will of 32-bit.
ax*(16-bit)=dx & ax
但是,如果答案是(dx& ax)
,并且我想将8位或16位数字相乘,那么它将简单地与ax一起执行,但是我需要将数字与用(dx& ax)
回答.那么如何克服这个问题呢?
But if the answer is in (dx & ax)
and I want to multiply 8-bit or 16-bit number then it will simply perform with ax But I needed to multiply a number with answer in (dx & ax)
. So how to overcome this problem?
我需要为析因程序解决这种情况.我想在哪里找到10个!
I need solve this situation for the factorial program. Where I am trying to find the 10!
推荐答案
假设您必须在十进制系统中执行此操作,例如 37 * 8
.您将计算并添加两个术语: 30 * 8
+ 7 * 8
.第一项可以转换为 3 * 8 * base(10)
.
Imagine you have to do it in the decimal system, e.g. 37 * 8
. You would calculate and add two terms: 30 * 8
+ 7 * 8
. The first term can be transformed to 3 * 8 * base (10)
.
37 * 8
------
56 (7*8)
24 (3*8)
======
296
您会看到"base"的位置为空,因为此值始终为0.
You see the place of "base" is empty since this value is always 0.
现在让我们将基数 10 更改为基数寄存器":
Now let us change base 10 to base "register":
DX:AX * CX
----------
DX:AX (AX*CX)
DX:AX (DX*CX)
==========
XX:XX:XX
结果需要两个乘法和三个词.此外,您必须存储第一次乘法的结果(DX:AX),因为第二次乘法需要寄存器.
You need two multiplications and three words for the result. Furthermore you have to store the result (DX:AX) from the first multiplication because you need the registers for the second multiplication.
这里是10的代码!我省略了结果最左边的单词,因为 3628800 不需要它:
Here's the code for 10! I omit the leftmost word of the result since it isn't needed for 3628800:
.MODEL SMALL
.STACK 1000h
.DATA
Result DD 1 ; 01 00 00 00 (little endian!)
.CODE
main PROC
mov ax, @DATA ; Initialize DS
mov ds, ax
mov cx, 2 ; Start value
L1:
mov ax, WORD PTR Result + 0 ; Low word
mul cx
mov di, dx ; Store high result
mov WORD PTR Result + 0, ax ; Low result won't be changed anymore
mov ax, WORD PTR Result + 2 ; High word
mul cx
add ax, di ; add low result from last mul to low result here
mov WORD PTR Result + 2, ax ; Store it
; adc dx, 0 ; this would store the "highest" word of result (13!)
; mov WORD PTR Result + 4, dx
add cx, 1
cmp cx, 10
jbe L1 ; While CX <= 10
mov ax, 4C00h
int 21h
main ENDP
END main
这篇关于在汇编语言程序中连续两次相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!