I have been following a tutorial series and I have come to the part where we write to files. Here is a part of the code that writes to files, as it has been shown in the tutorial:
mov ah, 3d
mov al, 1
mov dx, 150
int 21
mov si, ax
mov ah, 40
mov bx, si
mov cx, 0d
mov dx, 175
int 21
int 20
The author does explain everything step by step, it's just that he does not explain every line in an equally understandable way. I understand the inital part, that ah, 3d & al, 1 opens the file and sets up the stage for writing while dx, 150 targets the specific file, but I am not sure about the following
For example, why do we move ax to si, why can not the value remain in ax? Why do we further proceed to move si to bx - why can not the value remain in si? Thank you for any clarification... I am aware that this is a noobie question but it would help me a lot to have this clarified. Thanks.
The calling convention for interrupt 21h "system calls" has the arguments and return codes in specific registers. E.g. the selector of which type of call to make is in register ah
. For the Open File call (3dh), al
is the sharing mode and ds:dx
is the pointer to the filename. It returns its result in ax
. But ax
overlaps ah
and al
and in order to do the write call, 40h must be put in ah
as that is the selector for the Write call. The file handle returned in ax
must be preserved, which involves either writing it to memory or moving it to a register which is guaranteed to be preserved across the call.
You can see documentation on the int 21h
calling conventions e.g. here, or in Ralph Brown's interrupt list. See also other x86 docs in the x86 tag wiki.