两个大整数相乘的汇编语言实现
.386
.model flat,stdcall
option casemap:none
includelib msvcrt.lib
include msvcrt.inc
printf PROTO C :dword, :VARARG
scanf PROTO C :ptr sbyte,:vararg
.data
szFmt1 byte '%d',0
szFmt2 byte 'Please input the lenth of NumA',0ah,0
szFmt3 byte 'Please input NumA',0ah,0
szFmt4 byte 'Please input the lenth of NumB',0ah,0
szFmt5 byte 'Please input NumB',0ah,0
szFmt byte '%d',0
NumA byte '5552411648486158345165841513184515313515465435131548543413135485468543513513543541513515366661651295'
NumB byte '3331513184515313515465435131548543413135485468543513511513184515313515465435131548543413188889878396'
A byte 100 dup(?)
B byte 100 dup(?)
Result dd 300 dup(?)
LenA dd 100
LenB dd 100
Max dd 300
ten dd 10
.code
start proc
mov al,'0'
mov ecx,LenA
mov ebx,0
e1: sub NumA[ebx],al ;第一个操作数 - 0
mov dl,NumA[ebx]
mov A[ecx-1],dl ;倒序存放到A
inc ebx
loop e1
mov ebx,0
mov ecx,LenB
e2: sub NumB[ebx],al ;第二个操作数 - 0
mov dl,NumB[ebx]
mov B[ecx-1],dl ;倒序存放到B
inc ebx
loop e2
mov ecx,0
xor eax,eax
xor ebx,ebx
e3:
mov edx,0
e4: mov al,A[ecx] ;相乘
mov bl,B[edx]
imul eax,ebx ;两个个位数相乘不会大于127
mov edi,ecx
add edi,edx
add Result[edi*4],eax
inc edx
cmp edx,LenB
jl e4
inc ecx
cmp ecx,LenA
jl e3
mov ecx,Max ;将进位加到前一位上
mov ebx,10
mov edi,0
e5: cmp Result[edi*4],ebx ;一定记得dd 乘4 !!!!
jl e6
mov eax,Result[edi*4]
xor edx,edx
idiv ebx
add Result[edi*4+4],eax
mov Result[edi*4],edx
e6: inc edi
loop e5
mov ecx,Max
e7: dec ecx ;除去前导0
cmp Result[ecx*4],0
jz e7
e8: push ecx
invoke printf,offset szFmt,Result[ecx*4] ;倒序输出
pop ecx
dec ecx
cmp ecx,0
jnl e8
ret
start endp
end start