Home > database >  Move a character left, right, up or down until a key is pressed
Move a character left, right, up or down until a key is pressed

Time:12-13

dosseg
.model small
.386
.stack 0100H
.data
    col db 0CH
    row db 28H
.code
        main proc
            mov ax, @data
            mov ds, ax
            
            mov ax, 0003H
            int 10H
            
            call start2
            Call cls
            Mov cx, 0079H
A:          Call disp
            call get
            push cx         
            call dlay
            call cls    
            pop cx
            loop a 
            call finish
            
        main endp
        
        start2 proc
            mov ah, 02h
            mov bx, 0000h
            mov dx, 0c28h
            int 10h
                        
            mov ah, 02h
            mov dx, 0b2h
            int 21H
        start2 endp
        
        disp proc
            mov ah, 02h
            mov bx, 0000h
            mov dh, col
            mov dl, row
            int 10h
                    
            cont:
            MOV AH, 02H
            MOV DX, 0B2H                        
            INT 21H
            RET
        disp endp
        
        get proc
            mov ah, 07H
            int 21h
            mov bl, al
            
            cmp al, 'a'
            je Left
            cmp al, 'd'
            je Right
            cmp al, 'w'
            je Up
            cmp al, 's'
            je Down
            cmp al, 'q'
            je stop
            
            ret
                    
        get endp
        
        cls proc
            mov ax, 0600h
            mov bx, 0700h
            mov cx, 0000h
            mov dx, 2479H
            int 10h
        cls endp
        
        dlay proc
            mov cx, 0100H
        X:  PUSH cx
            mov cx, 0FFFFH
        Y:  Loop Y
            pop cx
            loop x 
            ret
        dlay endp
        
Left:   Dec DL
        mov col, DL
        jmp cont

Right:  Inc DL
        Mov col, DL
        jmp cont
            
Up:     Dec DH
        Mov row, DH
        jmp cont
            
Down:   Inc DH
        Mov row, DH
        jmp cont
        
        finish proc
stop:       mov ax, 4c00H
            int 21H
        finish endp
    
end

'Anyways

So im trying to make a code that moves the a character left, right, up, and down and continuously until w,a,s, or d key is pressed or hits the border of the screen (not yet implemented) with a blinking effect from the clear screen but the problem is the moment i press either wasd the program crashes, it works if i press q to terminate the program but other than that it crashes the moment any w,a,s, or d key is pressed.

What am i doing wrong is it the order of the calls or the procs themselves. '

CodePudding user response:

col db 0CH
row db 28H

These values are to be reversed. Column at 40 and Row at 12.

mov dh, col
mov dl, row

These registers don't match the names. DL is Column and DH is Row.

mov dx, 2479H

This value is taking decimals for hexadecimals. The bottomright corner of the screen is at (79,24) or (4Fh,18h) or mov dx, 184Fh.


The Left, Right, Up, and Down codes are using DL and DH registers that have been clobbered by the passage through disp.

The start2 and cls procedures are missing the ret instruction. I don't think endp does that for you.

The start2 procedure is totally redundant. You don't need it at the very beginning at all, due to the call cls that immediately follows.

It's irregular to jump out of a proc like you do in get and then to jump into a proc like you do in disp (at the label cont). Next is a better get procedure. Use it with modifying the main loop to read ... call disp call get call disp ...

get proc
  mov  ah, 07h
  int  21h
  cmp  al, 'a'
  je   Left
  cmp  al, 'd'
  je    Right
  cmp  al, 'w'
  je   Up
  cmp  al, 's'
  je   Down
  cmp  al, 'q'
  je   stop
  ret
Left:
  dec  col
  ret
Right:
  inc  col
  ret
Up:
  dec  row
  ret
Down:
  inc  row
  ret
get endp

CodePudding user response:

I think you migh want the cont label to be moved there:

        get proc
            mov ah, 07H
            int 21h
            mov bl, al
            
            cmp al, 'a'
            je Left
            cmp al, 'd'
            je Right
            cmp al, 'w'
            je Up
            cmp al, 's'
            je Down
            cmp al, 'q'
            je stop
            
cont:       ret

So that the function call resumes cleanly from just before it jumped to the key-specific case handle.

  • Related