Building Programs in PWB

This section shows how to build programs in the PWB environment. If you don't have a program of your own, you might want to use the following program:

; HELLO.ASM defines a string and calls the procedure PutStr to

; display the text. PutStr is in a separate module PUTSTR.ASM.

.MODEL small, c

; Tell assembler PutStr's argument type and how to call PutStr:

PutStr PROTO pMsg:PTR BYTE

.DOSSEG

.STACK

.DATA

msg BYTE "Hello, world.", 13, 10, 0 ; Null-terminated string

.CODE

.STARTUP ; Initialize data and stack segments

INVOKE PutStr, ADDR msg ; Call external procedure

.EXIT 0 ; Exit program

END

Note that there are new directives (such as .DOSSEG) and new constructs (such as INVOKE and PROTO) in this and the following procedure. MASM version 6.0 has a number of new features to make assembly-language programming more like programming in a high-level language, while maintaining the compact, fast-executing code assembly language offers. These features are explained in detail in the online reference system and in the Programmer's Guide.

Saving a Program

To save a source file, select the File menu and choose either Save or Save As. If the file is new or you choose Save As, a dialog box prompts you for a filename (see Figure 3.3). If you choose Save with an existing file, PWB automatically saves the file under the name listed in the title bar of the Source window. To save a copy of a file under a different name, choose Save As. (The older version of the file, with its original name, is not deleted or modified.)

Type in the name of the source file, HELLO.ASM. Press ENTER or click the OK button to save the file with this name.

Writing and Saving a Procedure

HELLO.ASM calls the PutStr routine to display a message (in this case, Hello, world) on the screen. You could add this routine to the HELLO.ASM file; but because it's a procedure that could be used in other programs, you should put it in a separate file.

Use the New command in the File menu to clear the screen. Type in the PutStr routine that follows and save it in a file named PUTSTR.ASM:

; PUTSTR.ASM contains a procedure PutStr, which displays a

; null-terminated string on the screen.

.MODEL small, c

.CODE ; Address of string (near or far

PutStr PROC pMsg:PTR BYTE ; depending on model) is passed on

; the stack

mov ah, 02h ; Display character function

mov di, pMsg ; Load address in DI

mov dl, [di] ; Load each character through [DI]

.WHILE (dl)

int 21h ; DOS displays character

inc di ; Point to next character

mov dl, [di] ; Load each character through [DI]

.ENDW

ret

PutStr ENDP

END

Setting the Main Language and Build Options

Before assembling the HELLO program, you must first specify the type of program you are building. Once you choose a main language and an initial build option, you can change its settings using the MASM Options command from the Options menu.

·To specify a main language and an initial build option:

1.Choose the Build Options command from the Options menu

2.Choose the Set Main Language button

3.Use the mouse or the ARROW keys to select “Assembler” from the list, and then press ENTER

4.Choose the Set Initial Build Options button

5.Use the mouse or the ARROW keys to select a build option from the list, and then press ENTER

For the HELLO program, select the DOS EXE build option.

Setting and Clearing the Program List

Once you set the main language and initial build options, you need to set the program list. The Set Program List command in the Make menu tells PWB which makefile to use in building a program. It clears the current makefile setting, instructs PWB to use the makefile you have specified, and specifies which Browser (.BSC) file is associated with the current project.

Choose the Set Program List command from the Make menu. In the dialog box that appears, type the main program's filename (HELLO), then press ENTER (see Figure 3.4). PWB automatically loads the .MAK file if it exists or asks you if you want to create it.

If the makefile does not exist, PWB asks if you want to create a new makefile; answer Yes. The Edit Program List dialog box appears, listing all files in the current directory. Select the files you want to include in the project (in this example, HELLO.ASM and PUTSTR.ASM). It doesn't matter in which order the files appear.

·Keyboard

To select the files:

1.Press TAB until the cursor is within the list box containing the directory of files (another box lists the directory names).

2.Use the ARROW keys to move to the HELLO.ASM file. Press ENTER to add the filename to the program list.

3.Repeat step 2 for PUTSTR.ASM.

4.Once both filenames appear in the program list, press TAB until the Save List command button is highlighted, and then press ENTER.

·Mouse

To select the files:

1.Click HELLO.ASM once; then click the Add/Delete button (or just double-click HELLO.ASM).

2.Repeat step 1 for PUTSTR.ASM.

3.Once both filenames appear in the program list, click the Save List command button to save the makefile.

PWB automatically creates a makefile appropriate for the files you selected. You can edit the .MAK text file to change or add additional commands.

If you want to create a new program or instruct PWB to “forget” the current program, use the Clear Program List command in the Make menu. This action clears the current program-list setting. You can then specify a new program to build using the Set Program List command. You should always clear the program list before starting work on a different project.

Assembling and Linking HELLO.ASM

The MASM Options command in the Options menu lets you control a variety of assembly options (see Figure 3.5).

From within the Macro Assembler Global Options dialog box, you can use the Set Debug Options and Set Release Options buttons to easily create debug and release versions of your program.

The Set Debug Options button displays a dialog box where you can set parameters for activities like generating list files and debugging. These settings also apply to programs built with the Debug Build option, which you select from the dialog box displayed by the Build Options command. Use debug settings when you are developing and actively debugging your program. When the Release Build option is checked in the Build Options dialog box, the parameters specified in the Set Release Options dialog box apply.

Similar debug and release options are also available for the linker with the LINK Options command in the Options menu (see Figure 3.6). You can display the options in both the Macro Assembler Global Options and LINK Options dialog boxes using Show Debug Options and Show Release Options buttons.

The Set Release Options button in the Macro Assembler Global Options dialog box offers the same choices as Set Debug Options, but Set Release applies them to a final version of the program. Use these settings when your program is completely debugged and ready for release.

Before building a program, make sure its name appears after the Compile File and Build commands in the Make menu. If the name does not appear after the Build command, choose the Set Program List command in the Make menu to set the makefile as the current project.

To build the program, choose the Build command in the Make menu. If an assembly or link error occurs, the build terminates and the error appears in the Compile Results window. You can get information about unknown errors from the online reference system by selecting the error and pressing F1. In OS/2, you are notified of errors and asked if you want to see the assembly log. (If you want to view the assembly log when running PWB under DOS, you must select the Compile Results command from the View menu before starting assembly.)

Running the Program

After the program is built, PWB adds the name of the executable file to the Execute command in the Run menu. Choose Execute to run the program. When the program is complete, press any key to return to PWB.