8. MACROS, Modules, and Mouse Background Macros, Modules and the Mouse is a combination of concepts that will introduce you to modular programming while learning how to interface with the mouse. Macros allow us to use a sequence of instructions over and over without retyping them. Modules assist us in breaking the program up into many pieces that can stand alone. Objectives Be able to: Pre-Lab A. Develop Macro sequences for common programming tasks. B. Develop modules C. Pass parameters between modules D. Access the mouse. E. Include the mouse in MACROS. Demonstrate the program from Applying what you have learned section of the previous lab. Describe what occurs internally in x86 microprocessors when the call and ret instructions are executed. Be sure to include any registers or memory that may be modified. Lab A. Using MACROS MACROS prevent programmers from having to retype the same instruction over and over. The MACRO is stored in-line in the program source code file so it does not require accesses to a disk or memory. However, the instructions for the MACROS may be stored on a disk and referenced or included in multiple programs. The MACRO definition has 3 parts. name MACRO variable1, variable2, variable3......... The MACRO statement identifies the beginning and the statement identifies the end of the MACRO. The variables that are carried into the MACRO are listed after the MACRO statement. The instructions are included between MACRO and.
How would you change Example 6.2 (lab #6) to a MACRO? How would you use the MACRO to clear only a portion of the screen? Example 8.1 CLEAR_PART MACRO START, STOP ;This MACRO clears a portion of the screen ;;Uses option 06 of INT 10H MOV AX, 0600H ;scroll screen function MOV BH, 07 ;normal attribute MOV CX, START ;start at word stored in START MOV DX, STOP INT 10H ;end at word stored in STOP The values stored in the variables, START and STOP are word values for this MACRO. For example, to clear the upper left corner of the screen, START=0000H and STOP=0C27H. Thus, the MACRO will be called from the main routine as such: CLEAR_PART 0000,0C27H ;clear from 00,00 to 0CH,27H The lines in the MACRO that contain the single semicolon (;) is a comment that will appear on the in the.lst file. The comment with the double semicolon (;;) will not appear in the MASM list file. In the MASM file, the MACROS are listed after the PAGE segment but before the STACK segment. How would you change the program written in Applying what you have learned of lab 6 using MACROS? Example 8.2 demonstrate the use of data segments in a MACRO. Example 8.2 DISPLAY MACRO STRING ;This MACRO displays a string of data ;;DX= address of the string uses option 9 of INT 21H MOV AH,09 ;option 9 MOV DX, Offset STRING INT 21H ;----- STSEG SEGMENT DB 64 DUP (?) STSEG ENDS ;-----
DTSEG SEGMENT NAME1 DB My name, $ NAME2 DB is James, $ Class1 DB I am, $ Class2 DB a Jr, $ DTSEG ENDS ;----- CDSEG SEGMENT MAIN PROC FAR ASSUME CS:CDSEG;DS:DTSEG;SS:STSEG MOV AX, DTSEG MOV DS,AX.LALL CLEAR_PART 0000,184FH DISPLAY NAME1 DISPLAY NAME2.XALL DISPLAY CLASS1.SALL DISPLAY CLASS2 MOV AH,4CH INT 21H MAIN ENDP CDSEG ENDS END MAIN How can you improve the above program? Write a MACRO that will place the cursor in a position for the next string using your improved method. What is the effect of the.lall,.xall, and.sall on the list file? MACROS can also contain local variables. The variables are usually used as labels. It is very important to define any variables used inside the MACRO as LOCAL. For example, the DISPLAY MACRO may be changed to exit upon receipt of a certain character. Example 8.3 DISPLAY MACRO Charact LOCAL EXIT, BEGIN BEGIN:
EXIT: MOV AH,2 MOV DL, Charact INT 21H JNE EXIT OR AL, 0 JE BEGIN MOV BL,00 ;store and display 00 upon exit DISPLAY BL INT 21H When using the same MACRO in several programs, we may include the MACROs in a separate file. Suppose we store the MACROs in a file called macro.mac, we can use the INCLUDE directive in order to access the MACRO in our program. We would use the following syntax. CDSEG SEGMENT code... ASSUME CS:CODE INCLUDE MACRO.MAC B. Using the Mouse The mouse has become extremely popular in the last few years. In assembly language, we use INT 33H to access the mouse. INT 33H has a different nomenclature than INT 16H, INT 10H, and INT 21H. In INT 33H, AL is used to set various modes of the function and AH is set to zero. Upon return of INT 33H is AX=0, then no mouse is supported. Therefore, our first MACRO will be used to determine whether the mouse is supported. Also upon return, BX holds the number of buttons on the mouse. Example 8.4 CHECK_MOUSE MACRO MOV AX,0 INT 33H CMP AX,0 JE EXIT The AX=01 function of INT 33H enables (displays) the mouse cursor and the AX=02 function disables the mouse cursor. Example 8.5 shows a MACRO to enable the mouse. It is important to disable the mouse prior to updating graphical or video information.
Example 8.5 MOUSE_ON MACRO MOV AX, 01 INT 33H We are often required to know the position of the mouse cursor. The mouse adopts the resolution of the video; however, the cursor uses 8x8 pixels. Therefore, when a program gets a mouse cursor position, the values are provided in pixels and must be divided by eight (8) to get the mouse cursor position in terms of the character locations 00,00 to 18,4FH. INT 33H option 3 gets the current position of the mouse. Upon return from INT 33H, CX contains the horizontal position and DX contains the vertical position. Remember that the positions are in pixels. You may need a routine to convert the pixels to characters for video mode. BX contains the button status. The LSB (D0) contains the left button status, D1 contains the right button status, and D2 (3rd bit) contains the center button status. The status is 1 if the button is down and 0 if its up. Option 5 returns the button status and the position of the position of the last button pressed. When calling INT 33H, AX=5 to select option 5. BX should contain the button number being tested. BX=0 indicates the left button, BX=1 indicates the right button. If a third button is present, BX=4 indicates the middle button. On return from option 5, AX shows the status of the buttons. The LSB (D0) is 1 for left button, D1 is 1 for the right button and D3 is 1 if the middle button was pressed. BX contains the number of times that the button was pressed. DX contains the row position of the click, and CX contains the column position of the click. Again, the values in CX and DX are in pixels and must be converted to characters. Option 6 provides information concerning button release. It operates the same as option 5 except BX contains the button release count. What is the difference in option 3 and option 5? Example 8.6 is a MACRO for detecting the number of times that the left button was pressed. How may we change the MACRO to use for various buttons? Example 8.6 LEFT_BUTN MACRO MOV AX,05 MOV BX,0 INT 33H Incorporate the MACRO into a program that displays which button is pressed and how many times. Turn the mouse cursor off prior to displaying text. Don t forget to turn it back on.
How would you detect whether a red square in the center of the screen has been clicked? Write a program that answers the question above. If the red square is clicked, clear the screen, and exit the program. Otherwise, display You missed the square. on the screen. Use MACROS to 1) detect mouse click 2) display words on the screen 3) draw the square 4) clear the screen. Remember to convert from pixels to characters for the mouse position. C. Using Modules MACROS allow us to use routines by calling the name of the routine in line in the main program. There are other methods of modular programming. One other method is subroutines. Subroutines are typically larger than MACROs. The PROC directive discussed in Lab 3 is also used for subroutines. Subroutines are also declared as FAR, NEAR, PROC. To use a subroutine in the main procedure, we must define the subroutine prior to its use in the code segment. We use the EXTRN directive to notify the assembler that the routine name following EXTRN is in another module. EXTRN name:type Name can be data where type would be either byte, word, dword, fword, qword, or tbyte. Or name can be a subroutine where type would be NEAR, FAR, or PROC. Any variable that is declared as EXTRN in one module must be linked to a module that declares the variable as PUBLIC. PUBLIC name The subroutines may be in separate files or the same file. The format for a subroutine is as follows. Example 8.7 CDSEG SUBMOD SUBMOD CDSEG PUBLC SUBMOD SEGMENT PROC FAR ASSUME CS:CDSEG... RET ENDP ENDS END ;return to the calling program Modules are linked using the link command at the MS-DOS prompt or selecting LINK option in MASM. To link at the MS-DOS prompt, c:> link proc.obj + submod.obj + submod2.obj where proc.obj has the MAIN PROC, and SUBMOD and SUBMOD2 are declared as EXTRN in main. We use the CALL statement to access the subroutines for example, to call SUBMOD, we use the following statement. CALL SUBMOD When using subroutines, it is often useful to store the registers on the stack using the PUSH at the beginning of the routine and using POP at the end.
What is the difference between a MACRO and a subroutine? Write a subroutine that will convert pixel values to characters upon return from option 3 of INT 33H. Do you remember SHR? How can you use it in the subroutine above? Lab Report A. Describing What You Learned Answer all of the Think about it questions above B. Applying what you have learned Be prepared to demonstrate any of the programs written for the various "Think about it" questions above..