Download - Requirement for Coding in Assembly Language
2
Contents
• The basic requirements for developing an assembly program:– The use of comments– The general coding format– The directives for controlling the
assembled program listing– The requirements for defining
segments and procedures
3
Contents...
• The general organization of the program:– Initializing the program– Ending its execution
• The requirements for defining data items.
4
Assemblers & Compilers
• Two main classes of computer language
– high-level language
•C
•BASIC
– low-level language
•Assembly
5
High-Level Language
• Uses powerful command
– generate many machine language instructions
• More productive
• Uses a compiler program to translate the source code into machine code (object code).
6
Low-Level Language
• Uses code symbolic instructions
– generates one machine instructions
• Advantages:
– Provides more control over handling particular hardware requirements.
– May generate smaller, more compact executable modules.
– Often results in faster execution.
7
• Uses an assembler program to perform the translation.
Low-Level Language...
8
Steps in Assembly, Link & Execute
• Create an Assembler Source program (.ASM)
• Assembler the Source Program, Create an Object Program (.OBJ)
• Link the Object Program, Create an Executable Program (.EXE)
• Load and Execute the .EXE Program
9
Steps in Assembly, Link & Execute
CreateSource Code
Assembler Link Execute
Prog.ASM Prog.OBJ Prog.EXE
MASM.EXE LINK.EXE
ML.EXE
10
ไฟล์�ที่��ได้จากกระบวนการแปล์งไฟล์�
• Object File (*.OBJ)– จะประกอบด้วยภาษาเคร��องที่��ใช้สำ าหร"บแปล์งเป#น
ไฟล์� .EXE โด้ยโปรแกรม LINK• List File (*.LST)
– จะบรรจ&ภาษาเคร��อง แล์ะโปรแกรมต้นฉบ"บ รวมที่")งอธิ+บายข้อผิ+ด้พล์าด้ที่��เก+ด้ข้/)น
• Cross-Reference File (*.CRF)– จะรวบรวมช้��อต้0าง ๆ ที่��ใช้ในโปรแกรมที่")งหมด้
11
ไฟล์�ที่��ได้จากโปรแกรม Link
• Run File (*.EXE)– สำามารถโหล์ด้ล์งหน0วยความจ าแล์ะที่ างานได้
• Loadmap File (*.MAP)– จะบอกความสำ"มพ"นธิ�ข้องเซกเมนต้�ต้0าง ๆ ใน
โปรแกรม
12
General format
[identifier] operation [operand(s) ] [;comment]
Statements...
13
• ค�อช้��อที่��ใช้แที่นสำ+�งต้0าง ๆ ในโปรแกรมที่��คาด้ว0าจะอางถ/ง
• แบ0งได้เป#น2 ช้น+ด้– Name : อางถ/งต้ าแหน0งข้องข้อม4ล์ต้0างๆ– Label : อางถ/งต้ าแหน0งข้องค าสำ"�ง โพรซ�
เยอร� หร�อเซกเมนต้�
Identifiers
14
• กฎการต้")งช้��อ– สำามารถใช้ต้"วอ"กษร A-Z, a-z, 0-9, ?, _,
$, @, (.)
– หามใช้ต้"วเล์ข้ 0-9 แล์ะ (.) ข้/)นต้นช้��อ– ต้"วอ"กษรเล์6ก/ใหญ่0ถ�อเป#นต้"วเด้�ยวก"น– สำ4งสำ&ด้ที่�� 31 ต้"วอ"กษร
Identifiers...
15
Reserved Words
• ช้��อต้0าง ๆ ในภาษาแอสำเซมบล์�ที่��ถ4กจองไวเพ��อว"ต้ถ&ประสำงค�ข้องแต้0ล์ะต้"ว
• ประเภที่– Instructions– Directives– Operators– Predefined symbols
16
• operation : code ต้0าง ๆ– defining data areas
– coding instructions
• operand : เต้ร�ยมข้อม4ล์ไวเพ��อกระที่ าoperation น")น ๆ
Statements...
17
Program Comments
• ใช้เพ��อเพ+�มความกระจ0างใหก"บโปรแกรมแล์ะง0ายต้0อการปร"บปร&งแล์ะพ"ฒนา
• การใสำ0หมายเหต้&จะไม0ม�ผิล์ต้0อข้นาด้ข้องโปรแกรม
• เข้�ยนข้/)นต้นด้วยเคร��องหมาย ; (semicolon)
– เช้0น ; Calculate productivity ratio
18
Statements
• Two types of statements:
– Instructions : สำ+�งที่��แอสำเซมเบล์อร�จะ ต้องแปล์ไปเป#น object code
– Directives : สำ+�งที่��บอกใหแอสำเซมเบล์อร�ด้ าเน+นการปฏิ+บ"ต้+ต้ามที่��ระบ&
19
Directives
• Statements that enable you to control the way in which a program assembles and lists.
• Act only during the assembly of a program and generate no machine-executable code.
20
• Most common directives:
– PAGE & TITLE
– SEGMENT
– PROC
– ASSUME
– END
Directives...
21
PAGE Directive
• ก าหนด้ไวที่��จ&ด้เร+�มต้นข้องโปรแกรม เพ��อก าหนด้จ านวนบรรที่"ด้ในแต้0ล์ะหนาแล์ะจ านวนต้"วอ"กษรในแต้0ล์ะบรรที่"ด้
22
General format
PAGE [length] [,width]
length จ านวนบรรที่"ด้/หนาwidth จ านวนต้"วอ"กษร/บรรที่"ด้ex. PAGE 60,132
PAGE Directive...
23
TITLE Directive
• Use to cause a title for a program to print on line 2 of each page of the program listing.
• Code at the start of the program.
24
General format
text ใช้หล์"กการเด้�ยวก"บการต้")งช้��อ ก าหนด้ความยาวได้ถ/ง 60 ต้"วอ"กษร
comment ไม0ต้องใสำ0 ; น าหนาข้อความ ex. TITLE ASMSORT Assembly
program
TITLE text [comment]
TITLE Directive...
25
SEGMENT Directive
• For defining a segment:
– SEGMENT
– ENDS
• The maximum size of a segment in real mode is 64K.
26
General format
NAME OPERATION OPERAND COMMENT
name SEGMENT [options] ;Begin
segment
.
.
name ENDS ;End segment
SEGMENT Directive...
27
SEGMENT Directive...
• SEGMENT statement
– defines the start of a segment
– segment name must be
•present
•unique
•follow assembler naming conventions
28
SEGMENT Directive...
• The operand of a SEGMENT statement may contain three types of options:
– alignment
– combine
– class
29
name SEGMENT align combine ‘class’
General format of SEGMENT statement
ex. name SEGMENT PARA STACK ‘stack’
SEGMENT Directive...
30
SEGMENT Directive...
• Alignment type
– align : indicates the boundary on which the segment is to begin.
– PARA : segment aligns on a paragraph boundary
31
SEGMENT Directive...
• Combine type
– combine : indicates whether to combine the segment with other segments when they are linked after assembly.
– STACK, COMMON, PUBLIC, AT, NONE
32
SEGMENT Directive...
• Class type
– class : used to group related segment when linking.
– Enclosed in apostrophes.
33
SEGMENT Directive...
• ENDS statement
– indicates the end of the segment
– contains the same name as the SEGMENT statement
34
• ใช้ในการก าหนด้ procedure ในโปรแกรม• General format
NAME OPERATION OPERANDCOMMENT
segname SEGMENT PARAprocname PROC FAR
procname ENDPsegname ENDS
PROC Directive
35
• เพ��อก าหนด้ว"ต้ถ&ประสำงค�การใช้งานข้องแต้0ล์ะเซกเมนต้�ในโปรแกรม
• General format
OPERATION OPERAND
ASSUME SS:stackname,DS:datasegname,CS:codesegname,...
ASSUME Directive
36
• เพ��อบอกการสำ+)นสำ&ด้ข้องโปรแกรม• วางไวที่��บรรที่"ด้สำ&ด้ที่ายข้องโปรแกรมเสำมอ• General Format
OPERATION OPERAND END [procname]
END Directive
37
• เพ��อก าหนด้ต้ าแหน0งเร+�มต้นข้อง data segment
• General format
MOV AX,DATASG ;Get address of data segmentMOV DS,AX ;Store address in DS
Instructions for initializing a program
38
• เพ��อจบปฏิ+บ"ต้+การข้อง code segment• General Format
MOV AH,4CH ;Request end processingMOV AL,retcode ;Optional return code
21INT H ;Call interruptserviceMOV AX,4C00H ;Request normal exit
21INT H ;Call interrupt service
Instructions for ending program execution
39
.MODEL memory-model
เป#นค าสำ"�งที่��ใช้ในการก าหนด้ Segment แบบรวด้เร6ว โด้ยสำามารถเล์�อกร4ปแบบข้อง
Segment ต้าม Memory Model แบบต้0าง ๆโด้ยม�ร4ปแบบค�อ
Simplified Segment Directives
40
Model Number of Number of
Code Segment Data Segments
TINY * *
SMALL 1 1
MEDIUM >1 1
COMPACT 1 >1
LARGE >1 >1
Memory Model
41
General format
.STACK [size]
.DATA
.CODE [name]
Simplified Segment Directives
42
ค าสำ"�งการใหน+ยามข้อม4ล์ เป#นค าสำ"�งที่��ก าหนด้เน�)อที่��ในหน0วยความจ าสำ าหร"บเก6บค0าข้อง
ต้"วแปร บางคร")งก6ม�การใหค0าเร+�มต้นก"บต้"วแปรด้วย
Data definition
43
[name] Dn expression
General format
Data definition...
44
• name: ช้��อข้องต้"วแปร จะม�หร�อไม0ม�ก6ได้ ถาม�จะเป#นค0าข้องออฟเซต้ข้องไบต้�แรกข้องค าสำ"�งน�)
• expression: ค�อค0าที่��ก าหนด้ใหก"บต้"วแปรอาจจะใสำ0เป#นค0าเร+�มต้นหร�อไม0ใสำ0ก6ได้
• Dn : เป#นค าสำ"�งที่��ก าหนด้เน�)อที่��ใหก"บต้"วแปรม� 3 ค าสำ"�งค�อ
Data definition...
45
– DB/Define Byte
• ก าหนด้ 1 ไบต้�ในหน0วยความจ า– DW/Define Word
• ก าหนด้ 2 ไบต้�ในหน0วยความจ า– DD/Define Double Word
• ก าหนด้ 4 ไบต้�ในหน0วยความจ า
Data definition...
46
– DF/Define Farword
• ก าหนด้6 ไบต้�ในหน0วยความจ า– DQ/Define Quadword
• ก าหนด้8 ไบต้�ในหน0วยความจ า– DT/Define Tenbytes
• ก าหนด้10 ไบต้�ในหน0วยความจ า
Data definition...
47
• ex.
– DATA_BYTE DB 10, 4, 10H
– DATA_WORD DW 100, 100H, -5
– DATA_DD DD 0FFFCH
Data definition...
48
จะม�การก าหนด้เน�)อที่��ในหน0วยความจ า แล์ะใหค0าเร+�มต้นก"บต้"วแปรด้"งน�)
0 A041064000001FBFF
DB
DW
. . .
. . .
DATA_BYTE
DATA_WORD
49
• ก าหนด้ค0าข้อม4ล์ในล์"กษณะข้องต้าราง– B_TABLEDB 0, 0, 0, 0, 8, 13
DB -10, 0, 5, 63, 63, 63
• การก าหนด้ค0า Operand ที่��ซ )าก"น– B_TABLEDB 4 DUP (0), 8, 13,
DB -10, 0, 5, 3 DUP (63)
Data definition...
50
• ก าหนด้เน�)อที่��ข้องต้"วแปรโด้ยไม0ใหค0าเร+�มต้น– COUNT DW ?
– AVERAGE DW ?
– BUFFER DB 100 DUP (?)
Data definition...
51
• ก าหนด้น+พจน�ที่��เป#นต้"วอ"กษร– MESSAGE DB‘Enter the number’
DB ‘This number is too large’ DB ‘Please re-enter it’
Data definition...
52
• ใหค0าออฟเซต้แอด้เด้รสำข้อง name/label
– PARA_TABLE DW PAR1DW PAR2DW PAR3
Data definition...
53
จะที่ าใหเก+ด้การเก6บค0าออฟเซต้แอด้เด้รสำข้อง PAR1, PAR2 แล์ะ PAR3 ด้"งร4ป
ออฟเซต้ข้อง PAR1
. . .. . .
PARA_TABLE
Low-order
High-order ออฟเซต้ข้อง PAR2
ออฟเซต้ข้อง PAR3
54
name EQU expression
ค าสำ"�งการใหน+ยามสำ"ญ่ล์"กษณ� โด้ยที่��น+พจน� อาจเป#นค0าคงที่�� สำ"ญ่ล์"กษณ� ค0าข้องเซกเมนต้� ม�
ร4ปแบบค�อ
The EQU Directive
55
• สำ"ญ่ล์"กษณ�ที่��ถ4กก าหนด้จะไม0สำามารถเปล์��ยนค0าได้ในโปรแกรม– K EQU 1024
– A EQU AB+10
– B EQU [BP+8]
– P8 EQU DS:[BP+8]
The EQU Directive...
56
• ค าสำ"�ง (=) เป#นการใหค0าน+พจน�เหม�อน EQU แต้0จะต้0างที่��สำามารถใหค0าน+พจน�ใหม0ก"บสำ"ญ่ล์"กษณ�ได้– CONST = 56
– CONST = CONST+1
(=) Directive...