8086 Assembler Tutorial for Beginners (Part
9)
The Stack |
הדרכה בתכנות אסמבלר 8086 למתחילים (חלק
9)
המחסנית |
Stack is an area of memory for keeping temporary data. Stack is used by CALL instruction to keep return address for procedure, RET instruction gets this value from the stack and returns to that offset. Quite the same thing happens when INT instruction calls an interrupt, it stores in stack flag register, code segment and offset. IRET instruction is used to return from interrupt call. |
מחסנית הוא אזור של הזיכרון (מקטע) לשמירת נתונים זמניים. המחסנית היא בשימוש על ידי פקודת CALL לשמירת את הכתובת החזרה מפרוצדורה, פקודת RET לוקחת הערך שנשמר במחסנית לחזרה לתוכנית הראשי למקום ממנו יצאה. אותו דבר קורה כאשר פקודת INT מבקשת ביצוע של פסיקה, במקרה זה שומרים במחסנית את אוגר הדגלים וערכי CS:IP . משתמשים בפקודת IRET לחזרה מביצוע פסיקה . |
We can also use the stack to keep any other data, there are two instructions that work with the stack: PUSH - stores 16 bit value in the stack. POP - gets 16 bit value from the stack.
|
אנחנו יכולים להשתמש במחסנית לשמירת כל נתון אחר דרך
התוכנית שלנו, יש שתי פקודות לעבודה עם המחסנית: PUSH - דוחף (מאחסן) במחסנית ערך של 16 סיביות (מילה). POP - מוציא (משיג) נתון בעל 16 סיביות (מילה). מהמחסנית. התחביר של פקודת ה- PUSH: |
|
REG: AX, BX, CX, DX, DI, SI, BP, SP. SREG: DS, ES, SS, CS. memory: [BX], [BX+SI+7], 16 bit variable, etc... immediate: 5, -24, 3Fh, 10001101b, etc... Syntax for POP instruction: |
אוגר:
AX, BX, CX, DX, DI, SI, BP, SP אוגר סגמנט: DS, ES, SS, CS זיכרון: BX], [BX+SI+7], 16 bit variable] ערך מיידי: 5, 24-, 3Fh, 10001101b התחביר של פקודת ה- POP: |
|
REG: AX, BX, CX, DX, DI, SI, BP, SP. SREG: DS, ES, SS, (except CS). memory: [BX], [BX+SI+7], 16 bit variable, etc... |
אוגר:
AX, BX, CX, DX, DI, SI, BP, SP אוגר סגמנט: DS, ES, SS, CS זיכרון: BX], [BX+SI+7], 16 bit variable] |
Notes:
|
הערות:
|
It is very important to do equal number of PUSHs
and POPs, otherwise the stack maybe corrupted and it will be impossible
to return to operating system. As you already know we use RET instruction
to return to operating system, so when program starts there is a return address
in stack (generally it's 0000h). PUSH and POP instruction are especially useful because we don't have too much registers to operate with, so here is a trick:
Here is an example: |
זה מאוד חשוב לבצע מספר שווה של פקודות
PUSH ושל
POP, אחרת מצביע
המחסנית SP משנה את ערכו
ויהיה בלתי אפשרי לחזור למערכת ההפעלה של המחשב. כפי שאתה כבר יודע אנחנו
משתמשים בפקודת RET
לחזרה למערכת ההפעלה (או לתוכנית הראשית), לכן כאשר מתחילה התוכנית היא
שומרת במחסנית כתובת חזרה למערכת ההפעלה (בדרך כלל 0000h).
כאן דוגמה: |
ORG 100h |
Another use of the stack is for exchanging the values,here is an example: | שימוש נוסף של המחסנית הוא להחלפת ערכים של אוגרים. כאן דוגמה: |
|
The exchange happens because stack uses LIFO
(Last In First Out) algorithm, so when we push 1212h and then 3434h,
on pop we will first get 3434h and only after it 1212h.
The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register. Generally operating system sets values of these registers on program start. |
מתבצעת ההחלפה כי המחסנית משתמשת באלגוריתם בשיטה ה-
LIFO, וכאשר אנו
דוחפים במחסנית את הערך 1212h ואז
3434h, בזמן ה-POP
מקבלים קודם ה- 3434h ורק אחרי זה ה-
1212h. תחום של מקטע המחסנית נקבע על ידי צמד האוגרים Stack Segment ו- Stack Pointer. בדרך כלל מערכת ההפעלה מגדירה את ערכי האוגרים האלה בהפעלת התוכנית. |
"PUSH source" instruction does the
following:
|
הפקודה " מקור PUSH" מבצעת התהליך:
הפקודה " יעד POP" מבצעת התהליך:
הכתובת הנוכחית שמצביעים SS:SP
נקרא החלק העליון של המחסנית. |
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.