8086 Assembler Tutorial for Beginners (Part 1)


This tutorial is intended for those who are not familiar with assembler at all, or have a very distant idea about it. Of course if you have knowledge of some other programming language (Basic, C/C++, Pascal...) that may help you a lot.
But even if you are familiar with assembler, it is still a good idea to look through this document in order to study Emu8086 syntax.

It is assumed that you have some knowledge about number representation (HEX/BIN), if not it is highly recommended to study Numbering Systems Tutorial before you proceed.


What is an assembly language?


Assembly language is a low level programming language. You need to get some knowledge about computer structure in order to understand anything. The simple computer model as I see it:

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


הדרכה זו מיועדת לאותם מתכנתים שלא מכירים כלל את שפת אסמבלר, או בעלי ידע בסיסי בלבד. כמובן שאם אתה יודע לכתוב בשפת תכנות כלשהיא (...Basic, C/C++, Pascal) יש לך יתרון גדול.
 אבל אפילו אם אתה מכיר אסמבלר, עדיין זה רעיון טוב לדפדף מסמך זה לצורך לימוד של התחביר של מערכת התכנות של EMU8086 .

יש להניח שאתה בקיא בתחום של הצגת מספרים בבסיס 2 ו- 16 (HEX/BIN), במידה ולא, מומלץ ללמוד תחום זה מ- הדרכה של שיטות ספירה בטרם תמשיך.



מהי שפת אסמבלי ?

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

המחשב הבסיסי כפי שאני רואה אותו הוא:

The system bus (shown in yellow) connects the various components of a computer.
The CPU is the heart of the computer, most of computations occur inside the CPU.
RAM is a place to where the programs are loaded in order to be executed.

Inside the CPU

ערוץ המערכת (בצבע צהוב) מחבר בין הרכיבים השונים של המחשב.
המעבד המרכזי (CPU) הינו המרכז של המחשב, רוב העיבודים מתרחשים בתוך המעבד.
זיכרון ה-RAM הוא המקום בו עולים התוכניות לצורך הרצה (הפעלה).



בתוך המעבד (CPU)

GENERAL PURPOSE REGISTERS

8086 CPU has 8 general purpose registers, each register has its own name:

  • AX - the accumulator register (divided into AH / AL).

  • BX - the base address register (divided into BH / BL).

  • CX - the count register (divided into CH / CL).

  • DX - the data register (divided into DH / DL).

  • SI - source index register.

  • DI - destination index register.

  • BP - base pointer.

  • SP - stack pointer.




Despite the name of a register, it's the programmer who determines the usage for each general purpose register. The main purpose of a register is to keep a number (variable). The size of the above registers is 16 bit, it's something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form.

4 general purpose registers (AX, BX, CX, DX) are made of two separate 8 bit registers, for example if AX= 0011000000111001b, then AH=00110000b and AL=00111001b. Therefore, when you modify any of the 8 bit registers 16 bit register is also updated, and vice-versa. The same is for other 3 registers, "H" is for high and "L" is for low part.

Because registers are located inside the CPU, they are much faster than memory. Accessing a memory location requires the use of a system bus, so it takes much longer. Accessing data in a register usually takes no time. Therefore, you should try to keep variables in the registers. Register sets are very small and most registers have special purposes which limit their use as variables, but they are still an excellent place to store temporary data of calculations.

SEGMENT REGISTERS

  • CS - points at the segment containing the current program.

  • DS - generally points at segment where variables are defined.

  • ES - extra segment register, it's up to a coder to define its usage.

  • SS - points at the segment containing the stack.

Although it is possible to store any data in the segment registers, this is never a good idea. The segment registers have a very special purpose - pointing at accessible blocks of memory.

Segment registers work together with general purpose register to access any memory value. For example if we would like to access memory at the physical address 12345h (hexadecimal), we should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much more memory than with a single register that is limited to 16 bit values.
CPU makes a calculation of physical address by multiplying the segment register by 10h and adding general purpose register to it (1230h * 10h + 45h = 12345h):

The address formed with 2 registers is called an effective address.
By default BX, SI and DI registers work with DS segment register;
BP and SP work with SS segment register.
Other general purpose registers cannot form an effective address!
Also, although BX can form an effective address, BH and BL cannot!

SPECIAL PURPOSE REGISTERS

  • IP - the instruction pointer.

  • Flags Register - determines the current state of the processor.


IP register always works together with CS segment register and it points to currently executing instruction.
Flags Register is modified automatically by CPU after mathematical operations, this allows to determine the type of the result, and to determine conditions to transfer control to other parts of the program.
Generally you cannot access these registers directly.


>>> to Part 2 >>>

אוגרים לשימוש כללי

במעבד 8086 יש 8 אוגרים לשימוש כללי (ומספר שימושים יחודיים), כאשר לכל אחד שם משלו:

  • AX - אוגר צובר, מחולק ל- AL ו- AH.

  • BX - אוגר בסיס, מחולק ל- BL ו- BH.

  • CX - אוגר מונה, מחולק ל- CL ו- CH.

  • DX - אוגר נתונים, מחולק ל- DL ו- DH.

  • SI - אוגר מצביע מקור.

  • DI - אוגר מצביע יעד.

  • BP - אוגר מצביע בסיס.

  • SP - אוגר מצביע מחסנית.

למרות השם של כל אוגר, המתכנת הוא זה שקובע את מטרת השימוש של כל אחד מהאוגרים הכלליים. המטרה העיקרי של אוגר הוא לשמור ערך מספרי כל שהוא (משתנה). גודל האוגרים הנ"ל הינו 16 סיביות כל אחד, זה משהוא דומה ל:
 0011000000111001b (בצורה בינארית) או בצורה דצימלית הוא: 12345.

4 אוגרים הינם לשימוש כללי (AX, BX, CX, DX), כל אחד מהם מורכב משני אוגרים נפרדים של 8 סיביות. למשל, אם  AX= 0011000000111001b, אז : AH=00110000b and AL=00111001b .
 כלומר, כאשר אנו משנים את ערכו של כל אחד מהאוגרים של 8 סיביות, גם האוגר של 16 סיביות  משנה את ערכו, ולהפך. כנ"ל לגבי 3 האוגרים האחרים, "H" זה עבור החלק הגבוה ו-"L" עבור החלק הנמוך.

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



אוגרי סגמנטים (אוגרי בסיס)

  • CS - מצביע על תחילת אזור הזיכרון המכיל את התוכנית הנוכחית.
  • DS - מצביע על תחילת אזור הזיכרון המכיל את הנתונים והמשתנים של התוכנית הנוכחית.
  • ES - מצביע על תחילת אזור מיוחד של הזיכרון המכיל נתונים מיוחדים של התוכנית הנוכחית.
  • SS - מצביע על תחילת אזור הזיכרון המכיל את נתוני המחסנית של התוכנית הנוכחית.

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

אוגרי הסגמנט פועלים יחד עם האוגרים המצביעים לצורך גישה לכל מקום של הזיכרון. למשל, אם אנו רוצים נתון של הכתובת הממשית (כתובת פיזית) 12345h (בסיס 16), יש צורך להגדיר DS = 1230h  ו- SI = 0045h . שיטה זו יעילה שכן בדרך זו ניתן לגשת ליותר כתובות של זיכרון לעומת אוגר מצביע המוגבל לכתובות של 16 סיביות בלבד.

המעבד מבצע חישוב של הכתובת הממשית על ידי ביצוע כפל בין אוגר סגמנט לבין  10h ומוסיף אליו את האוגר המצביע:
(1230h * 10h + 45h = 12345h)

הכתובת המורכבת דרך 2 האוגרים נקראת כתובת ממשית (effective address).
כברירת מחדל האוגרים  DI,   SI, BX  עובדים עם אוגר סגמנט DS  register;
האוגרים SP ו-BP עובדים עם SS .
אוגרים כלליים אחרים לא יכולים להרכיב כתובת ממשית לעבודת המתכנת!
כמו כן, למרות שאוגר BX יכול להרכיב כתובת ממשית, אין אפשרות שהאוגרים BL או BH יעשו זאת, כיוון שהם מורכבים מ-8 סיביות כל אחד!

אוגרים לשימוש מיוחד

  • IP - מצביע פקודות.
  • FR - אוגר דגלים, מראה את המצב הנוכחי של המעבד (ALU) לאחר פעולה מתמטית.

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

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

בדרך כלל אין לנו גישה לאוגרים אלה באופן ישיר.
 
>>> לחלק 2 >>>