1 / 7

CS344-321 Assembly Language Programming

CS344-321 Assembly Language Programming. Period 10. ตัวอย่าง การเขียนโปรแกรมแบบแฟ้ม .EXE display equ 9h ;display output function doscall equ 21h ;DOS interrupt number data_seg segment hello_message db 'Hello, world!', 0dh, 0ah, '$' data_seg ends code_seg segment

robbin
Download Presentation

CS344-321 Assembly Language Programming

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. CS344-321 Assembly Language Programming Period 10

  2. ตัวอย่าง การเขียนโปรแกรมแบบแฟ้ม .EXE display equ 9h ;display output function doscall equ 21h ;DOS interrupt number data_seg segment hello_message db 'Hello, world!', 0dh, 0ah, '$' data_seg ends code_seg segment assume cs:code_seg, ds:data_seg start: mov ax,data_seg mov ds,ax mov ah,display mov dx,offset hello_message int doscall mov ax,4c00h int doscall code_seg ends end start

  3. Listing = 0009 display equ 9h ;display output function = 0021 doscall equ 21h ;DOS interrupt number 0000 data_seg segment 0000 48 65 6C 6C 6F 2C hello_message db 'Hello, world!', 0dh, 0ah, '$' 20 77 6F 72 6C 64 21 0D 0A 24 0010 data_seg ends 0000 code_seg segment assume cs:code_seg, ds:data_seg 0000 start: 0000 B8 ---- R mov ax,data_seg 0003 8E D8 mov ds,ax 0005 B4 09 mov ah,display 0007 BA 0000 R mov dx,offset hello_message 000A CD 21 int doscall 000C B8 4C00 mov ax,4c00h 000F CD 21 int doscall 0011 code_seg ends end start

  4. คำสั่ง assume cs:code_seg, ds:data_segบอก assembler ว่า จะใช้ ds เป็นจุดเริ่มต้นของ data_seg แต่หลังจาก DOS บรรจุโปรแกรมแบบ .EXE แล้ว จะกำหนดให้ ds ชี้ไปที่จุดเริ่มต้นของ PSP ท่านต้องปรับให้ ds ชี้มาที่ data_seg ด้วยคำสั่ง สองคำสั่งต่อไปนี้ mov ax,data_seg mov ds,ax ท่านไม่สามารถใช้คำสั่งเดียว คือ mov ds,data_seg เพราะเป็นข้อห้ามข้อคำสั่ง mov (ดู chapter 4 หน้า 4) หมายเหตุdata_seg จะถูกปรับค่าให้ถูกต้องเมื่อมีการบรรจุโปรแกรมนี้ในหน่วยความจำ เพราะรู้ว่าได้บรรจุ data_seg ที่ใด

  5. หมายเหตุ 1. Directive EQU 2. Operator OFFSET ซึ่งมีความหมายอธิบายได้ด้วยตัวอย่าง ต่อไปนี้ mov ax,OFFSET data กับ mov ax,data สมมุติว่าใน Listing บอกว่า data อยู่ที่ ออฟเซต 0020h และข้อมูลที่ออฟเซตดังกล่าว คือ 25h 0020 data dw 25h ถ้าทำคำสั่ง mov ax,offset data ค่าใน ax จะเท่ากับ 0020h แต่ถ้าทำคำสั่ง mov ax,data ค่าใน ax จะเท่ากับ 0025h สรุป ให้มองว่า data เป็นตัวแปรตัวหนึ่งที่เก็บค่า 25h ถ้าต้องการค่าของตัวแปร data ให้อ้าง ถึงชื่อตัวแปรนั้น คือ data แต่ถ้าต้องการอ้างถึงที่อยู่ในที่นี้คือ ค่าออฟเซตของตัวแปร data ให้ใส่ operator OFFSET ข้างหน้าคือ OFFSET data เป็นต้น

  6. แฟ้มแบบ .exe มี header ตรงต้นแฟ้ม เพื่อเก็บข้อมูลบางอย่าง เช่น MINALLOC, MAXALLOC, MAGIC #, Relocation Table, และ CS:IP ที่จะเริ่มต้น execute (ตรง directive END) ใน Listing มี อักษร R ใช้เพื่อบอกให้ตัวเชื่อมโยง ปรับออฟเซตให้ถูกต้อง เมื่อมีการเชื่อมโยงระหว่างหลายๆ segment ที่เขียนแยกจากกัน แล้วนำมารวมเป็น segment เดียวกัน ผู้เขียนโปรแกรมจึงไม่จำเป็นต้องสนใจอักษร ดังกล่าว อักษรดังกล่าว จะไม่กินเนื้อที่ในหน่วยความจำ ตรง 0000 B8 ---- R mov ax,data_seg เนื่องจากช่วงการแปล ยังไม่รู้ว่า data_seg จะถูกบรรจุที่ตำแหน่งใดในหน่วยความจำ assembler จึงใส่ เครื่องหมาย --- และมีอัษร R เพื่อบอกให้รู้ว่า หลังการบรรจุโปรแกรมในหน่วยความจำ แล้ว ต้องมีการปรับค่าดังกล่าวให้ถูกต้อง ตำแหน่งดังกล่าว จะเก็บไว้ใน Relocation Table ตรง header ของแฟ้ม .exe

  7. ดังนั้น ถ้าดูจาก Listing แล้ว ท่านจะสามารถตรวจสอบได้ว่า โปรแกรมดังกล่าว เป็นโปรแกรม แบบ .COM หรือไม่ โดยพิจารณา ดังนี้ 1. ตรง directive END เริ่มที่ CS:0100h หรือไม่ ถ้าไม่ แสดงว่าไม่ใช่โปรแกรมแบบ .COM 2. มี เครื่องหมาย ---- R ที่กล่าวมาข้างต้นหรือไม่ ถ้ามี แสดงว่าไม่ใช่โปรแกรมแบบ .COM 3. มี stack segment เช่น my_stack segment STACK my_stack ends เป็นต้น ถ้ามี แสดงว่าไม่ใช่โปรแกรมแบบ .COM ถ้า ไม่ใช่โปรแกรมแบบ .COM ท่านไม่สามารถใช้ exe2bin เปลี่ยนจากแฟ้ม .EXE เป็น .COM หมายเหตุ อย่าลืมว่า โปรแกรมแบบ .COM ต้องโตไม่เกิน 64K - 2 - 256

More Related