title: Snake Assembly 8086: not moving correctly
body: <pre>
  .model small
  .data
  
  ;variables needed for snake body
  
  
  head db '@', '$' ;head and body symbols
  tail db ' ', '$' ;tail part set color to black
  
  snake_length db 3 ; 1-2 body 3 tail
  copy_snake_length db ? ;for loop
  sEnd db ?
  
  head_col db 40 ;starting col for snake 
  head_row db 12 ;starting row for snake
  
  ; snake movement algorithm
  ; always copy the coordinates of the symbol in front.
  copy_col db ?
  copy_row db ?
  
  
  delaytime db 1
  ;variable for control keys
  input db ?
  .stack 100h
  .code
  
  ;to delay time
  delay proc
      mov ah, 00
      int 1Ah
      mov bx, dx
  
  jmp_delay:
      int 1Ah
      sub dx, bx
      cmp dl, delaytime
      jl jmp_delay
      ret
  
  delay endp
  
  ;make snake head go to the right
  
  righty proc
      cmp head_col,79
      je resetposl
  
      zero: 
      inc head_col
      jmp rightyie
  
      resetposl:
      mov head_col, 0
  
  
      rightyie:
      mov dl,head_col
      mov dh,head_row
      xor bh, bh
  
      mov ah, 02h
      int 10h
      ret 
  righty endp
  
  startgame proc
  
      mov dh, 12 ;row
     mov dl, 40 ;column
     xor bh, bh
     mov ah, 02h
     int 10h 
  
      mov dx, offset head
              mov ah, 09h
                  int 21h
  ret
  startgame endp
  
  
  mov cl, head_row
  mov copy_row,cl
  
  mov cl, head_col
  mov copy_col, cl
  
  ;print head
  mov dh, head_row
  mov dl, head_col
  xor bh, bh
  mov ah, 02h
  int 10h 
  mov dx, offset head
  mov ah, 09h
  int 21h
  
  
  
  
  
  main    proc
      mov ax, @data
      mov ds, ax
  
      ;set video mode
      mov al, 03h 
      mov ah, 00h
      int 10h
  
      ;clear screen
      ;only need it once (reason no need to use function)
      mov ax, 0600h
      mov bh, 07h
      xor cx, cx
      mov dx, 184fh
      int 10h
  
      mov cx, 3200h  ;stop cursor blinking
      mov ah, 01h
      int 10h
  
  ;set start head snake in the middle of the screen
      call startgame
  
  
  ;control
  mov ah,00h
  int 16h
  mov input, 'd'
  
  ;to change direction or to keep on going
  getinput:
  mov ah, 01h
  int 16h
  jz key
  mov ah,00h
  int 16h
  mov input,al
  
  ;control keys
  key:
  ;cmp input, 'w'
  ;je w 
  ;cmp input, 's'
  ;je s 
  ;cmp input, 'a'
  ;je a 
  
  cmp input, 'd'
  je d
  
  jne rak
  
  
  d:
  
      mov cl, head_row
      mov copy_row,cl
      mov cl, head_col
      mov copy_col, cl
  
      mov dh, head_row
      mov dl, head_col
      xor bh, bh
      mov ah, 02h
      int 10h     
  
      mov al, tail
      mov bh, 0
      mov bl, 000h
      mov cx, 1
      mov ah, 09h
      int 10h
  
  mov cl, snake_length
  mov copy_snake_length, cl
  dec copy_snake_length
  
  mov bl,0
  
  printbody:
  mov al, head_row
  mov copy_row,al
  mov al, head_col
  mov copy_col, al
  
  call righty
  
  ;print head
  ;coordinates
      mov dh, head_row
      mov dl, head_col
      xor bh, bh
      mov ah, 02h
      int 10h 
  ;printing "@"
      mov dx, offset head
      mov ah, 09h
      int 21h
  
  ; inc copy_col to update the head.
  inc copy_col
  mov al,copy_col
  mov head_col,al
  
  inc bl
  ; now loop to print other characters
  cmp bl,copy_snake_length
  
  jl printbody
  dec head_col
  jmp rak
  
  
  
  
  
  
  rak: 
  call delay
  jmp getinput
  
  
  mov ax, 4c00h
  int 21h
  main endp
  end main
  </pre>
  
  <p>If you want to try out the code press d to move the character.</p>
  
  <p>The problem is that when it reaches the last column, my function righty suppose to change head_col to 0 so that it looks like it reaches the end of the screen. Unfortunately, it leaves out one character at the end col 80 then it continues to print but in the next row.</p>
  
  <p>Then when it reaches the end at row 13 it doesn't leave a character at col 0 but still goes down another row, and after 3 continues printing, it seems that it goes back to the original row, then when it reaches the starting point it doesn't anymore print anything.</p>