我目前工作的一个任务,在那里我写一个子程序,其中2张无符号数相乘得到,并在DX产生一个结果:AX对。但我不能使用mul,imul,div和idiv的说明。当我运行我的代码时,下半部分(AX寄存器)总是正确的,但DX寄存器不是。任何人都可以指出我正确的方向,我做错了什么?乘两个16位无符号值,而无需使用乘法和除法指令[8086大会]
;-----------------------------------------------------------
;
; Program: MULTIPLY
;
; Function: Multiplies two 16 bit unsigned values ...
; .... duplicating the MUL instruction
;
; Input: The two values to be multiplied are passed on the stack
; The code conforms to the C/C++ calling sequence
;
; Output: The 32 bit result is returned in the dx:ax pair
; Registers required by C/C++ need to be saved and restored
;
; Owner: Andrew F.
;
; Changes: Date Reason
; ------------------
; 07/20/2013 Original version
;
;
;---------------------------------------
.model small
.8086
public _multiply
.data
;---------------------------------------
; Multiply data
;---------------------------------------
.code
;---------------------------------------
; Multiply code
;---------------------------------------
_multiply:
push bp ; save bp
mov bp,sp ; anchor bp into the stack
mov ax,[bp+4] ; load multiplicand from the stack
mov dx,[bp+6] ; load multiplier from the stack
push bx
push cx
push di
;---------------------------------------
; copy ax to cx, and dx to bx
;---------------------------------------
mov cx,ax ;using bx and cx as my inputs
mov bx,dx
;---------------------------------------
; Check for zeros, zero out ax and dx
;---------------------------------------
start:
xor ax,ax ; check for multiplication by zero
mov dx,ax ; and zero out ax and dx
mov di,cx ;
or di,bx ;
jz done ;
mov di,ax ; DI used for reg,reg adc
;---------------------------------------
; loop/multiply algorithm
;---------------------------------------
loopp:
shr cx,1 ; divide by two, bottom bit moved to carry flag
jnc skipAddToResult ;no carry -> just add to result
add ax,bx ;add bx to ax
adc dx,di ;add the carry to dx
skipAddToResult:
add bx,bx ;double bx current value
or cx,cx ; zero check
jnz loopp ; if cx isnt zero, loop again
;---------------------------------------
; Restore register values, return
;---------------------------------------
done:
pop di ;restore di
pop cx ;restore cx
pop bx ;restore bx
pop bp ; restore bp
ret ; return with result in dx:ax
;
end ; end source code
;---------------------------------------
当加倍“bx”时,您需要考虑进入“di”,并且您还需要将di加倍。我想'adc di,di'会起作用。 PS:学会使用一个调试器,这样你可以浏览你的代码,看看它出错的地方。 – Jester