0

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

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.

alpha a
  • 11
  • 1
  • 4
    Try to get something simple to work first, then increase complexity slowly keeping it working, knowing that the area to focus on is the latest additions. – Erik Eidt Dec 24 '22 at 21:41
  • 2
    But one obvious issue is that you put data in the middle of code. – Jester Dec 24 '22 at 22:53
  • 1
    Your asm function is using integers, not double-precision floating point. Also, as Jester says you randomly put a `dw 0` in the middle or your program. IIRC, that's `add [bx+si], al` so it might not actually break anything in a DOS program, but if you store any data there it'll be a different instruction. That's an obvious bug, not worth looking for more until fixing that. If it still doesn't work, single-step your code with a debugger, and include the results in your [mcve] - what values do you see in registers. And what *does* this function do, vs. what you want, for some inputs? – Peter Cordes Dec 24 '22 at 23:16
  • @Peter. I edited the function: and placed the data at the end of it after ```ret```. I also edited the C function to show that parameters are guaranteed to be unsigned integers. But, it's not working yet – alpha a Dec 25 '22 at 08:53
  • You fixed that bug of having `dw` mixed with machine code, but it's not a [mcve]. (What's `matrix`? Is that instead of function args?) It also doesn't show what's wrong for any test cases, or any of the internal variable values you get along the way with C vs. asm. See https://idownvotedbecau.se/nodebugging/ and https://idownvotedbecau.se/itsnotworking/ for why this is a problem. From a quick glance through the code, it's weird that you're doing 8-bit division when you have 16-bit integers. In fact it's weird that you're using `div` at all for powers of 2 instead of `shr ax, 2`. – Peter Cordes Dec 25 '22 at 08:56
  • It's also strange that you do `add dx,bx` right after `mul ax`, so you're adding something to the high half of the DX:AX product. (Your input numbers were apparently 8-bit, unlike in C where they're `unsigned` so at least 16-bit, not `uint8_t`, so the high half of the products is I think always going to be zero.) Anyway, presumably that's the bug, not realizing how `mul` works. – Peter Cordes Dec 25 '22 at 09:00
  • @Peter Here is the syntax. It is placed in the ".data" section ``` .data matrix db 2 dup(?) db 2 dup(?) db 2 dup(?) db 2 dup(?) ``` – alpha a Dec 25 '22 at 09:02
  • @Peter I should add dx,ax. Instead of mov bx,ax then add dx,bx. It's just an unnecessary step. – alpha a Dec 25 '22 at 09:11
  • 1
    No, read the manual for `mul` (https://www.felixcloutier.com/x86/mul), it overwrites DX when used with a 16-bit source. (And see the duplicates I added about that). If you single-step with a debugger, you'll see DX getting zeroed by `mul ax`. If you're sure your `centerX - x` fits in 8 bits, you could use `mul al`. – Peter Cordes Dec 25 '22 at 09:52

0 Answers0