admin管理员组

文章数量:1123891

I'm a cs student and I need to build a project for my architecture class. I have tried to build a simple drag and drop program in asm. The whole idea would be to draw a sqaure somewhere on the screen and then drag it somewhere else using the mouse just like you would on a regular desktop.

I have tried using a buffer but it was to no avail. I'm too much of a beginner to make it work. so I tried some functions to redraw the square. Here is the best I've got using Copilot:

.model small
.stack 100h
.data
    x dw 100           ; Initial x position of square
    y dw 100           ; Initial y position of square
    size_square dw 50  ; Size of the square
    isDragging db 0    ; Flag to check if the square is being dragged
    prev_x dw 100      ; Previous x position of square
    prev_y dw 100      ; Previous y position of square

.code
start:
    ; Initialize graphics mode
    mov ax, 13h
    int 10h

    ; Initialize mouse
    mov ax, 0
    int 33h
    mov ax, 1
    int 33h

    ; Hide mouse cursor
    mov ax, 2
    int 33h

    ; Draw initial square
    call draw_square

main_loop:
    ; Show mouse cursor
    mov ax, 1
    int 33h

    ; Get mouse status
    mov ax, 3
    int 33h

    ; Check if left button is pressed
    test bx, 1
    jz no_drag

    ; Start dragging
    mov isDragging, 1
    mov prev_x, cx
    mov prev_y, dx

dragging:
    ; Hide mouse cursor while dragging
    mov ax, 2
    int 33h

    ; Get mouse status
    mov ax, 3
    int 33h

    ; Check if button is still pressed
    test bx, 1
    jz stop_drag

    ; Clear previous square position
    call clear_square

    ; Update square position
    mov x, cx
    mov y, dx

    ; Draw square at new position
    call draw_square

    ; Store current position as previous position
    mov prev_x, x
    mov prev_y, y

    jmp dragging

no_drag:
    ; If not dragging, wait for next mouse event
    jmp main_loop

stop_drag:
    ; Stop dragging
    mov isDragging, 0
    ; Show mouse cursor
    mov ax, 1
    int 33h
    jmp main_loop

draw_square:
    ; Draw the square
    push si
    push di
    push cx
    push bx
    push dx
    mov cx, 0
draw_loop1:
    mov dx, 0
draw_loop2:
    mov ax, y
    add ax, cx
    mov si, ax
    mov ax, x
    add ax, dx
    mov di, ax
    mov al, 15
    mov ah, 0Ch
    int 10h

    inc dx
    cmp dx, size_square
    jl draw_loop2

    inc cx
    cmp cx, size_square
    jl draw_loop1

    pop dx
    pop bx
    pop cx
    pop di
    pop si
    ret

clear_square:
    ; Clear the previous square position
    push si
    push di
    push cx
    push bx
    push dx
    mov cx, 0
clear_loop1:
    mov dx, 0
clear_loop2:
    mov ax, prev_y
    add ax, cx
    mov si, ax
    mov ax, prev_x
    add ax, dx
    mov di, ax
    mov al, 0
    mov ah, 0Ch
    int 10h

    inc dx
    cmp dx, size_square
    jl clear_loop2

    inc cx
    cmp cx, size_square
    jl clear_loop1

    pop dx
    pop bx
    pop cx
    pop di
    pop si
    ret

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

end start

Please help me understand what went wrong ;(

本文标签: Drag and Drop project in assembly 8086 (move a square with the mouse)Stack Overflow