为什么我在程序集8086中得到这个除法错误?

时间:2017-06-30 14:41:25

标签: assembly x86-16

我在程序集中有一个函数,从我的C代码调用,它需要3个素数 p1,p2,p3 ,以及其他3个整数 r1,r2,r3 其中每个 r1,r2,r3 整数小于所有 p1,p2,p3 素数。现在我应该找到一个 n ,它满足 n%p_i = r_i,0< n< p1 * p2 * p3 表示 i = 1,2,3

然后通过检查 0 p1 * 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: %d\n", 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

运行程序时,我不断出现除法错误。那是为什么?

1 个答案:

答案 0 :(得分:1)

将程序更改为NEAR修复了问题