Requirement for Coding in Assembly Language

56
1 Requirement for Coding in Assembly Language SCC : Suthida Chaichomchuen [email protected]

description

Requirement for Coding in Assembly Language. SCC : Suthida Chaichomchuen [email protected]. 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 - PowerPoint PPT Presentation

Transcript of Requirement for Coding in Assembly Language

Page 1: Requirement for Coding in Assembly Language

1

Requirement for Coding in

Assembly Language

SCC : Suthida [email protected]

Page 2: 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

Page 3: Requirement for Coding in Assembly Language

3

Contents...

• The general organization of the program:– Initializing the program– Ending its execution

• The requirements for defining data items.

Page 4: Requirement for Coding in Assembly Language

4

Assemblers & Compilers

• Two main classes of computer language

– high-level language

•C

•BASIC

– low-level language

•Assembly

Page 5: Requirement for Coding in Assembly Language

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).

Page 6: Requirement for Coding in Assembly Language

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.

Page 7: Requirement for Coding in Assembly Language

7

• Uses an assembler program to perform the translation.

Low-Level Language...

Page 8: Requirement for Coding in Assembly 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

Page 9: Requirement for Coding in Assembly Language

9

Steps in Assembly, Link & Execute

CreateSource Code

Assembler Link Execute

Prog.ASM Prog.OBJ Prog.EXE

MASM.EXE LINK.EXE

ML.EXE

Page 10: Requirement for Coding in Assembly Language

10

ไฟล์�ที่��ได้จากกระบวนการแปล์งไฟล์�

• Object File (*.OBJ)– จะประกอบด้วยภาษาเคร��องที่��ใช้สำ าหร"บแปล์งเป#น

ไฟล์� .EXE โด้ยโปรแกรม LINK• List File (*.LST)

– จะบรรจ&ภาษาเคร��อง แล์ะโปรแกรมต้นฉบ"บ รวมที่")งอธิ+บายข้อผิ+ด้พล์าด้ที่��เก+ด้ข้/)น

• Cross-Reference File (*.CRF)– จะรวบรวมช้��อต้0าง ๆ ที่��ใช้ในโปรแกรมที่")งหมด้

Page 11: Requirement for Coding in Assembly Language

11

ไฟล์�ที่��ได้จากโปรแกรม Link

• Run File (*.EXE)– สำามารถโหล์ด้ล์งหน0วยความจ าแล์ะที่ างานได้

• Loadmap File (*.MAP)– จะบอกความสำ"มพ"นธิ�ข้องเซกเมนต้�ต้0าง ๆ ใน

โปรแกรม

Page 12: Requirement for Coding in Assembly Language

12

General format

[identifier] operation [operand(s) ] [;comment]

Statements...

Page 13: Requirement for Coding in Assembly Language

13

• ค�อช้��อที่��ใช้แที่นสำ+�งต้0าง ๆ ในโปรแกรมที่��คาด้ว0าจะอางถ/ง

• แบ0งได้เป#น2 ช้น+ด้– Name : อางถ/งต้ าแหน0งข้องข้อม4ล์ต้0างๆ– Label : อางถ/งต้ าแหน0งข้องค าสำ"�ง โพรซ�

เยอร� หร�อเซกเมนต้�

Identifiers

Page 14: Requirement for Coding in Assembly Language

14

• กฎการต้")งช้��อ– สำามารถใช้ต้"วอ"กษร A-Z, a-z, 0-9, ?, _,

$, @, (.)

– หามใช้ต้"วเล์ข้ 0-9 แล์ะ (.) ข้/)นต้นช้��อ– ต้"วอ"กษรเล์6ก/ใหญ่0ถ�อเป#นต้"วเด้�ยวก"น– สำ4งสำ&ด้ที่�� 31 ต้"วอ"กษร

Identifiers...

Page 15: Requirement for Coding in Assembly Language

15

Reserved Words

• ช้��อต้0าง ๆ ในภาษาแอสำเซมบล์�ที่��ถ4กจองไวเพ��อว"ต้ถ&ประสำงค�ข้องแต้0ล์ะต้"ว

• ประเภที่– Instructions– Directives– Operators– Predefined symbols

Page 16: Requirement for Coding in Assembly Language

16

• operation : code ต้0าง ๆ– defining data areas

– coding instructions

• operand : เต้ร�ยมข้อม4ล์ไวเพ��อกระที่ าoperation น")น ๆ

Statements...

Page 17: Requirement for Coding in Assembly Language

17

Program Comments

• ใช้เพ��อเพ+�มความกระจ0างใหก"บโปรแกรมแล์ะง0ายต้0อการปร"บปร&งแล์ะพ"ฒนา

• การใสำ0หมายเหต้&จะไม0ม�ผิล์ต้0อข้นาด้ข้องโปรแกรม

• เข้�ยนข้/)นต้นด้วยเคร��องหมาย ; (semicolon)

– เช้0น ; Calculate productivity ratio

Page 18: Requirement for Coding in Assembly Language

18

Statements

• Two types of statements:

– Instructions : สำ+�งที่��แอสำเซมเบล์อร�จะ ต้องแปล์ไปเป#น object code

– Directives : สำ+�งที่��บอกใหแอสำเซมเบล์อร�ด้ าเน+นการปฏิ+บ"ต้+ต้ามที่��ระบ&

Page 19: Requirement for Coding in Assembly Language

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.

Page 20: Requirement for Coding in Assembly Language

20

• Most common directives:

– PAGE & TITLE

– SEGMENT

– PROC

– ASSUME

– END

Directives...

Page 21: Requirement for Coding in Assembly Language

21

PAGE Directive

• ก าหนด้ไวที่��จ&ด้เร+�มต้นข้องโปรแกรม เพ��อก าหนด้จ านวนบรรที่"ด้ในแต้0ล์ะหนาแล์ะจ านวนต้"วอ"กษรในแต้0ล์ะบรรที่"ด้

Page 22: Requirement for Coding in Assembly Language

22

General format

PAGE [length] [,width]

length จ านวนบรรที่"ด้/หนาwidth จ านวนต้"วอ"กษร/บรรที่"ด้ex. PAGE 60,132

PAGE Directive...

Page 23: Requirement for Coding in Assembly Language

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.

Page 24: Requirement for Coding in Assembly Language

24

General format

text ใช้หล์"กการเด้�ยวก"บการต้")งช้��อ ก าหนด้ความยาวได้ถ/ง 60 ต้"วอ"กษร

comment ไม0ต้องใสำ0 ; น าหนาข้อความ ex. TITLE ASMSORT Assembly

program

TITLE text [comment]

TITLE Directive...

Page 25: Requirement for Coding in Assembly Language

25

SEGMENT Directive

• For defining a segment:

– SEGMENT

– ENDS

• The maximum size of a segment in real mode is 64K.

Page 26: Requirement for Coding in Assembly Language

26

General format

NAME OPERATION OPERAND COMMENT

name SEGMENT [options] ;Begin

segment

.

.

name ENDS ;End segment

SEGMENT Directive...

Page 27: Requirement for Coding in Assembly Language

27

SEGMENT Directive...

• SEGMENT statement

– defines the start of a segment

– segment name must be

•present

•unique

•follow assembler naming conventions

Page 28: Requirement for Coding in Assembly Language

28

SEGMENT Directive...

• The operand of a SEGMENT statement may contain three types of options:

– alignment

– combine

– class

Page 29: Requirement for Coding in Assembly Language

29

name SEGMENT align combine ‘class’

General format of SEGMENT statement

ex. name SEGMENT PARA STACK ‘stack’

SEGMENT Directive...

Page 30: Requirement for Coding in Assembly Language

30

SEGMENT Directive...

• Alignment type

– align : indicates the boundary on which the segment is to begin.

– PARA : segment aligns on a paragraph boundary

Page 31: Requirement for Coding in Assembly Language

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

Page 32: Requirement for Coding in Assembly Language

32

SEGMENT Directive...

• Class type

– class : used to group related segment when linking.

– Enclosed in apostrophes.

Page 33: Requirement for Coding in Assembly Language

33

SEGMENT Directive...

• ENDS statement

– indicates the end of the segment

– contains the same name as the SEGMENT statement

Page 34: Requirement for Coding in Assembly Language

34

• ใช้ในการก าหนด้ procedure ในโปรแกรม• General format

NAME OPERATION OPERANDCOMMENT

segname SEGMENT PARAprocname PROC FAR

procname ENDPsegname ENDS

PROC Directive

Page 35: Requirement for Coding in Assembly Language

35

• เพ��อก าหนด้ว"ต้ถ&ประสำงค�การใช้งานข้องแต้0ล์ะเซกเมนต้�ในโปรแกรม

• General format

OPERATION OPERAND

ASSUME SS:stackname,DS:datasegname,CS:codesegname,...

ASSUME Directive

Page 36: Requirement for Coding in Assembly Language

36

• เพ��อบอกการสำ+)นสำ&ด้ข้องโปรแกรม• วางไวที่��บรรที่"ด้สำ&ด้ที่ายข้องโปรแกรมเสำมอ• General Format

OPERATION OPERAND END [procname]

END Directive

Page 37: Requirement for Coding in Assembly Language

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

Page 38: Requirement for Coding in Assembly Language

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

Page 39: Requirement for Coding in Assembly Language

39

.MODEL memory-model

เป#นค าสำ"�งที่��ใช้ในการก าหนด้ Segment แบบรวด้เร6ว โด้ยสำามารถเล์�อกร4ปแบบข้อง

Segment ต้าม Memory Model แบบต้0าง ๆโด้ยม�ร4ปแบบค�อ

Simplified Segment Directives

Page 40: Requirement for Coding in Assembly Language

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

Page 41: Requirement for Coding in Assembly Language

41

General format

.STACK [size]

.DATA

.CODE [name]

Simplified Segment Directives

Page 42: Requirement for Coding in Assembly Language

42

ค าสำ"�งการใหน+ยามข้อม4ล์ เป#นค าสำ"�งที่��ก าหนด้เน�)อที่��ในหน0วยความจ าสำ าหร"บเก6บค0าข้อง

ต้"วแปร บางคร")งก6ม�การใหค0าเร+�มต้นก"บต้"วแปรด้วย

Data definition

Page 43: Requirement for Coding in Assembly Language

43

[name] Dn expression

General format

Data definition...

Page 44: Requirement for Coding in Assembly Language

44

• name: ช้��อข้องต้"วแปร จะม�หร�อไม0ม�ก6ได้ ถาม�จะเป#นค0าข้องออฟเซต้ข้องไบต้�แรกข้องค าสำ"�งน�)

• expression: ค�อค0าที่��ก าหนด้ใหก"บต้"วแปรอาจจะใสำ0เป#นค0าเร+�มต้นหร�อไม0ใสำ0ก6ได้

• Dn : เป#นค าสำ"�งที่��ก าหนด้เน�)อที่��ใหก"บต้"วแปรม� 3 ค าสำ"�งค�อ

Data definition...

Page 45: Requirement for Coding in Assembly Language

45

– DB/Define Byte

• ก าหนด้ 1 ไบต้�ในหน0วยความจ า– DW/Define Word

• ก าหนด้ 2 ไบต้�ในหน0วยความจ า– DD/Define Double Word

• ก าหนด้ 4 ไบต้�ในหน0วยความจ า

Data definition...

Page 46: Requirement for Coding in Assembly Language

46

– DF/Define Farword

• ก าหนด้6 ไบต้�ในหน0วยความจ า– DQ/Define Quadword

• ก าหนด้8 ไบต้�ในหน0วยความจ า– DT/Define Tenbytes

• ก าหนด้10 ไบต้�ในหน0วยความจ า

Data definition...

Page 47: Requirement for Coding in Assembly Language

47

• ex.

– DATA_BYTE DB 10, 4, 10H

– DATA_WORD DW 100, 100H, -5

– DATA_DD DD 0FFFCH

Data definition...

Page 48: Requirement for Coding in Assembly Language

48

จะม�การก าหนด้เน�)อที่��ในหน0วยความจ า แล์ะใหค0าเร+�มต้นก"บต้"วแปรด้"งน�)

0 A041064000001FBFF

DB

DW

. . .

. . .

DATA_BYTE

DATA_WORD

Page 49: Requirement for Coding in Assembly Language

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...

Page 50: Requirement for Coding in Assembly Language

50

• ก าหนด้เน�)อที่��ข้องต้"วแปรโด้ยไม0ใหค0าเร+�มต้น– COUNT DW ?

– AVERAGE DW ?

– BUFFER DB 100 DUP (?)

Data definition...

Page 51: Requirement for Coding in Assembly Language

51

• ก าหนด้น+พจน�ที่��เป#นต้"วอ"กษร– MESSAGE DB‘Enter the number’

DB ‘This number is too large’ DB ‘Please re-enter it’

Data definition...

Page 52: Requirement for Coding in Assembly Language

52

• ใหค0าออฟเซต้แอด้เด้รสำข้อง name/label

– PARA_TABLE DW PAR1DW PAR2DW PAR3

Data definition...

Page 53: Requirement for Coding in Assembly Language

53

จะที่ าใหเก+ด้การเก6บค0าออฟเซต้แอด้เด้รสำข้อง PAR1, PAR2 แล์ะ PAR3 ด้"งร4ป

ออฟเซต้ข้อง PAR1

. . .. . .

PARA_TABLE

Low-order

High-order ออฟเซต้ข้อง PAR2

ออฟเซต้ข้อง PAR3

Page 54: Requirement for Coding in Assembly Language

54

name EQU expression

ค าสำ"�งการใหน+ยามสำ"ญ่ล์"กษณ� โด้ยที่��น+พจน� อาจเป#นค0าคงที่�� สำ"ญ่ล์"กษณ� ค0าข้องเซกเมนต้� ม�

ร4ปแบบค�อ

The EQU Directive

Page 55: Requirement for Coding in Assembly Language

55

• สำ"ญ่ล์"กษณ�ที่��ถ4กก าหนด้จะไม0สำามารถเปล์��ยนค0าได้ในโปรแกรม– K EQU 1024

– A EQU AB+10

– B EQU [BP+8]

– P8 EQU DS:[BP+8]

The EQU Directive...

Page 56: Requirement for Coding in Assembly Language

56

• ค าสำ"�ง (=) เป#นการใหค0าน+พจน�เหม�อน EQU แต้0จะต้0างที่��สำามารถใหค0าน+พจน�ใหม0ก"บสำ"ญ่ล์"กษณ�ได้– CONST = 56

– CONST = CONST+1

(=) Directive...