8086 Assembler Tutorial for Beginners (Part 2)

Memory Access

To access memory we can use these four registers: BX, SI, DI, BP.
Combining these registers inside [ ] symbols, we can get different memory locations. These combinations are supported (addressing modes):

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

גישה לזיכרון

לצורך  גישה לזיכרון אנחנו יכולים להשתמש בארבעת האוגרים האלה: BX, SI, DI, BP.
תוך שילוב אוגרים אלה בתוך הסמלים [ ], אנחנו יכולים להשיג מיקומים שונים של זיכרון. השילובים האלה הנתמכים  (שיטות מעון) הם:

[BX + SI]
[BX + DI]
[BP + SI]
[BP + DI]

[SI]
[DI]
d16 (variable offset only)
[BX]

[BX + SI] + d8
[BX + DI] + d8
[BP + SI] + d8
[BP + DI] + d8

[SI] + d8
[DI] + d8
[BP] + d8
[BX] + d8

[BX + SI] + d16
[BX + DI] + d16
[BP + SI] + d16
[BP + DI] + d16

[SI] + d16
[DI] + d16
[BP] + d16
[BX] + d16

d8 - stays for 8 bit displacement.

d16 - stays for 16 bit displacement.

Displacement can be a immediate value or offset of a variable, or even both. It's up to compiler to calculate a single immediate value.

Displacement can be inside or outside of [ ] symbols, compiler generates the same machine code for both ways.

Displacement is a signed value, so it can be both positive or negative.

Generally the compiler takes care about difference between d8 and d16, and generates the required machine code.


For example, let's assume that DS = 100, BX = 30, SI = 70.
The following addressing mode: [BX + SI] + 25
is calculated by processor to this physical address: 100 * 16 + 30 + 70 + 25 = 1725.

By default DS segment register is used for all modes except those with BP register, for these SS segment register is used.

There is an easy way to remember all those possible combinations using this chart:


You can form all valid combinations by taking only one item from each column or skipping the column by not taking anything from it. As you see BX and BP never go together. SI and DI also don't go together. Here is an example of a valid addressing mode: [BX+5].

The value in segment register (CS, DS, SS, ES) is called a "segment",
and the value in purpose register (BX, SI, DI, BP) is called an "offset".
When DS contains value 1234h and SI contains the value 7890h it can be also recorded as 1234:7890. The physical address will be 1234h * 10h + 7890h = 19BD0h.


In order to say the compiler about data type,these prefixes should be used:

BYTE PTR - for byte.
WORD PTR - for word (two bytes).

For example:

BYTE PTR [BX]     ; byte access.
    or
WORD PTR [BX]     ; word access.

Emu8086 supports shorter prefixes as well:

b. - for BYTE PTR
w. - for WORD PTR

sometimes compiler can calculate the data type automatically, but you may not and should not rely on that when one of the operands is an immediate value.

 d8 - מוגדר כנתון (היסט) של 8 סיביות.

 d16 - מוגדר כנתון (היסט) של 16 סיביות.

נתונים (היסט) יכולים להיות ערך מיידי או אופסט (קיזוז) של משתנה, או אפילו שניהם. על המהדר לחשב ערך מיידי ויחיד.

נתונים (היסט) יכולים להיות בין או מחוץ סמלי [ ], המהדר יוצר אותו צופן (קוד) של מכונה לשני הדרכים.
 
נתונים (היסט) זה ערך מסומן, לכן שניהם יכולים להיות חיוביים או שליליים.
 
 בדרך כלל המהדר מקפיד על הבדל בין  d8 לבין d16, ויוצר את צופן המכונה הדרוש.
 
 
 למשל, נניח ש -
                      DS=100, BX=30, SI=70

 שיטת המעון שבהמשך:
                                   25 +  [BX+SI]

  מחושב על ידי המעבד אל הכתובת הפיזית:
       
100 * 16 + 30 + 70 + 25 = 1725
 
 כברירת המחדל משתמשים באוגר סגמנט DS לכל השיטות חוץ מאלו שעובדים עם המצביע BP, לאלה משתמשים באוגר סגמנט SS.
 
 יש דרך קלה לזכור את כל השילובים האפשריים האלו אם משתמשים בטבלה הבאה:
 
 


 אתה יכול ליצור את השילובים על ידי לקיחת פריט אחד מכל עמודה או מדלגים על העמודה מבלי לקחת משהו ממנה.  כפי שאתה רואה BX ו- BP לעולם לא ילכו ביחד. SI ו- DI גם לא ילכו ביחד. להלן דוגמה לשיטת מיעון תקינה: [BX+5].
 


הערך בקטע האוגר (CS, DS, SS, ES)  נקרא "סגמנט" (גזרה), והערך באוגרי  מצביע (BX, SI, DI, BP) נקרא "אופסט".
 כאשר DS מכיל ערך 1234h ו- SI מכיל את הערך 7890h ניתן לרשום בצורה הבאה: 1234:7890. הכתובת הפיזית תהיה:
       1234h * 10h + 7890h = 19BD0h
 
 


כדי לומר למהדר לגבי סוג הנתונים, צריכים להשתמש בקידומות הבאות:
 
 BYTE PTR  - עבור בית (8 סיביות)
 WORD PTR - עבור מילה (16 סיביות)

 למשל:
 
 [
BYTE PTR [BX  ; עבור נתונים בגודל בית.
    או
[WORD PTR [BX  ; עבור נתונים בגודל מילה.
 

תוכנת ה- Emu8086  גם מאפשרת  קידומות קצרות יותר:

.b - עבור BYTE PTR
.w - עבור WORD PTR
 

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

MOV instruction
  • Copies the second operand (source) to the first operand (destination).

  • The source operand can be an immediate value, general-purpose register or memory location.

  • The destination register can be a general-purpose register, or memory location.

  • Both operands must be the same size, which can be a byte or a word.

פקודת MOV
 מקרא: Operands - משתנים -אופרנד, ערך או פונקציה שעליהם מבצעים את הפעולות המתמטיות / לוגיות.

  • מעתיקה את האופרנד השני (מקור) לאופרנד הראשון (יעד).

  • האופרנד המקור יכול להיות ערך מיידי, אוגר כללי של המעבד או מיקום של זיכרון.

  • היעד יכול להיות אוגר כללי,או מיקום של זיכרון.

  • שני האופרנדים חייבים להיות באותו הגודל, שיכול להיות בית (8 סיביות) או מילה (16 סיביות).

These types of operands are supported:
MOV REG, memory
MOV memory, REG
MOV REG, REG
MOV memory, immediate
MOV REG, immediate
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory: [BX], [BX+SI+7], variable, etc...

immediate: 5, -24, 3Fh, 10001101b, etc...
סוגי האופרנדים הנתמכים הם:

MOV REG, memory
MOV memory, REG
MOV REG, REG
MOV memory, immediate
MOV REG, immediate

REG (אוגר): AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory (זיכרון): [BX] , [BX+SI+7] ,
 variable, etc....

immediate (מיידי): 5, -24, 3Fh, 10001101b

For segment registers only these types of MOV are supported:
MOV SREG, memory
MOV memory, SREG
MOV REG, SREG
MOV SREG, REG
SREG: DS, ES, SS, and only as second operand: CS.

REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory: [BX], [BX+SI+7], variable, etc...

  לאוגרי סגמנטים רק סוגי MOV הבאים נתמכים:

MOV SREG, memory
MOV memory, SREG
MOV REG, SREG
MOV SREG, REG
 

SREG (אוגר סגמנט): DS, ES, SS ורק כאופרנד שני אוגר הסגמנט CS.
 
REG (אוגר): AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

memory (זיכרון): [BX] , [BX+SI+7] ,
 variable, etc...

The MOV instruction cannot be used to set the value of the CS and IP registers.

Here is a short program that demonstrates the use of MOV instruction:

 פקודת MOV  לא יכולה לשמש לקביעת הערך של האוגרים CS ו- IP.

להלן תוכנית קצרה שמציגה את השימוש של פקודת ה- MOV :

 #MAKE_COM# ; instruct
   compiler to make COM file.
 ORG 100h ; directive required
   for a COM program.
 MOV AX, 0B800h ; set AX to
   hexadecimal value of B800h.
 MOV DS, AX ; copy value of AX
   to DS.
 MOV CL, 'A' ; set CL to ASCII
   code of 'A', it is 41h.
 MOV CH, 01011111b ; set CH to
   binary value.
 MOV BX, 15Eh ; set BX to
   15Eh.
 MOV [BX], CX ; copy contents
  of CX to memory at B800:015E
 RET ; returns to operating
     system.
 #MAKE_COM#
 ORG 100h 
 MOV AX, 0B800h
 MOV DS, AX
 MOV CL, 'A'
 MOV CH, 01011111b
 MOV BX, 15Eh
 MOV [BX], CX
 RET
 

#MAKE_COM#;מלמד את המעבד ליצור
               .קובץ מסוג רגיל
 ORG 100h ; הנחייה הדרושה עבור
                 .תוכנית רגילה
 MOV AX, 0B800h ;   AX -קובע ל
        .B800h :ערך בסיס 16 של
 MOV DS, AX ;   AX-מעתיק ערך מ
                      .DS לתוך
 MOV CL, 'A' ;      CL -קובע ל
       .(41h) 'A' צופן אסקי של
 MOV CH, 01011111b ;CH -קובע ל
                   .ערך בינארי
 MOV BX, 15Eh ;    BX -מעתיק ל
                 .15Eh את הערך
 MOV [BX], CX;CX מעתיק את תוכן
         .B800:015E -לזיכרון ב
 RET ;      חזרה למערכת ההפעלה
     

You can copy & paste the above program to Emu8086 code editor, and press [Compile and Emulate] button (or press F5 key on your keyboard).

The Emulator window should open with this program loaded, click [Single Step] button and watch the register values.


How to do copy & paste:
  1. Select the above text using mouse, click before the text and drag it down until everything is selected.

  2. Press Ctrl + C combination to copy.

  3. Go to Emu8086 source editor and press Ctrl + V combination to paste.

As you may guess, ";" is used for comments, anything after ";" symbol is ignored by compiler.

אתה יכול להעתיק ולהדביק את התוכנית הקודמת לתוך העורך Emu8086 , להפעיל  "הידור והדמיה" על ידי לחיצת הכפתור המתאים (או הקש F5 במקלדת).

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

כיצד לבצע העתק והדבק:

  1. סמן את הטקסט שבחלון הלבן בעזרת העכבר, הקש על הטקסט (הכתוב), ומשוך אותו כלפי מטה עד שכולו יסומן.
     

  2.  לחץ על השילוב Ctrl + C בכדי להעתיק.
     

  3. לך לעורך מקור של EMU8086 ולחץ השילוב Ctrl + V על מנת להדביק.

כפי שאתה יכול לנחש, ";" משתמש להערות, המהדר מתעלם מכל דבר כתוב לאחר ";" .

Actually the above program writes directly to video memory, so you may see that MOV is a very powerful instruction.
למעשה התוכנית הקודמת כותבת ישירות לזיכרון המסך, לכן אתה יכול לראות ש- MOV היא פקודה רב עוצמה.

<<< לחלק 3 <<   >> לחלק 1 >>>