html5-img
1 / 56

Requirement for Coding in Assembly Language

Requirement for Coding in Assembly Language. SCC : Suthida Chaichomchuen std@kmitnb.ac.th. 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

vince
Download Presentation

Requirement for Coding in Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Requirement for Coding in Assembly Language SCC : Suthida Chaichomchuen std@kmitnb.ac.th

  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. Low-Level Language... • Uses an assembler program to perform the translation.

  8. Steps in Assembly, Link & Execute • Create an AssemblerSource 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. Prog.ASM Prog.OBJ Prog.EXE Create Source Code Assembler Link Execute MASM.EXE LINK.EXE ML.EXE Steps in Assembly, Link & Execute

  10. ไฟล์ที่ได้จากกระบวนการแปลงไฟล์ไฟล์ที่ได้จากกระบวนการแปลงไฟล์ • Object File (*.OBJ) • จะประกอบด้วยภาษาเครื่องที่ใช้สำหรับแปลงเป็นไฟล์ .EXE โดยโปรแกรม LINK • List File (*.LST) • จะบรรจุภาษาเครื่อง และโปรแกรมต้นฉบับ รวมทั้งอธิบายข้อผิดพลาดที่เกิดขึ้น • Cross-Reference File (*.CRF) • จะรวบรวมชื่อต่าง ๆ ที่ใช้ในโปรแกรมทั้งหมด

  11. ไฟล์ที่ได้จากโปรแกรม Link • Run File (*.EXE) • สามารถโหลดลงหน่วยความจำและทำงานได้ • Loadmap File (*.MAP) • จะบอกความสัมพันธ์ของเซกเมนต์ต่าง ๆ ในโปรแกรม

  12. [identifier] operation [operand(s)] [;comment] Statements... General format

  13. Identifiers • คือชื่อที่ใช้แทนสิ่งต่าง ๆ ในโปรแกรมที่คาดว่าจะอ้างถึง • แบ่งได้เป็น 2 ชนิด • Name : อ้างถึงตำแหน่งของข้อมูลต่างๆ • Label : อ้างถึงตำแหน่งของคำสั่ง โพรซีเยอร์ หรือเซกเมนต์

  14. Identifiers... • กฎการตั้งชื่อ • สามารถใช้ตัวอักษร A-Z, a-z, 0-9, ?, _, $, @, (.) • ห้ามใช้ตัวเลข 0-9 และ (.) ขึ้นต้นชื่อ • ตัวอักษรเล็ก/ใหญ่ถือเป็นตัวเดียวกัน • สูงสุดที่ 31 ตัวอักษร

  15. Reserved Words • ชื่อต่าง ๆ ในภาษาแอสเซมบลีที่ถูกจองไว้เพื่อวัตถุประสงค์ของแต่ละตัว • ประเภท • Instructions • Directives • Operators • Predefined symbols

  16. Statements... • operation : code ต่าง ๆ • defining data areas • coding instructions • operand : เตรียมข้อมูลไว้เพื่อกระทำ operation นั้น ๆ

  17. Program Comments • ใช้เพื่อเพิ่มความกระจ่างให้กับโปรแกรมและง่ายต่อการปรับปรุงและพัฒนา • การใส่หมายเหตุจะไม่มีผลต่อขนาดของโปรแกรม • เขียนขึ้นต้นด้วยเครื่องหมาย ; (semicolon) • เช่น ; 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. Directives... • Most common directives: • PAGE & TITLE • SEGMENT • PROC • ASSUME • END

  21. PAGE Directive • กำหนดไว้ที่จุดเริ่มต้นของโปรแกรม เพื่อกำหนดจำนวนบรรทัดในแต่ละหน้าและจำนวนตัวอักษรในแต่ละบรรทัด

  22. PAGE [length] [,width] PAGE Directive... General format length จำนวนบรรทัด/หน้า width จำนวนตัวอักษร/บรรทัด ex. PAGE 60,132

  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. TITLE text [comment] TITLE Directive... General format text ใช้หลักการเดียวกับการตั้งชื่อ กำหนดความยาวได้ถึง 60 ตัวอักษร comment ไม่ต้องใส่ ; นำหน้าข้อความ ex. TITLE ASMSORT Assembly program

  25. SEGMENT Directive • For defining a segment: • SEGMENT • ENDS • The maximum size of a segment in real mode is 64K.

  26. NAME OPERATION OPERAND COMMENT name SEGMENT [options] ;Begin segment . . name ENDS ;End segment SEGMENT Directive... General format

  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’ SEGMENT Directive... General format of SEGMENT statement ex. name SEGMENT PARA STACK ‘stack’

  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. NAME OPERATION OPERAND COMMENT segname SEGMENT PARA procname PROC FAR procname ENDP segname ENDS PROC Directive • ใช้ในการกำหนด procedure ในโปรแกรม • General format

  35. OPERATION OPERAND ASSUME SS:stackname,DS:datasegname,CS:codesegname,... ASSUME Directive • เพื่อกำหนดวัตถุประสงค์การใช้งานของแต่ละเซกเมนต์ในโปรแกรม • General format

  36. OPERATION OPERAND END [procname] END Directive • เพื่อบอกการสิ้นสุดของโปรแกรม • วางไว้ที่บรรทัดสุดท้ายของโปรแกรมเสมอ • General Format

  37. Instructions for initializing a program • เพื่อกำหนดตำแหน่งเริ่มต้นของ data segment • General format MOV AX,DATASG ;Get address of data segment MOV DS,AX ;Store address in DS

  38. Instructions for ending program execution • เพื่อจบปฏิบัติการของ code segment • General Format MOV AH,4CH ;Request end processing MOV AL,retcode ;Optional return code INT 21H ;Call interrupt service MOV AX,4C00H ;Request normal exit INT 21H ;Call interrupt service

  39. Simplified Segment Directives เป็นคำสั่งที่ใช้ในการกำหนด Segment แบบรวดเร็ว โดยสามารถเลือกรูปแบบของ Segment ตาม Memory Model แบบต่าง ๆ โดยมีรูปแบบคือ .MODEL memory-model

  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. Simplified Segment Directives General format .STACK [size] .DATA .CODE [name]

  42. Data definition คำสั่งการให้นิยามข้อมูล เป็นคำสั่งที่กำหนดเนื้อที่ในหน่วยความจำสำหรับเก็บค่าของตัวแปร บางครั้งก็มีการให้ค่าเริ่มต้นกับตัวแปรด้วย

  43. Data definition... General format [name] Dn expression

  44. Data definition... • name: ชื่อของตัวแปร จะมีหรือไม่มีก็ได้ ถ้ามีจะเป็นค่าของออฟเซตของไบต์แรกของคำสั่งนี้ • expression: คือค่าที่กำหนดให้กับตัวแปรอาจจะใส่เป็นค่าเริ่มต้นหรือไม่ใส่ก็ได้ • Dn : เป็นคำสั่งที่กำหนดเนื้อที่ให้กับตัวแปรมี 3 คำสั่งคือ

  45. Data definition... • DB/Define Byte • กำหนด 1 ไบต์ในหน่วยความจำ • DW/Define Word • กำหนด 2 ไบต์ในหน่วยความจำ • DD/Define Double Word • กำหนด 4 ไบต์ในหน่วยความจำ

  46. Data definition... • DF/Define Farword • กำหนด 6 ไบต์ในหน่วยความจำ • DQ/Define Quadword • กำหนด 8 ไบต์ในหน่วยความจำ • DT/Define Tenbytes • กำหนด 10 ไบต์ในหน่วยความจำ

  47. Data definition... • ex. • DATA_BYTE DB 10, 4, 10H • DATA_WORD DW 100, 100H, -5 • DATA_DD DD 0FFFCH

  48. . . . DATA_BYTE 0A 04 DB 10 DATA_WORD 64 00 00 DW 01 FB FF . . . จะมีการกำหนดเนื้อที่ในหน่วยความจำ และให้ค่าเริ่มต้นกับตัวแปรดังนี้

  49. Data definition... • กำหนดค่าข้อมูลในลักษณะของตาราง • B_TABLE DB 0, 0, 0, 0, 8, 13 DB -10, 0, 5, 63, 63, 63 • การกำหนดค่า Operand ที่ซ้ำกัน • B_TABLE DB 4 DUP (0), 8, 13, DB -10, 0, 5, 3 DUP (63)

  50. Data definition... • กำหนดเนื้อที่ของตัวแปรโดยไม่ให้ค่าเริ่มต้น • COUNT DW ? • AVERAGE DW ? • BUFFER DB 100 DUP (?)

More Related