I tried to convert the following C function to assembly emu8086. But, the function is not working. There are some bugs. The function is supposed to check if a set of 4 points forms a rectangle. The C function work correctly.
C function
bool isRectangle(unsigned int x1, unsigned int y1, unsigned int x2,
unsigned int y2, unsigned int x3, unsigned int y3, unsigned int x4,
unsigned int y4)
{
double cx,cy;
double dd1,dd2,dd3,dd4;
cx=(x1+x2+x3+x4)/4;
cy=(y1+y2+y3+y4)/4;
dd1=(cx-x1) * (cx-x1) + (cy-y1) * (cy-y1);
dd2=(cx-x2) * (cx-x2) + (cy-y2) * (cy-y2);
dd3=(cx-x3) * (cx-x3) + (cy-y3) * (cy-y3);
dd4=(cx-x4) * (cx-x4) + (cy-y4) * (cy-y4);
return dd1==dd2 && dd1==dd3 && dd1==dd4;
}
Assemply function
isRectangle PROC
xor ax,ax
mov al, matrix[0]
xor dx,dx
mov dl,matrix[2]
add ax,dx
xor dx,dx
mov dl,matrix[4]
add ax,dx
xor dx,dx
mov dl,matrix[6]
add ax,dx
xor dx,dx
mov dl,4
div dl
xor ah,ah
mov centerX,ax
xor ax,ax
mov al, matrix[1]
xor dx,dx
mov dl,matrix[3]
add ax,dx
xor dx,dx
mov dl,matrix[5]
add ax,dx
xor dx,dx
mov dl,matrix[7]
add ax,dx
xor dx,dx
mov dl,4
div dl
xor ah,ah
mov centerY,ax
mov ax,centerX
xor cx,cx
mov cl,matrix[0]
sub ax,cx
mul ax
mov dx,ax
mov bx,centerY
xor cx,cx
mov cl,matrix[1]
sub bx,cx
mov ax,bx
mul ax
add dx,ax
mov dd1,dx
mov ax,centerX
xor cx,cx
mov cl,matrix[2]
sub ax,cx
mul ax
mov dx,ax
mov bx,centerY
xor cx,cx
mov cl,matrix[3]
sub bx,cx
mov ax,bx
mul ax
add dx,ax
mov dd2,dx
mov ax,centerX
xor cx,cx
mov cl,matrix[4]
sub ax,cx
mul ax
mov dx,ax
mov bx,centerY
xor cx,cx
mov cl,matrix[5]
sub bx,cx
mov ax,bx
mul ax
mov bx,ax
add dx,bx
mov dd3,dx
mov ax,centerX
xor cx,cx
mov cl,matrix[6]
sub ax,cx
mul ax
mov dx,ax
mov bx,centerY
xor cx,cx
mov cl,matrix[7]
sub bx,cx
mov ax,bx
mul ax
mov bx,ax
add dx,bx
mov dd4,dx
xor ax,ax
mov ax,dd1
cmp ax,dd2
jne return
cmp ax,dd3
jne return
cmp ax,dd4
jne return
PRINT "The shape is rectangle"
int 21h
; draw the rectangle
ret
return:ret
centerX dw 0
centerY dw 0
dd1 dw 0
dd2 dw 0
dd3 dw 0
dd4 dw 0
isRectangle ENDP
matrix is placed in the ".data" segment Here is its syntax
matrix db 2 dup(?)
db 2 dup(?)
db 2 dup(?)
db 2 dup(?)
I reviewed the code many times, but I could not find the bugs.