From news.csie.nctu.edu.tw!tsaiwn Sun Nov 6 09:41:54 1994 Path: news.csie.nctu.edu.tw!tsaiwn From: tsaiwn@csie.nctu.edu.tw (Wen-Nung Tsai) Newsgroups: csie.course.asm.a,csie.course.asm.b Subject: A Simple sample ASM program Date: 14 Oct 1994 21:11:54 GMT Organization: Dep. Computer Sci. & Information Eng., Chiao Tung Univ., Taiwan, R.O.C Lines: 128 Message-ID: <37ms6q$n8j@news.csie.nctu.edu.tw> NNTP-Posting-Host: tsaiwn@ccsun2.csie.nctu.edu.tw X-Newsreader: TIN [version 1.2 PL2] Xref: news.csie.nctu.edu.tw csie.course.asm.a:44 csie.course.asm.b:164 The following is a simple sample to show you how to write a ".COM" file. (Your "LINK.EXE" might not work with "/TINY" if its version is too old.) Please try to read the listing file (In example, "simple.lst") and make sure you understand it. -------------------------------------------------------------------- c:\testdir>cat -l simple.asm 1 .model small 2 .code 3 ORG 100h 4 main proc near 5 xor ax,ax 6 push ax 7 push cs 8 pop ds 9 mov dx,offset msg 10 mov ah,9 11 int 21h 12 mov ax,4c00h 13 int 21h 14 msg db 'hello there',10,13,'$' 15 main endp 16 .stack 17 end main c:\testdir>masm simple,,,; Microsoft (R) Macro Assembler Version 5.10 Copyright (C) Microsoft Corp 1981, 1988. All rights reserved. 47138 + 375594 Bytes symbol space free 0 Warning Errors 0 Severe Errors c:\testdir>type simple.lst Microsoft (R) Macro Assembler Version 5.10 10/15/94 04:55:0 Page 1-1 1 .model small 2 .code 3 0100 ORG 100h 4 0100 main proc near 5 0100 33 C0 xor ax,ax 6 0102 50 push ax 7 0103 0E push cs 8 0104 1F pop ds 9 0105 BA 0111 R mov dx,offset msg ^____ This "R" means Relocatable, ---- I.e., the "0111" might be changed to other ---- value when "LINKING" 10 0108 B4 09 mov ah,9 11 010A CD 21 int 21h 12 010C B8 4C00 mov ax,4c00h 13 010F CD 21 int 21h 14 0111 68 65 6C 6C 6F 20 msg db 'hello there',10,13,'$' 15 74 68 65 72 65 0A 16 0D 24 17 011F main endp 18 .stack 19 end main Microsoft (R) Macro Assembler Version 5.10 10/15/94 04:55:0 Symbols-1 Segments and Groups: N a m e Length Align Combine Class DGROUP . . . . . . . . . . . . . GROUP _DATA . . . . . . . . . . . . 0000 WORD PUBLIC 'DATA' STACK . . . . . . . . . . . . 0400 PARA STACK 'STACK' _TEXT . . . . . . . . . . . . . 011F WORD PUBLIC 'CODE' Symbols: N a m e Type Value Attr MAIN . . . . . . . . . . . . . . N PROC 0100 _TEXT Length = 001F MSG . . . . . . . . . . . . . . L BYTE 0111 _TEXT @CODE . . . . . . . . . . . . . TEXT _TEXT @CODESIZE . . . . . . . . . . . TEXT 0 @CPU . . . . . . . . . . . . . . TEXT 0101h @DATASIZE . . . . . . . . . . . TEXT 0 @FILENAME . . . . . . . . . . . TEXT simple @VERSION . . . . . . . . . . . . TEXT 510 17 Source Lines 17 Total Lines 19 Symbols 47138 + 375594 Bytes symbol space free 0 Warning Errors 0 Severe Errors c:\testdir>link/tiny simple; Microsoft (R) Segmented Executable Linker Version 5.31.009 Jul 13 1992 Copyright (C) Microsoft Corp 1984-1992. All rights reserved. LINK : warning L4045: name of output file is 'simple.com' c:\testdir>dir simple Volume in drive C is MS-DOS_6 Serial number is 1CD1:B701 Directory of c:\testdir\simple.* simple.asm 223 10-15-94 4:51a simple.com 31 10-15-94 4:56a simple.crf 231 10-15-94 4:55a simple.lst 1899 10-15-94 4:55a simple.obj 160 10-15-94 4:55a 2,544 bytes in 5 file(s) 40,960 bytes allocated 43,196,416 bytes free c:\testdir>simple hello there c:\testdir> -- -------------------------------------------- Wen-Nung Tsai 蔡文能 INTERNET: tsaiwn@csunix.csie.nctu.edu.tw Dep. of CSIE BITNET: tsaiwn@twnctu01.bitnet National Chiao Tung University HsinChu, Taiwan, R.O.C. From news.csie.nctu.edu.tw!tsaiwn Sun Nov 6 09:42:08 1994 Path: news.csie.nctu.edu.tw!tsaiwn From: tsaiwn@csie.nctu.edu.tw (Wen-Nung Tsai) Newsgroups: csie.course.asm.a,csie.course.asm.b Subject: Re: A Simple sample ASM program Date: 15 Oct 1994 16:07:54 GMT Organization: Dep. Computer Sci. & Information Eng., Chiao Tung Univ., Taiwan, R.O.C Lines: 20 Message-ID: <37ouoq$5p2@news.csie.nctu.edu.tw> References: <37ms6q$n8j@news.csie.nctu.edu.tw> <37onek$4m6@news.csie.nctu.edu.tw> NNTP-Posting-Host: tsaiwn@operator-gw.csie.nctu.edu.tw X-Newsreader: TIN [version 1.2 PL2] Xref: news.csie.nctu.edu.tw csie.course.asm.a:47 csie.course.asm.b:167 Huang-wen Chen (hwchen@csie.nctu.edu.tw) 提到: : Sorry ... 請問一下 , 程式開始時使用 : : : 5 xor ax,ax : : 6 push ax : 而結束時用 : : : 12 mov ax,4c00h : : 13 int 21h : 而不是用 RET 來結束程式 , 那麼 5 6 行的程式還有存在的必要嗎 ? : Thanks ... 可拿掉。 Even you use "RET"來結束程式, Line 5,6 都可拿掉(for .COM file)。 請看第三章最後一節! -- -------------------------------------------- Wen-Nung Tsai 蔡文能 INTERNET: tsaiwn@csunix.csie.nctu.edu.tw Dep. of CSIE BITNET: tsaiwn@twnctu01.bitnet National Chiao Tung University HsinChu, Taiwan, R.O.C. From news.csie.nctu.edu.tw!tsaiwn Sun Nov 6 09:42:18 1994 Path: news.csie.nctu.edu.tw!tsaiwn From: tsaiwn@csie.nctu.edu.tw (Wen-Nung Tsai) Newsgroups: csie.course.asm.a,csie.course.asm.b Subject: Re: A Simple sample ASM program Date: 24 Oct 1994 19:07:54 GMT Organization: Dep. Computer Sci. & Information Eng., Chiao Tung Univ., Taiwan, R.O.C Lines: 64 Message-ID: <38h0ma$deu@news.csie.nctu.edu.tw> References: <37ms6q$n8j@news.csie.nctu.edu.tw> <38dqrb$j2m@news.csie.nctu.edu.tw> NNTP-Posting-Host: tsaiwn@operator-gw.csie.nctu.edu.tw X-Newsreader: TIN [version 1.2 PL2] Xref: news.csie.nctu.edu.tw csie.course.asm.a:52 csie.course.asm.b:169 Ruven Wang (rvwang@csie.nctu.edu.tw) 提到: : 老師您好......請問一下...... : : 4 main proc near : 如果改成 main proc FAR 會不會更好? In this example, 不會更好! FAR or Near here only affects the "RET" instruction. FAR will cause "RET" to be treated as "RETF" while Near will cause "RET" to be translated into "RETN". The RETF requires 4 bytes return address in STACK. We don't care here since we use "INT 21h with ah=4ch" to "return". : : 5 xor ax,ax : : 6 push ax : : 7 push cs : : 8 pop ds : 7∼8 在 .COM 好像可以省去? Already discussed this in previous article. They can be removed in this example, either for .COM or for .EXE。 The reason is that we use "INT 21h with ah=4ch" to "return". See Chapter 3 for detail. : : Segments and Groups: : : N a m e Length Align Combine Class : : DGROUP . . . . . . . . . . . . . GROUP : : _DATA . . . . . . . . . . . . 0000 WORD PUBLIC 'DATA' : : STACK . . . . . . . . . . . . 0400 PARA STACK 'STACK' : : _TEXT . . . . . . . . . . . . . 011F WORD PUBLIC 'CODE' : 請問 Length 那欄代表什麼意義。 The length in byte of that segment. In this example, you can see the ".STACK" has a default stack length of 1024 (400h) bytes. And the length of "_TEXT" (default name for ".code") is 11fh bytes. (Note that we use "ORG 100h" though the actual code length is only 1fh) : : Symbols: : : N a m e Type Value Attr : : MAIN . . . . . . . . . . . . . . N PROC 0100 _TEXT Length = 001F : : MSG . . . . . . . . . . . . . . L BYTE 0111 _TEXT : ^^請問 L 的意義。 L==Label. : : @CODE . . . . . . . . . . . . . TEXT _TEXT : : @CODESIZE . . . . . . . . . . . TEXT 0 : : @CPU . . . . . . . . . . . . . . TEXT 0101h : : @DATASIZE . . . . . . . . . . . TEXT 0 : : @FILENAME . . . . . . . . . . . TEXT simple : : @VERSION . . . . . . . . . . . . TEXT 510 : 請問這些是什麼? 這些是一些default 的NAME .CODE 等於寫 _TEXT segment public 'CODE' 而 @CODE 代表 .CODE 那segment的名稱 _TEXT @DATA 代表 .DATA 那segment的名稱 _DATA @FILENAME 會被換成該file之名, 此例為 "simple".asm @VERSION 會被換成該MASM之version no., 此例為 510 (version 5.10) : Thank you! You're welcome. -- -------------------------------------------- Wen-Nung Tsai 蔡文能 INTERNET: tsaiwn@csunix.csie.nctu.edu.tw Dep. of CSIE BITNET: tsaiwn@twnctu01.bitnet National Chiao Tung University HsinChu, Taiwan, R.O.C.