8086 Assembler Tutorial for Beginners (Part 10)

Macros

הדרכה בתכנות אסמבלר 8086 למתחילים (חלק 10)  

פקודות מקרו

Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled, after compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it. emu8086.inc is a good example of how macros can be used, this file contains several macros to make coding easier for you. פקודות מקרו הם כמו פרוצדורות, אבל לא באמת. מקרו נראה כמו פרוצדורות, אבל הם קיימים רק עד שהצופן הוא מהודר. לאחר ההידור כל מקרו מוחלף עם הוראות אמיתיות.
אם אתה מגדיר מקרו ולאורך התוכנית לא משתמש בו, המהדר פשוט מתעלם ממנו. הקובץ  emu8086.inc הוא דוגמה טובה של איך ניתן להשתמש במקרו, הקובץ הזה מכיל כמה קבוצות של הוראות מקרו להקלת עבודת המתכנת.

Macro definition - הגדרת מקרו

name    MACRO  [parameters,...]
                [נתונים להעברה]

             <instructions>
               < פקודות >
ENDM

Unlike procedures, macros should be defined above the code that uses it, for example: שונה מפרוצדורות, פקודות מקרו חייבות להיות מודגרות מעל כל הפקודות של התוכנית.
למשל:

MyMacro    MACRO  p1, p2, p3

     MOV AX, p1
     MOV BX, p2
     MOV CX, p3

ENDM

ORG 100h

MyMacro 1, 2, 3

MyMacro 4, 5, DX

RET

The above code is expanded into:

הצופן הקודם מפושט כ:

MOV AX, 00001h
MOV BX, 00002h
MOV CX, 00003h
MOV AX, 00004h
MOV BX, 00005h
MOV CX, DX

Some important facts about macros and procedures:
  • When you want to use a procedure you should use CALL instruction, for example:

    CALL MyProc

  • When you want to use a macro, you can just type its name. For example:

    MyMacro

  • Procedure is located at some specific address in memory, and if you use the same procedure 100 times, the CPU will transfer control to this part of the memory. The control will be returned back to the program by RET instruction. The stack is used to keep the return address. The CALL instruction takes about 3 bytes, so the size of the output executable file grows very insignificantly, no matter how many time the procedure is used.

  • Macro is expanded directly in program's code. So if you use the same macro 100 times, the compiler expands the macro 100 times, making the output executable file larger and larger, each time all instructions of a macro are inserted.

  • You should use stack or any general purpose registers to pass parameters to procedure.

  • To pass parameters to macro, you can just type them after the macro name. For example:

    MyMacro 1, 2, 3

  • To mark the end of the macro ENDM directive is enough.

  • To mark the end of the procedure, you should type the name of the procedure before the ENDP directive.

כמה עובדות חשובות על מקרו ופרוצדורות:
 

  • כאשר אתה רוצה להשתמש בפרוצדורה יש להשתמש בפקודת CALL ,למשל:
     

CALL MyProc

  • כאשר אתה רוצה להשתמש במקרו, אתה יכול פשוט לכתוב השם שלו, למשל:
     

MyMacro

  • פרוצדורה ממוקמת במקום מוגדר של הזיכרון, ואם אתה משתמש באותה פרוצדורה 100 פעמים, המעבד המרכזי יעביר את שליטת התוכנית בחלק זה של הזיכרון.  הבקרה תחזור לתוכנית הראשית על ידי פקודת ה- RET. המחסנית משמשת כמקום אחסון של כתובת החזרה. לפקודת ה- CALL דרושים 3 בתים, והגודל הכולל של התוכנית הוא גדל באופן זניח, ללא חשיבות של מספר הפעמים שהשתמש בפרוצדורה.

     

  • המקרו מפושט ישירות בצופן של התוכנית. אז אם אתה משתמש באותו מקרו 100 פעמים, המהדר מרחיב את המקרו 100 פעמים, ותוכנית הביצוע גודל יותר ויותר, כל פעם שהמהדר מכניס פקודות המקרו.

     

  • יש צורך בשימוש של המחסנית או של אוגרים של המעבד כדי להעביר פרמטרים לפרוצדורה.

     

  • כדי להעביר פרמטרים למקרו, פשוט רושמים אותם לאחר שם המקרו. למשל:

MyMacro 1, 2, 3

  • לציון סוף של מקרו מספיק בפקודת: ENDM

     

  • לציון סוף של פרוצדורה יש להקליד את שם הפרוצדורה לפני פקודת ENDP.

Macros are expanded directly in code, therefore if there are labels inside the macro definition you may get "Duplicate declaration" error when macro is used for twice or more. To avoid such problem, use LOCAL directive followed by names of variables, labels or procedure names. For example: קטע של מקרו מפושט ישירות בצופן, לכן אם יש תוויות בתוך הגדרת המקרו נקבל את הערת שגיאה: "Duplicate declaration" (הגדרה כפולה) כאשר מפעילים את המקרו פעמיים או יותר.
כדי להימנע מהבעיה הזאת, משתמשים בפקודת: LOCAL ולאחריה שמות המשתנים, תוויות או פרוצדורות.
למשל:

MyMacro2    MACRO
LOCAL label1, label2

        CMP  AX, 2
        JE label1
        CMP  AX, 3
        JE label2
label1:
        INC  AX
label2:
        ADD  AX, 2
        ENDM


ORG 100h

MyMacro2

MyMacro2

RET

If you plan to use your macros in several programs, it may be a good idea to place all macros in a separate file. Place that file in Inc folder and use INCLUDE file-name directive to use macros. See Library of common functions - emu8086.inc for an example of such file. אם אתה מתכונן להשתמש בפקודות המקרו שלך בכמה תוכניות, מומלץ למקם כל פקודות המקרו בקובץ נפרד. שים את הקובץ הזה בתיקייה INC והשתמש בפקודה:
 "שם_קובץ Include" .  ראה דוגמה של קובץ מסוג זה ב:
           Library of common functions - emu8086.inc.

<<< to Part 9 <<   >> to Part 11 >>>

<<< לחלק 11 <<   >> לחלק 9 >>>