title: Assembly 8086: Append at the end of file
body: <p>I know this is a simple problem, but I really don't know how to solve this. 
  I'm using this code</p>
  
  <pre><code>mov bx, handle
  mov dx, offset data
  mov cx, 100
  mov ah, 40h
  int 21h
  </code></pre>
  
  <p>to write to a file. But I need to "update" it; sort of like appending at the end of it. Is it possible? If so, how would I do it? Is there a specific instruction for it? Thanks! </p>
  
  <p>This is my code:</p>
  
  <pre><code>.model small
  .data
      filename db "test1.txt", 0
      handle dw ?
      data db "write me! "
      buffer db 200 dup(' ') 
      errormess db "Error in opening file!$"
  .stack 100h
  .code
  
      main proc
  
      mov ax, @data
      mov ds, ax
  
      mov AH,3dh
      mov AL,2
      lea dx,filename
      int 21h
  
      mov handle,AX
      jc erroropen
      jmp noerror
      erroropen:
          lea dx, errormess
          mov ah, 09h
          int 21h
          jmp exit
      noerror:
  
      mov bx, handle
      mov ah, 42h  ; "lseek"
      mov al, 2    ; position relative to end of file
      mov cx, 0    ; offset MSW
      mov dx, 0    ; offset LSW
      int 21h
  
      ;mov bx, handle
      mov dx, offset data
      mov cx, 100
      mov ah, 40h
      int 21h ; write to file...
  
      mov bx, handle
      mov ah, 3eh
      int 21h ; close file... 
  
      exit:
      mov ax, 4c00h
      int 21h
  
      main endp
      end main
  </code></pre>