Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Assembly Language Step by Step Programming with DOS and Linux 2nd Ed 2000.pdf
Скачиваний:
156
Добавлен:
17.08.2013
Размер:
4.44 Mб
Скачать

NASM-IDE's Editor in Detail

As NASM-IDE's beating heart, the text editor deserves a little space all to itself. People who have read earlier editions of this book (there were two, in fact, though the first one didn't live very long and really doesn't count) may notice a remarkable resemblance between NASM-IDE's text window and my own JED editor, which is what I described for the two earlier editions of this book. The reason is pretty simple: Both JED and NASM-IDE were based on the Borland Binary Editor, a text editor module that could be linked into Turbo Pascal and Borland Pascal programs.

NASM-IDE uses a newer version of the editor than the one I incorporated into JED (which I originally wrote in 1989 with Turbo Pascal 5.0), and has the further advantage of using the Turbo Vision application framework, which provides all the window controls, the buttons, the scrollbars, and so on. But the two are remarkably alike in many ways, especially from the standpoint of the keystrokes that control their text editors.

Loading Files into the Editor

When you invoke NASM-IDE and it begins running, it loads either the file you named on the command line when you invoked it (as I described and showed you earlier) or else the last file it worked on, as recorded in a configuration file called NASMIDE.INI. (You can inspect this file, which is maintained by NASM-IDE, but don't alter it yourself!)

Individual lines within an edit file are limited to 254 characters. Loading a file with longer lines will cause the editor to insert hyphens at the 254-character point. You'll get an error box if you try to type past the 254-character point on any line.

Although it's not something you'll do a lot, it's useful to know that you can load more than one text file into NASM-IDE's environment at once. This can be handy when you want to cut or copy text out of one file and paste it into another. Each file remains in a separate edit window.

Opening a file in an edit window from inside NASM-IDE is done using either the menus or hotkeys. Selecting the File|Open menu item with the mouse will bring up a dialog box that will allow you to specify the file you want to open. The same dialog box can be invoked with the F3 hot key.

File|Open or F3 brings up the Open File dialog box.

The dialog is one of the more complex ones you'll encounter in using NASM-IDE, and I've shown it in Figure 5.6. Like all parts of NASM-IDE, the dialog box allows you to work either with the mouse or purely from the keyboard. Most people have a mouse, so I'll emphasize mouse-enabled techniques in my explanations.

Figure 5.6: The Open File dialog box.

From a height, what the dialog presents you with is a list of files (marked Files) from which you can choose (these are shown in the rectangular area in the center) as well as an edit line where the currently selected file's name is displayed. (That's the line at the top, marked Name.) If you click once on a file listed in the Files box, its name will appear in the Name box. You can move the highlight bar around the list of files with the arrow keys if you prefer to work from the keyboard.

By default, the only files that will appear in the dialog box are those with a .ASM file extension.

Directories will appear as well, indicated by a backslash character after the directory name. You can display files with file extensions other than .ASM, but you will have to place the cursor after the .ASM extension in the Name edit box, and backspace over ".ASM." Then type whatever extension you wish to display and press Enter.

Choosing a file to open is simple: You double-click on a file name in the Files list box, or else press Enter when the file you want to open is highlighted. The file will be opened and placed in an edit window.

You also have the option of typing the name of a file in the Name line at the top of the dialog. If another file name is already there, you will have to backspace over it before you type the new name in.

You can navigate through a DOS directory structure using the dialog box. Directories are shown with a backslash character after them to let you know that they are directories. Double-clicking on a directory name will take you into that directory and display any files present there.

A special symbol, "..\" indicates the parent directory. If you double-click on ..\, you will move up one level in the directory hierarchy. This is the same way you move to the parent directory using the DOS CD command, so it should come as no surprise.

A two-line blue bar at the bottom of the dialog box displays detailed information about the highlighted file, including its size, its timestamp (that is, the last time and date that it was modified), and the directory path where it resides.

Moving the Cursor

If you have a mouse, use it. Putting the cursor where you want it is easy: You click on a point on the screen and the cursor is simply there. Moving up and down through the file is done by grabbing the edit window's scroll bar thumb and dragging it. Most people who have any experience at all with graphical user interface (GUI) environments such as Windows will take to this without any trouble.

Many people who do a lot of text editing prefer to do their in-file cursor navigation strictly from the keyboard, to avoid having to take their hands away to grab the mouse. It's up to you. The mouse is there if you want it, but mouse or no mouse, there is a whole elaborate system for moving the cursor around without ever taking your hands away from the keyboard.

Apart from the obvious keypad keys, all editor commands are hot keys based on the Ctrl key. That is, you must hold the Ctrl key down while pressing another key or two keys. All of the hot keys that control cursor movement are grouped together for you in a cluster toward the left side of the keyboard:

This arrangement of cursor command keys will be familiar to anyone who has worked with the WordStar word processor, which in ancient times was almost everybody-and today is almost nobody except those of us who were around in ancient times.

One Character at a Time

Moving the cursor one character at a time can be done in all four directions:

Ctrl-E or Up Arrow moves the cursor up one character.

Ctrl-X or Down Arrow moves the cursor down one character.

Ctrl-S or Left Arrow moves the cursor left one character.

Ctrl-D or Right Arrow moves the cursor right one character.

The position of these four keys (E, X, S, and D) provides a hint as to which way they move the cursor. Look at how they are arranged on the keyboard:

Until the directions become automatic to your fingers (as they will, if you do enough editing!), thinking of the "magic diamond" will remind you which way the cursor will move for which keypress.

When you move the cursor to the bottom of the screen and press Ctrl-X one more time, the screen will scroll. All the lines on the screen will jump up by one, and the top line will disappear. As long as the cursor is on the bottom line of the screen and you continue to press Ctrl-X, the screen will scroll upward. If you use Ctrl-E to move the cursor back in the opposite direction (upward) until it hits the top of the screen, further Ctrl-Es will scroll the screen downward one line per Ctrl-E.

One Word at a Time

NASM-IDE will also move the cursor one word at a time to the left or right:

Ctrl-A or Ctrl-Left Arrow moves the cursor left one word.

Ctrl-F or Ctrl-Right Arrow moves the cursor right one word.

More hints are given here, since the A key is on the left side of the magic diamond, and the F key is on the right side of the magic diamond.

One Screen at a Time

It is also possible to move the cursor upward or downward through the file one whole screen at a time.

Upward in this sense means toward the beginning of the file; downward means toward the end of the file. A screen is the height of your CRT display (25, 43, or 50 lines, depending on how you've configured the NASM-IDE environment) minus four lines for the menu and prompt bars and the edit window borders.

Ctrl-R or PgUp moves the cursor up one screen.

Ctrl-C or PgDn moves the cursor down one screen.

Moving the Cursor by Scrolling the Screen

I have described how the screen will scroll when you use the one- character-at-a-time commands to move upward (Ctrl-E) from the top line of the screen or downward (Ctrl-X) from the bottom line of the screen. You can scroll the screen upward or downward no matter where the cursor happens to be by using the scrolling commands:

Ctrl-W scrolls the screen down one line.

Ctrl-Z scrolls the screen up one line.

When you scroll the screen with these commands, the cursor rides with the screen as it scrolls upward or downward, until the cursor hits the top or bottom of the screen. Then further scrolling will make the screen slip past the cursor. The cursor will never vanish and will always be visible.

These are all of the cursor movement commands that may be invoked by one Ctrl keystroke. There are a few more that are accomplished by holding the Ctrl key down and pressing two keys in succession.

You must hold the Ctrl key down through both keypresses!

Moving to the Ends of the Line

No matter where your cursor is on the screen, it is always within a line, even if that line happens to be empty of characters. There are two commands that will move the cursor either to the beginning (left end) of the line (screen column 1) or to the end of the line, which is the position following the last visible character on the line:

Ctrl-Q/S or Home sends the cursor to the beginning of the line.

Ctrl-Q/D or End sends the cursor to the end of the line.

Moving to the Ends of the File

The last set of cursor movement commands I describe takes the cursor to the beginning of the file or to the end of the file. If the file you are editing is more than a few screens long, it can save you a great deal of pounding on the keyboard to move one screen at a time.

Ctrl-Q/R or Ctrl-PgUp sends the cursor to the beginning of the file.

Ctrl-Q/C or Ctrl-PgDn sends the cursor to the end of the file.

Because all of the current file is in memory all of the time, moving between the ends of the file can be done very quickly.

Insert Mode and Cursor Coordinates

At the bottom edge of every edit window, toward the left side, are a pair of numbers indicating where the cursor is within the file. While you were moving the cursor around, the line and column numbers were continually changing to reflect the cursor's coordinates.

The number on the left is the line number, counting from the beginning of the file. The number on the right is the column number, reflecting the position of the cursor within its line. The line number always indicates the line in the file that contains the cursor-again, counting from the beginning of the file, not from the top of the screen.

There's another matter that you may or may not have noticed, and that is the state of the Insert toggle. A toggle is a condition that may exist in one of two different states. A toggle is like a switch controlling the lights in a room; the switch may be either on or off. The way you control the Insert toggle is with the Ins key: Tap Ins and you flip the state of the Insert toggle. Tap it again and you flip the state again.

The way you can monitor its state is by the shape of the text cursor in the edit window. The default is for Insert to be on. When Insert is on, the text cursor is a small flashing line. When Insert is off, the text cursor is a flashing block instead.

But what does it do? The Insert toggle determines how newly typed characters are added to your work file. When Insert is on (that is, if the cursor is a flashing line), characters that you type are inserted into the file. The characters appear over the cursor and immediately push the cursor and the rest of the line to the right to make room for themselves. The line becomes one character longer for each character that you type. If you press Enter, the cursor moves down one line, carrying with it the part of the line lying to its right.

When Insert is off (that is, if the cursor is a flashing block), characters that you type will overwrite characters that already exist in the file. Assuming you're typing in the middle of the file, somewhere within a line, you will overwrite one character for every character that you type. Only if you overwrite

existing characters all the way to the end of the line or the end of the file and keep typing will the total number of characters in the file actually increase. If you press Enter, the cursor will move down to the first character of the next line down, but nothing else will change. A line will only be added to the file if you press Enter with the cursor on the last line of the file.

Turning Insert on and off is done with a single control keypress:

Ctrl-V or Ins toggles Insert on and off.

The Indent Toggle

Indent is another toggle. It indicates whether NASM-IDE's auto-indent feature is on or off. When Indent is on, the cursor will automatically move beneath the first visible character on a new line when you press Enter. In other words (assuming that Indent is on), given this little bit of text on your screen:

Adjust:

MOV AX, [BP] + 6

SUB AX, Increment_ <--Before pressing Enter

_

^

| After pressing Enter

Before pressing Enter, the cursor is at the end of the last line of text, immediately after the word "Increment." When you press Enter, the cursor will move down one line, but it will also space over to the right automatically until it is beneath the S in "SUB." This allows you to begin typing the next line of code without having to space the cursor over so that it is beneath the start of the previous line.

Like Insert, Indent can be toggled on and off. It takes a double control keystroke to do it:

Ctrl-Q/I toggles Indent on and off.

Indent is considered on when the letter I appears in the bottom line of the edit window frame, to the left of the cursor coordinates, almost in the corner. If the I is there, you will get an automatic indent to the start of the previous line when you press Enter to insert a new line.

Deleting Text

There are also a number of different ways to delete text in NASM-IDE. The simplest is to use the Del (Delete) key. Pressing Ctrl-G performs exactly the same delete function:

Ctrl-G or Del deletes one character to the right of the cursor.

The cursor does not move. It swallows the character to its right, and the rest of the line to its right moves over to fill in the position left by the deleted character. The Backspace key can be used to delete characters to the left of the cursor; another way it differs from Del is that the cursor rides to the left on each deletion:

Backspace deletes one character to the left of the cursor.

You can think of backspace as eating one character to the left as it moves the cursor leftward. You can also (to save a few keystrokes) delete one word to the right of the cursor:

Ctrl-T deletes one word to the right of the cursor.

When you press Ctrl-T, all characters from the cursor position rightward to the end of the current word will be deleted. If the cursor happens to be on a space or group of spaces between words, that space or spaces will be deleted up to the beginning of the next word. It's possible to delete from the cursor position to the end of the cursor line:

Ctrl-Q/Y deletes from the cursor to the end of the line.

And finally, it's possible to delete the entire cursor line with a single control keystroke:

Ctrl-Y deletes the entire line containing the cursor.

The line beneath the cursor moves up to take the place of the deleted line, pulling up the rest of the file behind it.

A warning here for those of you with thick fingers: The T and Y characters are right next to one another on the keyboard. In a late-night frenzy at the keyboard you may find yourself reaching for Ctrl-T to delete a word and hit Ctrl-Y instead, losing the entire line irretrievably. I've done this often enough that I simply broke myself of the habit of using Ctrl-T at all.

Undoing Changes to a Line

NASM-IDE's editor keeps a backup copy of each line while you're working on it, and retains that copy as long as the cursor remains within the line. Therefore, if you delete a word or some other portion of the line, or add something to a line by mistake, you can undo those changes to the line as long as you haven't yet left the line. Once you leave the line even momentarily, the editor throws away the backup copy, and Undo is no longer possible.

Ctrl-Q/L restores a line to its condition before you entered it.

One drawback is that the undo feature will not restore a line deleted partially with Ctrl-Q/Y or entirely with the Ctrl-Y command. Once a line is deleted, the cursor (by necessity) leaves the line, and so the editor does not retain the backup copy of the line. Be careful how you use Ctrl-Y!

Marking Text Blocks

For several kinds of operations, it's useful to be able to mark a text block. You might want to copy the text to the clipboard, cut the text to the clipboard, or delete the block of text entirely. A logical pointer embedded invisibly in your text called a marker is used to specify the beginning and end of a text block. There are only two of these markers, and in consequence only one block may be marked within a given file at any given time. These block markers are named B and K, after the commands that position them in your file. (You can try and remember them this way: B and K are on opposite ends of the word block and the markers mark opposite ends of a block of text.)

The block markers are invisible and do not appear on your screen in any way. If both are present in a file, however, all the text between them (that is, the currently marked block) is shown as highlighted text.

As with most things in NASM-IDE, there are two ways to mark a block of text with the block markers. The easy way is with the mouse. Simply click the mouse at the point in the file where you want the marked block of text to begin, and hold the left button down. With the left button held down, drag the cursor to the other end of the block. The text between the two ends of the block will become highlighted as you drag the mouse. When the end of the block is where you want it to be, just let go of the left mouse button. The block will remain highlighted until you move the cursor again.

Placing the block markers can be done from the keyboard with a pair of two-character control keystrokes:

Ctrl-K/B places the B marker.

Ctrl-K/K places the K marker.

Again, when you mark a block from the keyboard, the block will remain marked only until you move the cursor somehow. So, once you mark a block, do whatever you intend to do with the block immediately. As soon as you move the cursor by even one character, the block vanishes and is no longer marked.

The Clipboard and Block Commands

The simplest block command to understand is Delete Block. Getting rid of big chunks of text that are no longer needed is easy: Mark the text as a block, and then issue the Delete Block command:

Ctrl-K/Y or Ctrl-Del will delete a block of text.

For some reason, this command is called Clear in NASM-IDE's Edit menu. Delete or clear, it does the same thing: It wipes away the highlighted block of text.

The rest of the block commands mostly concern the clipboard. The clipboard is a special text buffer built into NASM-IDE, into which you can copy blocks of text, and from which you can insert text into different places in the same text file, or into entirely different text files. People who use Windows a lot will be familiar with the clipboard, though there was no direct equivalent under DOS.

Copy is useful when you have some standard text construction (a standard boilerplate comment header for procedures, perhaps) that you need to use several times within the same text file. Rather than type it from the keyboard each time, you type it once, mark it as a block, and then copy it to the clipboard by pressing Ctrl-Ins.

Ctrl-Ins will copy the highlighted block of text to the clipboard.

Once it's in the clipboard, you move the cursor position to where you need it. Simply put the cursor where the first character of the copied text must go, and then issue the Paste command:

Shift-Ins will paste the clipboard contents to the cursor position.

Moving a block of text is similar to copying a block of text. The difference, of course, is that the marked block of text vanishes from its original position and reappears at the cursor position. It must pass through the clipboard on its way from one place to another. As with copying a block, the process of moving text requires two operations: First you cut the block from its original position into the clipboard, and then you paste the block from the clipboard to the cursor position. Cutting the block is done by highlighting a block of text and then pressing Shift-Del:

Shift-Del will cut the highlighted block of text to the clipboard.

Once the block of text is in the clipboard, you can use the Shift-Ins hot key to paste the text from the clipboard to the cursor position, just as you did to copy a block to multiple places in the file or in different files.

All of the clipboard-related commands may also be given from the Edit menu. Once you mark a block by highlighting it, you can pull down the Edit menu and select the Cut, Copy, or Clear items to act on the marked block. Once you have some text in the clipboard, you can pull down the Edit menu and select Paste to paste the clipboard text into the current file at the cursor position.

One final clipboard command allows you to actually look at what text is currently in the clipboard. This command is available only from the Edit menu. When you select Edit|Show clipboard, a text window will open that shows exactly what text is in the clipboard. You can edit the text if you like, and your changes will be reflected in the text that you paste into your other file or files.

When you're finished looking at the clipboard text in its window, click on the close control (the small square on the left end of the top edge of the window) and it will close. The clipboard text, however, remains on the clipboard.

Searching for Text

Much of the power of electronic text editing lies in its ability to search for a particular character pattern in a text file. Furthermore, once found, it's a logical extension of the search concept to replace the found text string with a different text string. For example, if you decide to change the name of a variable to avoid conflict with another identifier in a program, you might wish to have the text editor locate every instance of the old variable name in a program and replace each one with the new variable name.

NASM-IDE's editor can do both search and search/replace operations with great ease-and such easy searching makes page numbering unnecessary. If you wish to work on the part of a program that contains a particular procedure, all you need do is search for that procedure's name and you will move the cursor right to the spot you want:

Ctrl-Q/F will find a given text string.

The Find command can also be issued from the menu bar by selecting the Search|Find menu item.

When you issue the Find command (either from the keyboard or from the menu; it doesn't matter), NASM-IDE brings up a dialog box to find out what you want to find and how you want to configure the search. This dialog is shown in Figure 5.7.

Figure 5.7: The Find dialog box.

The text that you're searching for goes in the Text to find edit field in the dialog box. You then tab down to the two options fields. These are check boxes, and they both default to blank. The first specifies a case-sensitive search. If you check it, the text found must match the text you enter in Text to find both in characters and in case.

The second check box specifies that the search must be for Whole words only. This allows you to avoid finding strings inside of larger strings. For example: Suppose you're looking for the register name "AX." In the middle of your assembly file, you have a comment that reads like this:

; Axe the dead space at the end of the string

If you don't check Whole words only, your search for "AX" will also find "Axe" because "ax" is inside "axe." You could also discriminate between the two by case, but that's less reliable, especially when you're searching source code files written by other people who may have entirely different habits in using character case. (Or who aren't consistent in how they use it-that's even worse.)

If Whole words only is checked, a search for "AX" will not find "Axe," or "Tax," or "maximum," or anything else that isn't simply..."AX."

Once you have the Find dialog box filled in the way you want it, press the OK button. The search will commence, and if the search is successful the cursor will move to the first character of the first instance of the found text string. If the editor cannot find any instance of the requested text string in the work file, it displays an error message box containing the message "Search string not found." You must then click on the OK button to continue editing.

Searching for Additional Instances

Most assembly language source code files will have multiple instances of common identifiers like "AX." If you want to find not the first one but the third or fourth (or one further down that you know you can identify from its context), you need to search for additional instances of the text. NASM-IDE has a command for this as well: Search again. This command may be issued from the menu bar, by selecting menu item Search|Search again. The shortcut for Search again is Ctrl-L, which is very handy. (Sometimes using the mouse is more hand motion than it needs to be.)

Ctrl-L will find (or replace) the next instance of a given text string.

Replacing Found Text

Many times, the whole reason for finding a text string is to replace it with something else-sometimes not simply once but for every instance of the text string in the file. NASM-IDE has special machinery to do exactly this: the Replace command. Once the search text is found, NASM-IDE will replace the search text with replacement text that you provide.

As with Find, you can issue the Replace command either from the menu bar by selecting the Search|Replace menu item, or from the keyboard, by pressing Ctrl-QA.

Ctrl-QA will find and replace a given text string with your text.

As with Find, the Replace command brings up a dialog box, (shown in Figure 5.8) into which you must enter:

Figure 5.8: The Replace dialog box.

1.The text you're searching for.

2.The text you want to replace the found text with.

3.The kind of search you want to conduct (that is, whole words only or case-sensitive or both).

4.Whether to prompt for each replacement.

5.Whether to swoop through the file and replace all instances of the original text with your replacement text.

The search options are the same as those I described for the Find command. If you leave the Replace dialog box in its default state and click OK, the editor will locate the first instance of the search string, highlight it, and display another dialog box that asks the question: "Replace this instance?" You can click on the Yes button to replace the instance, the No button to leave it alone, or the Cancel button to end the Replace operation entirely. (Cancel is most useful when you're doing a Replace all operation and running through the whole file replacing every instance of some text string.)

Note that just as with the Find command, you can use the Ctrl-L hot key to perform a subsequent Replace command on the next instance of the text string you just replaced. Each Ctrl-L will locate and replace the next instance of the search text until no more instances of the search text are found in the file.

Saving Your Work

It's very important to keep in mind what is happening while you edit text files with the editor. You are editing entirely within memory. Nothing goes out to disk while you are actually doing the edit. You can work on a file for hours, and one power failure or dog tripping over your PC's power cord will throw it all away. You must develop the discipline of saving your work every so often.

You can save your work by going to the menu bar, pulling down the File menu, and selecting the Save item. But the easiest way to execute a Save command from within the editor is with the Save shortcut, F2.

F2 saves your work file.

Press F2 early and often. Everything depends on it.