为什么我在装配 8086 中出现此除法错误?



我在汇编中有一个从我的 C 代码调用的函数,它需要 3 个素数 p1、p2、p3 和其他 3 个整数 r1、r2、r3,其中每个 r1、r2、r3 整数都小于所有p1、p2、p3素数。现在我应该找到一个满足n%p_i=r_i , 0<p1*p2*p3> 的n

然后我被告知通过检查从0p1*p2*p3-1的所有数字来找到这个n,所以我做了以下操作:C 代码:

#include <stdio.h> 
#include <stdlib.h> 
extern int solve_equation(int p1, int p2, int p3, int r1, int r2, int r3); 
int main() {
int p1=3, p2=5, p3=7, rem1=1, rem2=2, rem3=3;
printf("Your integer is: %dn", solve_equation(p1, p2, p3, rem1, rem2, rem3));
}

我的组装 :

.MODEL small
.STACK 100H
.DATA
my_mul DW ?
.CODE
PUBLIC _solve_equation
_solve_equation PROC FAR
.386
.387
PUSH BP
MOV BP,SP
MOV CX,0
XOR AX,AX
XOR DX,DX
MOV AX,WORD PTR [BP+4];AX=P1
MUL WORD PTR [BP+6];AX=P1*P2
MUL WORD PTR [BP+8];AX=P1*P2*P3
DEC AX
MOV my_mul,AX;my_mul=p1*p2*p3-1
XOR CX,CX;CX will act as n ,start n=0
MY_LOOP:
XOR AX,AX
XOR DX,DX
CMP CX,my_mul;if n went above p1*p2*p3-1 then stop looping
JA finish
MOV AX,CX;AX=n
DIV WORD PTR [BP+4];divide by p1
CMP DX,WORD PTR [bp+10];compare n%p1 with r1
JNE continue;if not equal then continue to the next iteration 
XOR AX,AX
XOR DX,DX
MOV AX,CX;AX=n
DIV WORD PTR [BP+6];divide by p2
CMP DX,WORD PTR [BP+12];compare n%p2 with r2
JNE continue;if not equal then continue to the next iteration 
XOR AX,AX
XOR DX,DX
MOV AX,CX;AX=n
DIV WORD PTR [BP+8];divide n by p3
CMP DX,WORD PTR [BP+14];compare n%p3 with r3
JNE continue
MOV AX,CX; I get here if I've found the said n, so I save in AX for return value before qutting the function
JMP finish;end function 
continue:
INC CX;n=n+1
JMP MY_LOOP
finish:
POP BP
RET
_solve_equation ENDP
END

运行程序时,我不断收到除法错误。 为什么?

将过程更改为 NEAR 解决了问题

最新更新