8086 Assembler Tutorial for Beginners (Part
8)
Procedures |
הדרכה בתכנות אסמבלר 8086 למתחילים (חלק
8)
פרוצדורות (שגרות) |
Procedure is a part of code that can be called
from your program in order to make some specific task. Procedures make program
more structural and easier to understand. Generally procedure returns to the
same point from where it was called. The syntax for procedure declaration: |
פרוצדורה היא חלק של תוכנית שיכולים להפעיל
מתוך התוכנית הכוללת לצורך ביצוע משימה (או פונקציה) ספציפית. הפרוצדורות
עושות את התוכנית יותר מובנה ויותר קל להבנה. בדרך כלל בסיום ביצוע
הפרוצדורה בקרת התוכנית חוזרת לנקודה המיידית לאחר זה שהזמין את תחילת
הביצוע. |
|
|
name - is the procedure name, the same name
should be in the top and the bottom, this is used to check correct closing of
procedures. Probably, you already know that RET instruction is used to return to operating system. The same instruction is used to return from procedure (actually operating system sees your program as a special procedure). PROC and ENDP are compiler directives, so they are not assembled into any real machine code. Compiler just remembers the address of procedure. CALL instruction is used to call a procedure. Here is an example: |
שם - הוא השם של הפרוצדורה, אותו שם חייב
להיות בחלק העליון ובסיום של הפרוצדורה, המהדר משתמש בזה לבדיקת גבולות
הפרוצדורה. |
ORG 100h |
Copyright 2002-2003 - Emu8086, Inc. All rights reserved. - Portions Copyright 1997-2003 Barry Allyn. All rights reserved.
Hebrew Version Copyright COVAL Computer Software - Copyright © 2003 R&D Private Advisers All rights reserved.
The above example calls procedure m1, does
MOV BX, 5, and returns to the next instruction after CALL: MOV
AX, 2. There are several ways to pass parameters to procedure, the easiest way to pass parameters is by using registers, here is another example of a procedure that receives two parameters in AL and BL registers, multiplies these parameters and returns the result in AX register: |
הדוגמה הקודמת קוראת לפרוצדורה
m1, מבצעת הפקודה: MOV Bx, 5,
וחוזר לפקודה הבא לאחר ה- Call , כלומר:
MOV AX,2 . |
|
In the above example value of AL register
is update every time the procedure is called, BL register stays
unchanged, so this algorithm calculates 2 in power of 4, so final result in AX register is 16 (or 10h). Here goes another example, |
בדוגמה הקודמת, הערך של אוגר AL
מתעדכן כל פעם שנקראת הפרוצדורה, האוגר BL
נשאר ללא שינוי, כך האלגוריתם הזה מחשב הערך 2 בחזקת
הערך 4 . בסיום הערך של AX הוא 16 (10h) כאן דוגמה נוספת, המשתמשת בפרוצדורה להדפסת מסר על המסך "Hello World" : |
ORG 100h |
b." - prefix before [SI] means that we need
to compare bytes, not words. When you need to compare words add "w."
prefix instead. When one of the compared operands is a register it's not
required because compiler knows the size of each register. |
הסימן ".b "
שרשום לפני [Si] אומר שאנחנו
צריכים להשוות סיביות (כמו לרשום הפקודה Byte Ptr). עם צריכים לעבוד עם מילים אז רושמים ".w " (כמו לרשום הפקודה Word Ptr). כאשר אחד האופרנדים הוא אוגר אין צורך לרשום הפקודות המקדימות, המהדר יודע הגודל של כל אוגר. |
Copyright 2002-2003 - Emu8086, Inc. All rights reserved. - Portions Copyright 1997-2003 Barry Allyn. All rights reserved.
Hebrew Version Copyright COVAL Computer Software - Copyright © 2003 R&D Private Advisers All rights reserved.