Writing to a file in assembler

北战南征 提交于 2019-12-01 09:05:39

You have to create the file first (or open it if it already exists), then write the string, and finally close the file. Next code is MASM and made with EMU8086, I post it because it may help you to understand how to do it, interrupts are the same, as well as parameters, so the algorithm :

.stack 100h
.data

text db "Adam$"
filename db "name.txt",0
handler dw ?

.code          
;INITIALIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax

;CREATE FILE.
  mov  ah, 3ch
  mov  cx, 0
  mov  dx, offset filename
  int  21h  

;PRESERVE FILE HANDLER RETURNED.
  mov  handler, ax

;WRITE STRING.
  mov  ah, 40h
  mov  bx, handler
  mov  cx, 5  ;STRING LENGTH.
  mov  dx, offset text
  int  21h

;CLOSE FILE (OR DATA WILL BE LOST).
  mov  ah, 3eh
  mov  bx, handler
  int  21h      

;FINISH THE PROGRAM.
  mov  ax,4c00h
  int  21h           
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!