Experiment for a .COM file.
XP, DOS BOX
the CPU has CS = DS = ES = SS = PSP
Result -
0x0080h = length of the string for our command line argument, 1 byte
0x0081h = string of the input from command line, include the SPACE [0x20h] and last byte is CR [0x0d]
This is the way our program can obtains the command line argument. the standard approach would be
a) check the length of argument string
b) count the SPACE character [0x20] from this string, one [0x20] = start of one argument
c) preserve the individual argument for user's purpose
screen capture for 1 argument

screen capture for 2 arguments

Test purpose
procedure -
a) assemble the source test.asm to be test.com
b) create a file myfile.txt, can be empty
c) load the test.com, test if the message will be displayed
Reference for DOS interrupt service -
http://www.emu8086.com/assembly_language_tutorial_assembler_reference/8086_bios_and_dos_interrupts.html#int21h_09h
The Art of assembly
http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html
http://www.skynet.ie/~darkstar/assembler/tut5.html
http://eji.com/a86/
http://www.masm32.com/
http://home.educities.edu.tw/wanker742126/index.html
http://heim.ifi.uio.no/~stanisls/helppc/
DEBUG and the specific application
http://www.geocities.com/thestarman3/asm/2bytejumps.htm
Test result
DOS int 21, ah=9, the message can printed to screen when the myfile.txt is existed.
Enviroment : XP DOS BOX
Assembler : A86.com
listing of test.asm
-----------------------------
org 100h
myDATA:
jmp my_code
db 400h dup 90h
org 180h
my_code:
call OpenFile
call ReadFile
call CloseFile
mov dx, offset msg_ok
call print_msg
ret
;-----------------------------------
;INT 21h / AH= 3Dh - open existing file.
;
;Entry:
;
;AL = access and sharing modes:
;
;mov al, 0 ; read
;mov al, 1 ; write
;mov al, 2 ; read/write
;
;DS:DX -> ASCIZ filename.
;
;Return:
;
;CF clear if successful, AX = file handle.
;CF set on error AX = error code.
;
;note 1: file pointer is set to start of file.
;note 2: file must exist.
;-----------------------------------
openFile:
mov al, 2
mov dx, offset filename
mov ah, 3dh
int 21h
jc err
mov handle, ax ;save file handle
ret
;-----------------------------------
;INT 21h / AH= 3Eh - close file.
;
;Entry: BX = file handle
;
;Return:
;
;CF clear if successful, AX destroyed.
;CF set on error, AX = error code (06h)
;-----------------------------------
CloseFile:
mov ah, 3eh
mov bx, handle
int 21h
jc err
ret
err:
mov dx, offset msg_err
call print_msg
ret ;exit the program
print_msg:
;mov dx, offset msg_open
mov ah, 9
int 21h
ret
;-----------------------------------
;INT 21h / AH= 3Fh - read from file.
;Entry:
;BX = file handle.
;CX = number of bytes to read.
;DS:DX -> buffer for data.
;Return:
;CF is clear if successful - AX = number of bytes actually read;
;0 if at EOF (end of file) before call.
;CF is set on error AX = error code.
;Note: data is read beginning at current file position, and the file position is
;updated after a successful read the returned AX may be smaller than the request
;in CX if a partial read occurred.
readFile:
mov bx, handle
mov cx, 80h
mov dx, 100h ;load the file data to 0x100, override this COM file
mov ah, 3fh
int 21h
jc err
ret
;-----------------------------------
handle dw ?
msg_err db "myfile.txt, open or read or close, it was not OK $"
msg_ok db "myfile.txt, open / read / close, ok $"
filename db "myfile.txt", 0
db 256 dup 0h
-----------------------------
XP, DOS BOX
the CPU has CS = DS = ES = SS = PSP
Result -
0x0080h = length of the string for our command line argument, 1 byte
0x0081h = string of the input from command line, include the SPACE [0x20h] and last byte is CR [0x0d]
This is the way our program can obtains the command line argument. the standard approach would be
a) check the length of argument string
b) count the SPACE character [0x20] from this string, one [0x20] = start of one argument
c) preserve the individual argument for user's purpose
screen capture for 1 argument

screen capture for 2 arguments

Test purpose
procedure -
a) assemble the source test.asm to be test.com
b) create a file myfile.txt, can be empty
c) load the test.com, test if the message will be displayed
Reference for DOS interrupt service -
http://www.emu8086.com/assembly_language_tutorial_assembler_reference/8086_bios_and_dos_interrupts.html#int21h_09h
The Art of assembly
http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html
http://www.skynet.ie/~darkstar/assembler/tut5.html
http://eji.com/a86/
http://www.masm32.com/
http://home.educities.edu.tw/wanker742126/index.html
http://heim.ifi.uio.no/~stanisls/helppc/
DEBUG and the specific application
http://www.geocities.com/thestarman3/asm/2bytejumps.htm
Test result
DOS int 21, ah=9, the message can printed to screen when the myfile.txt is existed.
Enviroment : XP DOS BOX
Assembler : A86.com
listing of test.asm
-----------------------------
org 100h
myDATA:
jmp my_code
db 400h dup 90h
org 180h
my_code:
call OpenFile
call ReadFile
call CloseFile
mov dx, offset msg_ok
call print_msg
ret
;-----------------------------------
;INT 21h / AH= 3Dh - open existing file.
;
;Entry:
;
;AL = access and sharing modes:
;
;mov al, 0 ; read
;mov al, 1 ; write
;mov al, 2 ; read/write
;
;DS:DX -> ASCIZ filename.
;
;Return:
;
;CF clear if successful, AX = file handle.
;CF set on error AX = error code.
;
;note 1: file pointer is set to start of file.
;note 2: file must exist.
;-----------------------------------
openFile:
mov al, 2
mov dx, offset filename
mov ah, 3dh
int 21h
jc err
mov handle, ax ;save file handle
ret
;-----------------------------------
;INT 21h / AH= 3Eh - close file.
;
;Entry: BX = file handle
;
;Return:
;
;CF clear if successful, AX destroyed.
;CF set on error, AX = error code (06h)
;-----------------------------------
CloseFile:
mov ah, 3eh
mov bx, handle
int 21h
jc err
ret
err:
mov dx, offset msg_err
call print_msg
ret ;exit the program
print_msg:
;mov dx, offset msg_open
mov ah, 9
int 21h
ret
;-----------------------------------
;INT 21h / AH= 3Fh - read from file.
;Entry:
;BX = file handle.
;CX = number of bytes to read.
;DS:DX -> buffer for data.
;Return:
;CF is clear if successful - AX = number of bytes actually read;
;0 if at EOF (end of file) before call.
;CF is set on error AX = error code.
;Note: data is read beginning at current file position, and the file position is
;updated after a successful read the returned AX may be smaller than the request
;in CX if a partial read occurred.
readFile:
mov bx, handle
mov cx, 80h
mov dx, 100h ;load the file data to 0x100, override this COM file
mov ah, 3fh
int 21h
jc err
ret
;-----------------------------------
handle dw ?
msg_err db "myfile.txt, open or read or close, it was not OK $"
msg_ok db "myfile.txt, open / read / close, ok $"
filename db "myfile.txt", 0
db 256 dup 0h
-----------------------------
請先 登入 以發表留言。