How to set boundary for moving something around the screen when user presses keys, in assembly?

Say I have a code that makes a dot move around in the es (extra seg) with the wasd keys. How do I check if the dot is at the first line and if so, it doesn’t move?
(i asked my teacher if I need to use “cmp” and he said no)

In the proc of the up motion; (I took bx and set it to 0 to check if si is also 0, then i just made a loop that checks if they’re equal until bx is no longer on the first row) when i ran it, it didn’t respond when i pressed ‘w’.

proc up
    mov ah, 0
    mov al, ' '
    mov [es:si], ax
    
    sub si, 80*2
    mov ah, 156
    mov al, '*'
    mov [es:si], ax
  ret
endp up

I tried to use cmp

proc up
    mov ah, 0
    mov al, ' '
    mov [es:si], ax
    
        mov bx, 0
check:
    cmp bx, 80*2
    jz move
    cmp si, bx
    jz not_move
    inc bx
    jmp check
move:
    sub si, 80*2
    mov ah, 156
    mov al, '*'
    mov [es:si], ax
not_move:
  ret
endp up

  • You should consider how this might work in a language like C or another that you know well. The point is that you don’t have to think in assembly when you’re working out an algorithm. An algorithm will offer the variables and statements you need to get the job done. So, do that first, and only then, do the same thing in assembly.

    – 

  • You don’t need to use cmp because sub already sets the carry flag on wrap-around from 0 to 65535. (cmp is the same as sub except not modifying the first operand, so you could avoid it for other directions as well, if you wanted to. It will be useful for those but you still don’t literally need it, you could work around it with sub or something. Or I guess you could use div to do a modulo for at least the down and right directions, but that’s slower and probably not easier.)

    – 




Leave a Comment