Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  To illustrate the process involved in writing and linking an assembler
  program, we will show you how to create an incredibly simple and yet
  useful program that sounds a tone on the computer's speaker. To do this on
  any PC-family computer or any DOS computer, you write the bell character,
  ASCII 07H, to the screen. In this example, we do this by using DOS
  interrupt 21H, function 02H. Then we end the program and return program
  control to DOS using interrupt 21H, function 4CH. Follow this example and
  you'll learn quite a bit about creating self-contained assembly-language
  programs. The source code for this little program is on the following
  page.

  ; DOS generic beep program

  CodeSeg         SEGMENT byte
                  ASSUME  cs:CodeSeg

  Beep            PROC

                  mov     dl,7     ; bell character
                  mov     ah,2     ; interrupt 21H function number
                  int     21h      ; call DOS to write the character

                  mov     ax,4C00h ; AH = 4CH (interrupt 21H function number)
                                   ; AL = 00H (return code)
                  int     21h      ; call DOS to terminate the program

  Beep            ENDP

  CodeSeg         ENDS

                  END             Beep

  As you see, the program is only five instructions long, filling only 11
  bytes. If you save this program's source code in a file named BEEP.ASM,
  you can use the assembler to translate it into object code with a simple
  command:

  MASM BEEP;

  The resulting object file is ready for linking. In this case, you can link
  the program without subroutines, libraries, or other object files, like
  this:

  LINK BEEP;

  The linker program usually expects to find a stack segment in the programs
  it links, but our very simple program doesn't have one--a key
  characteristic that requires us to convert it into a .COM file, as we
  shall soon see. The linker will complain about the missing stack, but you
  can ignore its complaint.

  Linking will give you an executable program called BEEP.EXE. If you run
  BEEP.EXE, however, DOS won't know where to locate the program's stack. You
  can solve this problem by converting BEEP.EXE into a .COM program with
  EXE2BIN:

  EXE2BIN BEEP BEEP.COM

  When you run BEEP.COM, DOS automatically locates the stack for you. Now
  you have a finished beeper program that can be used on any computer that
  runs DOS. You can safely delete the intermediate files BEEP.OBJ and
  BEEP.EXE.

  Note what happens to the size of the BEEP program as it is transformed
  from an idea to an executable .COM file. The source code for this program
  is approximately 400 bytes (depending on such factors as the use of spaces
  in the comments). When you assemble and link it, you'll discover that only
  11 bytes of working machine-language instructions are created. However,
  the object file, which includes some standard linker information as
  overhead, is 71 bytes--much smaller than the source file, but much larger
  than the 11 bytes of actual machine code. After linking, the 71-byte
  object file swells to a 523-byte .EXE file. (Remember, the .EXE file
  contains a 512-byte header that contains program-loading information.)
  Converting the program to .COM format eliminates the 512 bytes of
  overhead, and you end up with a .COM file that's only 11 bytes of pure
  machine code.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson