;****************************************************************************
; Data segment
;****************************************************************************

data            segment
UserName        db      64,0,64 dup (0)
Msg1            db      "Enter your name: $"
Msg2            db      "Your name is $"
Msg3            db      "File access error",13,10,"$"
data            ends

;****************************************************************************
; Stack segment
;****************************************************************************

stack           segment stack
                dw      128 dup (?)
stack           ends

;****************************************************************************
; Code segment
;****************************************************************************

FILE_OFFSET     equ     202h                    ;Offset of user name from
                                                ;start of .EXE file
code            segment

main            proc    far
                assume  cs:code
                mov     ax,data                 ;Point DS to the program's
                mov     ds,ax                   ;data segment
                assume  ds:data
                cld                             ;Clear direction flag
;
; Ask for a user name if one isn't already recorded.
;
                cmp     byte ptr [UserName+2],0 ;Branch if user name is
                jne     main2                   ;already recorded

                mov     ah,09h                  ;Display the input prompt
                mov     dx,offset Msg1
                int     21h

                mov     ah,0Ah                  ;Input the user name
                mov     dx,offset UserName
                int     21h
;
; Copy the user name to the executable file image.
;
                push    ds                      ;Save DS
                mov     ds,es:[2Ch]             ;DS -> Environment segment
                assume  ds:nothing
                xor     si,si                   ;SI -> Base of segment

main1:          lodsb                           ;Search for the double
                or      al,al                   ;zeros that mark the end
                jne     main1                   ;of the environment
                cmp     byte ptr [si],0         ;strings
                jne     main1
                add     si,3                    ;SI -> File name

                mov     ax,3D02h                ;Open the file
                mov     dx,si
                int     21h
                pop     ds                      ;Restore DS
                assume  ds:data
                jc      error                   ;Jump on file error

                mov     bx,ax                   ;Move the file pointer
                mov     ax,4200h                ;to offset FILE_OFFSET
                xor     cx,cx
                mov     dx,FILE_OFFSET
                int     21h
                jc      error                   ;Jump on error

                mov     ah,40h                  ;Write the user name to
                mov     cx,64                   ;the file
                mov     dx,offset UserName+2
                int     21h
                jc      error                   ;Jump on error

                mov     ah,3Eh                  ;Close the file and
                int     21h                     ;exit
                jmp     short exit
;
; Display the user name.
;
main2:          mov     ah,09h                  ;Display "Your name is"
                mov     dx,offset Msg2
                int     21h

                mov     si,offset UserName+2    ;DS:SI -> User name
main3:          lodsb                           ;Get a character
                or      al,al                   ;Exit if it's zero
                jz      exit
                mov     dl,al                   ;Output the character
                mov     ah,02h
                int     21h
                jmp     main3                   ;Loop until done
;
; Terminate the program.
;
exit:           mov     ax,4C00h                ;Terminate
                int     21h

Error:          mov     ah,09h                  ;Display error message
                mov     dx,offset Msg3          ;and terminate
                int     21h
                mov     ax,4C01h
                int     21h
main            endp

code            ends
                end     main