![]() ![]() ภาษาแอสเซมบลี้ และ การแปล |
|
ปรับปรุง : 2566-01-31 (ปรับรุ่นเป็น 9.0) |
|
| Digital logic | OS | คำสั่งดอส | Batch | Debug | Assembly | GWBasic | Docker | |
|
ตัวอย่างการดู register ด้วยโปรแกรม debug
C:\MASM611\BIN>debug -r AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 DS=119B ES=119B SS=119B CS=119B IP=0100 NV UP EI PL NZ NA PO NC 119B:0100 C8 DB C8 -q C:\MASM611\BIN> |
|
วิธีติดตั้งตัวแปลภาษา Assembly
1. ตัวแปลภาษา : masm611.zip 4.7 MB และ dosbox074.zip 1.4 MB 2. คลาย zip ลงในเครื่อง 3. สั่ง run โปรแกรม setup.exe 4. กด Enter ประมาณ 20 ครั้ง - เมื่อ enter ที่ 12 ควรเปลี่ยน binr เป็น bin 5. ถ้าพบ Setup Successfully กด Ctrl-C ได้ 6. โปรแกรมทั้งหมดอยู่ในห้อง c:\masm611 7. พบตัวแปลภาษาใน c:\masm611\bin\ 8. ถ้าขณะ install ถ้าไม่เปลี่ยน bin เป็น binr - ต้อง cd c:\masm611\binr - ต้อง copy *.* c:\masm611\bin - จึงจะแปลโปรแกรมได้สำเร็จ ตามตัวอย่าง |
|
|
วิธีแปล (Compile) ลิงค์(Link) และสั่งประมวลผล(Execute)
การสั่งแปล source code ให้ได้ .exe ด้วย c:\masm611\bin>a sample1 สร้าง a.bat ด้วย notepad masm %1.asm link %1,,,,, %1 ประโยชน์ของโปรแกรม a.bat คือ ช่วยแปล ลิงค์ และประมวลผล ในคำสั่งเดียว
|
|
1. แสดงการสร้างโปรแกรมใช้งาน interrupt
:: สร้างโปรแกรม 2 byte .com เป็นบริการของ bios เพื่อแสดง Divide overflow ถ้าเป็น Windows XP จะแสดงข้อความเท่าที่เห็น แต่ถ้าเป็น 98 จะแสดงดังตัวอย่างด้านล่าง |
C:\>debug test.com -a 11BD:0100 int 0h 11BD:0102 -rcx CX 0001 :2 -w Writing 00002 bytes -q C:\>test.com Your program caused a divide overflow error. If the problem persists, contact your program vendor. C:\> |
|
2. แสดงการประมวลผลใน debug อย่างง่าย
:: ใช้คำสั่ง mov shr และ int 21 สำหรับ int 20 เป็นการหยุดการทำงาน |
C:\>debug -a 100 0AE8:0100 mov ah,02 0AE8:0102 mov dl,51 0AE8:0104 int 21 0AE8:0106 shr dl,1 0AE8:0108 shl dl,1 0AE8:010A int 21 0AE8:010C int 20 0AE8:010E -g QP Program terminated normally - |
|
3. แสดงใช้ directive segment ends แบบเก่า
:: พิมพ์ตัวอักษร a จนกระทั่งกดปุ่ม ctrl-break จึงหยุดการทำงาน |
; a1.asm compile by MASM611
cseg segment
assume cs:cseg,ds:cseg ; Code segment, Data segment
push cs
pop ds
jmp start ; กระโดดไป start label
msg1 db 'a',0dh,0ah,'$'
start: mov ah,09h ; พิมพ์ string ทางจอภาพ
lea dx,msg1
int 21h
mov ah,01h ; รับค่าจากแป้นพิมพ์
int 21h
jmp start
cseg ends ; ปิด segment
end ; เลิกการทำงาน
|
|
4. แสดงการทำซ้ำด้วย label, cmp, je
:: โปรแกรมนี้ใช้ directive .model .data .code .stack มิได้ใช้ segment ends แบบเก่า :: Loop รับ 1 อักษร จากบริการ int21 สำหรับที่ ah,06 และ dl,ff แต่พิมพ์ซ้ำตลอดกาล จนรับ e หรือ E จึงหยุด โปรแกรมนี้ชื่อ x.asm ถูกแปลด้วย MASM611 ซึ่งเป็นแนวการเขียนโปรแกรมแบบ .exe โปรแกรมที่ได้มีขนาด 578 Byte |
; Get char and repeat print char until e or E
.model small
.data
x1 db "type e or E to Exit",0ah,0dh,'$'
.code
pmain proc far ; can not change far to near
push ds ; 1 of 5 line required for end .exe
mov ax,0 ; 2 clear ax by xor ax,ax
push ax ; 3 send ax to stack
mov ax,@data
mov ds,ax
start: mov ah,09h ; พิมพ์ string ทางจอภาพ
lea dx,x1
int 21h
mov ah,06h ; รับ char จากแป้นพิมพ์ส่งเข้า al
top: mov dl,0ffh ; ถ้า dl เป็น ff จะรับค่าจาก console แบบไม่หยุดรอ
int 21h
jz typeo ; ถ้าไม่กดอะไร จะไป typeo เพราะ ah ค้างที่ 6 อยู่
mov cl,al ; เก็บตัวอักษรใน al ไว้กลัวหาย
cmp al,'E'
je finish
cmp al,'e'
je finish
typeo: mov dl,cl ; ถ้า dl ไม่เป็น ff จะส่งค่าใน al ไป console
int 21h
jmp top
finish: ret ; ไม่สามารถใช้ int20 เพื่อสั่งให้เลิกงาน
pmain endp
.stack 200h ; not required
end pmain
.model small
.data
msg1 db 'Hello ','$'
msg2 db 'World','$'
.code
pmain proc far
push ds
mov ax,0
push ax
mov ax,@data
mov ds,ax
start: mov ah,09h
lea dx,msg1
int 21h
lea dx,msg2
int 21h
finish: ret
pmain endp
end pmain
|
|
5. แสดงการเขียน macro
:: แสดงเทคนิคการใช้ macro ในภาษา assembly |
setproc macro
push ds ; 1 of 5 line required for end .exe
mov ax,0 ; 2 clear ax by xor ax,ax
push ax ; 3 send ax to stack
mov ax,@data
mov ds,ax
endm
prtout macro
mov ah,09h
lea dx,msg
int 21h
endm
; =============
; Main program
; =============
.model small
.data
msg db 'This is the program testing $'
.code
pmain proc far ; can not change far to near
setproc
prtout
ret
pmain endp
.stack 200h ; not required
end pmain
|
|
ช่วงปี 48 และ 49 ผมได้มีโอกาสเรียน Digital Computer กับอาจารย์ Hans ชาวเยอรมัน ในหลักสูตรสั้น ๆ ท่านสอนให้รู้จัก pspice สำหรับวาด Logic Gate และจบด้วยการใช้โปรแกรม Computer Simulation เพื่อแสดงการทำงานของคอมพิวเตอร์ โดยเฉพาะคำสั่งภาษา Assembly, Memory, Control Bus, Address Bus, Data Bus, Register และ CPU โปรแกรมนี้ท่านเคยนำเสนอมาครั้งหนึ่ง แต่ใช้ได้เฉพาะเครื่องเก่า ต่อมาผมตรวจพบว่าเขียนด้วย Pascal 7.0 จึงหา Patch มาแก้ปัญหา และสามารถนำมาใช้ได้กับเครื่องใหม่ รวมทั้งการแก้ไขภาษาเยอรมันบางส่วนเป็นภาษาไทย |
|
โปรแกรม Assembler-Intel 8086 พบในเว็บเพจของ อ.ธนาวินท์ ที่สอน 204221 : Computer Organization and Assembly Language ซึ่งมี Powerpoint ที่ละเอียดมาก และพบว่าโปรแกรม emu8086 สามารถ download ได้จาก http://www.emu8086.com |
|
| "ไม่เริ่มต้นในวันนี้ จะไม่มีทางสำเร็จในวันพรุ่ง" โดย โยฮัน ว็อล์ฟกัง ฟ็อน เกอเทอ |