用汇编实现双字相乘
上周同学让我用汇编写一个 32位 X 32位 的程序
要求键盘输入乘数和被乘数
忙活了老半天才搞定。
下面是部分代码(与大家共同学习)
读取键盘 调用21号中断
;======================
;入口 Bx 存放读入数据
read proc
push cx
push ax
push dx
mov dx,0
mov ch,8
next1: cmp ch,4
jnz n2
push ax
push dx
mov dl,' '
mov ah,2
int 21h
pop dx
pop ax
n2: mov ah,7
int 21h
cmp al,13
je over
push dx
mov dl,al
call check
mov cl,al
cmp cl,0
jnz next2
pop dx
jmp next1
next2: mov ah,2
int 21h
mov al,cl
pop dx
cmp ch,4
jnz full
mov [bx],dx
mov dx,0
full: mov cl,4
rol dx,cl
add dl,al
dec ch
jnz next1
over: mov [bx+2],dx
pop dx
pop ax
pop cx
ret
read endp
检查输入的子程序
;=====================
;入口Al
;出口AL
check proc
cmp al,30h
jl er
cmp al,3ah
jnl b1
sub al,30h
jmp ok
b1: cmp al,41h
jl er
cmp al,47h
jnl b2
sub al,37h
jmp ok
b2: cmp al,61
jl er
cmp al,67h
jnl er
sub al,57h
jmp ok
er: mov al,0h
ok: ret
check endp
关键的乘法部分
;===========相乘=========
mult proc
push ax
push bx
push cx
push dx
push di
mov ax,offset res
mov di,ax
mov bx,offset d1
mov ax,[bx+6]
mov cx,[bx+2]
mul cx
add [di+6],ax
add [di+4],dx
mov ax,[bx+4]
mul cx
add [di+4],ax
adc [di+2],dx
mov cx,[bx]
mov ax,[bx+6]
mul cx
add [di+4],ax
adc [di+2],dx
adc word ptr[di],0
mov ax,[bx+4]
mul cx
add [di+2],ax
adc [di],dx
pop di
pop dx
pop cx
pop bx
pop ax
ret
mult endp
以下是全部代码 在window 2000 下用TD编译通过(我是轻工学院的)
d1 dw 4 dup(0)
res dw 4 dup(0)
info db ' (H)','$'
info1 db 'Input the Num1 : ','$'
info2 db 'Input the Num2 : ','$'
info3 db 'Num1 * Num2 = ','$'
info4 db 0dh,0ah,'$'
adata ends
astack segment para stack 'stack'
sta dw 100 dup(?)
top label word
astack ends
acode segment
assume cs:acode,ds:adata
main proc far
start: mov ax,astack
mov ss,ax
mov sp,offset top
push ds
mov ax,0
push ax
mov ax,adata
mov ds,ax
;=============end init======
call en
mov dx,offset info1
mov ah,9
int 21h
mov bx,offset d1
call read
call h
call en
mov dx,offset info2
mov ah,9
int 21h
add bx,4
call read
call h
call en
call en
mov dx,offset info3
mov ah,9
int 21h
;============multiply===
call mult
;=========display result=====
mov bx,offset res
call dispall
call h
call en
call en
ret
main endp
;===========相乘=========
mult proc
push ax
push bx
push cx
push dx
push di
mov ax,offset res
mov di,ax
mov bx,offset d1
mov ax,[bx+6]
mov cx,[bx+2]
mul cx
add [di+6],ax
add [di+4],dx
mov ax,[bx+4]
mul cx
add [di+4],ax
adc [di+2],dx
mov cx,[bx]
mov ax,[bx+6]
mul cx
add [di+4],ax
adc [di+2],dx
adc word ptr[di],0
mov ax,[bx+4]
mul cx
add [di+2],ax
adc [di],dx
pop di
pop dx
pop cx
pop bx
pop ax
ret
mult endp
;===========enter
en proc
push ax
push dx
mov dx,offset info4
mov ah,9
int 21h
pop dx
pop ax
ret
en endp
;==============display (H)
h proc
push ax
push dx
mov dx,offset info
mov ah,9
int 21h
pop dx
pop ax
ret
h endp
;====================================
;显示入口bx
dispall proc
push cx
push bx
mov cx,4
l1: push cx
mov cl,8
mov ax,[bx]
add bx,2
call print
pop cx
loop l1
pop bx
pop cx
ret
dispall endp
;======================
;入口 Bx 存放读入数据
read proc
push cx
push ax
push dx
mov dx,0
mov ch,8
next1: cmp ch,4
jnz n2
push ax
push dx
mov dl,' '
mov ah,2
int 21h
pop dx
pop ax
n2: mov ah,7
int 21h
cmp al,13
je over
push dx
mov dl,al
call check
mov cl,al
cmp cl,0
jnz next2
pop dx
jmp next1
next2: mov ah,2
int 21h
mov al,cl
pop dx
cmp ch,4
jnz full
mov [bx],dx
mov dx,0
full: mov cl,4
rol dx,cl
add dl,al
dec ch
jnz next1
over: mov [bx+2],dx
pop dx
pop ax
pop cx
ret
read endp
;=====================
;入口Al
;出口AL
check proc
cmp al,30h
jl er
cmp al,3ah
jnl b1
sub al,30h
jmp ok
b1: cmp al,41h
jl er
cmp al,47h
jnl b2
sub al,37h
jmp ok
b2: cmp al,61
jl er
cmp al,67h
jnl er
sub al,57h
jmp ok
er: mov al,0h
ok: ret
check endp
;=====================
;入口ax
print proc
push cx
push bx
mov bx,ax
mov ch,4
next: mov cl,4
rol bx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,3Ah
jl disp
add al,7
disp: mov ah,2
mov dl,al
int 21h
dec ch
jnz next
pop bx
pop cx
mov dl,' '
mov ah,2
int 21h
ret
print endp
acode ends
end start