79350749

Date: 2025-01-12 21:40:05
Score: 0.5
Natty:
Report link

Please help me understand what went wrong ;(

So many things that are wrong in this program! The AI wasn't even able to draw a solid square at (x,y) using BIOS.WritePixel. (A solid white square could get displayed in the upper left corner of the screen). And why a different code was needed to wipe the square is beyond me. Same procedure, other set of arguments.

push bx
pop  bx

BX wasn't initialized to anything, so why waste bytes on preserving BX.

mov ax, y
add ax, cx
mov si, ax

BIOS.WritePixel expects its Y coordinate in the DX register, so very much not SI.

mov ax, x
add ax, dx
mov di, ax

BIOS.WritePixel expects its X coordinate in the CX register, so very much not DI.

My version:

  mov  cx, x
  mov  dx, y
  mov  al, 15           ; To 'clear' the square, you pass AL=0
  call PaintSquare

  ...

; IN (al,cx,dx)
PaintSquare:
  push bx               ; AL is Color=[0,255]
  push si               ; CX is X=[0,319]
  push di               ; DX is Y=[0,199]

  mov  bh, 0            ; DisplayPage
  mov  di, size_square  ; Height
.OuterLoop:
  mov  si, size_square  ; Width
.InnerLoop:
  mov  ah, 0Ch          ; BIOS.WritePixel
  int  10h
  inc  cx               ; X++
  dec  si
  jnz  .InnerLoop
  sub  cx, size_square
  inc  dx               ; Y++
  dec  di
  jnz  .OuterLoop
  sub  dx, size_square

  pop  di
  pop  si
  pop  bx
  ret
prev_x dw 100      ; Previous x position of square
prev_y dw 100      ; Previous y position of square

The AI is loading prev_x and prev_y with the coordinates where the mouse click was registered. This is unrelated to where the square needs to be erased!
Moreover, before any dragging can begin, you still need to check whether the mouse is actually on top of the current square.

exit_program:
  ; Restore text mode
  mov ax, 3
  int 10h
  ret

Although the AI included this nice 'exit_program' part, this code is unreachable and if jumped at, its ret could only return to DOS is this were a .COM executable. Sadly, the .model small says this is going to be an .EXE executable, for which additionally the DS segment register wasn't setup and therefore addressing the memory-based variables will fail completely.

The list goes on...


My suggestion then would be that you forget about using the mouse for now, and that, with the help of my PaintSquare procedure, you try your hand at a version of the program that moves the square based on the arrow keys on the keyboard. In the intrest of flicker-free graphics you could use a double buffer like you described in one of your comments. Good luck.

Reasons:
  • Blacklisted phrase (1): help me
  • RegEx Blacklisted phrase (3): Please help me
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-2):
Posted by: Sep Roland