| 
Loop กำหนดรอบ ใช้ debug สร้าง .com พิมพ์ A 16 อักษร
C:\>debug x1.com
-a
11BC:0100 mov ah,02    ; คู่กับ int 21 คือการพิมพ์อักษรใน dl
11BC:0102 mov cx,0010  ; กำหนดจำนวน loop ใน Register CX (10h = 16)
11BC:0105 mov dl,41
11BC:0107 int 21
11BC:0109 loop 0105    ; Loop ทำงานเท่าจำนวนใน CX
11BC:010B int 20
11BC:010D
-rcx
CX 0000
:d                    ; พิมพ์ D เพราะโปรแกรมมีขนาด 13 byte
-w
Writing 00010 bytes
-q
C:\>x1.com
AAAAAAAAAAAAAAAA
C:\>
Loop พิมพ์ a จนกว่า Ctrl-Breakเป็นแนวการเขียนโปรแกรมแบบ .com แต่แฟ้มที่ได้เป็น .exe โปรแกรมที่ได้มีขนาด 534 Byte
        ; 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',0ah,0dh,'$'
start:  mov     ah,09h
        lea     dx,msg1
        int     21h
        mov     ah,01h
        int     21h
        jmp     start
cseg    ends                      ; ปิด segment
        end                       ; เลิกการทำงาน
 
Loop รับ 1 อักษร แต่พิมพ์ซ้ำตลอดกาล จนรับ e หรือ E จึงหยุดเป็นแนวการเขียนโปรแกรมแบบ .exe โปรแกรมที่ได้มีขนาด 578 Byte
; Get char and print. It will 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 because non-stop on execute
        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
        lea     dx,x1
        int     21h
        mov     ah,6
top:    mov     dl,0ffh
        int     21h
        jz      typeo
        mov     cl,al
        cmp     al,'E'
        je      finish
        cmp     al,'e'
        je      finish
typeo:  mov     dl,cl
        int     21h
        jmp     top
finish: ret             ; Can not use int 20h
pmain   endp
        .stack  200h    ; not required
        end     pmain
 
Loop รับอักษร เมื่อรับ "b" จะแสดง "you pass" แต่ถ้าไม่พิมพ์ b เข้าไป ก็ไม่แสดงอะไรออกมาเลย
        .model  small   
        .data
pass    db      "b $"
txt     db      "you pass $"
        .code
pmain   proc    far     ; can not change far to near because non-stop on execute
        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,08h  ; รับตัวอักษร 1 ตัวอักษรส่งให้ al โดยไม่แสดงอะไรทางจอภาพเลย
        int     21h
        cmp     al,pass
        je      finish  ; ผลการ cmp ถ้าเท่ากันก็จะทำ finish
        jmp     start
finish: mov     ah,09h  ; แสดงสตริงค์ "you pass"
        lea     dx,txt  
        int     21h
        ret             ; Can not use int 20h
pmain   endp
        .stack  200h    ; not required. It will no warning if you have.
        end     pmain
 
รับอักษรเข้า buffer แล้วนำมาแสดงภายหลัง
; when get abc 
; will show
; a
; b
; c
        .model  small   
        .data
v       db      10 dup(?),00
        .code
pmain   proc    far     
        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
        mov     es,ax
start:  cld             ; clear direction
        mov     di,offset v
        mov     cx,10   ; maximum is 10 characters
getc: 
        mov     ah,01h  ; get character
        int     21h
        stosb           ; save in buffer 1 char (FIFO)
        cmp     al,0dh  ; check for enter
        je      outc1
        loop    getc
outc1:  cld
        mov     si,offset v
outc2:  
        mov     ah,02h
        mov     dl,0dh
        int     21h
        mov     dl,0ah
        int     21h
        lodsb           ; load from buffer 1 char
        cmp     al,00   ; check for last char
        je      bye
        mov     dl,al
        mov     ah,02h
        int     21h
        jmp     outc2
bye:    ret             ; Can not use int 20h
pmain   endp
        .stack  200h    ; not required
        end     pmain
รับ string จนกว่าจะพิมพ์คำว่า bye
; when get 7 string 
; will show 7 string
; exit when type bye
        .model  small   
        .data
vpar    label   byte     ; byte = 8
vmax    db      8        ; have 8 but can use 7
vlen    db      ?
v       db      8 dup('.'),'$'
n       db      30h
l       db      13,10,'$'
pass    db      'bye'
lpass   equ  lengthof pass
        .code
pmain   proc    far     
        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
        mov     es,ax       
start: 
      ; get string (required)
        mov     ah,0ah
        lea     dx,vpar        
        int     21h
      ; check for input, if not will exit (option)
        test    vlen,0ffh
        jz      bye
      ; print break line  (option)
        mov     ah,09h
        lea     dx,l
        int     21h
      ; print len of input string  (option)
        mov     dl,n ; เป็นเลขธรรมดา เช่น 1 2 3
        add     dl,vlen ; แปลงเป็นรหัส ascii เช่น 31 32 หรือ 33 ต่อยอดจาก 30
        mov     ah,02h
        int     21h
      ; print break line  (option)
        mov     ah,09h
        lea     dx,l
        int     21h
      ; print v in dx (may have .... and no problem on v)  (option)
        mov     ah,09h
        lea     dx,v
        int     21h
      ; compare string of pass and v (required)
        mov     cx,lpass
        mov     si, offset pass
        mov     di, offset v
        repe    cmpsb
        jne     start
bye:    ret             ; Can not use int 20h
pmain   endp
        .stack  200h    ; not required
        end     pmain
ตัวอย่างโปรแกรมตรวจการเป็น PALINDROME
PALINDROME คือ อักษรซ้ำ (ถ้าอักษรที่ป้อนเหมือนกัน ก็จะเป็น ถ้าไม่เหมือนก็ไม่เป็น PALINDROME)
 จาก http://www.experts-exchange.com/Programming/Programming_Languages/Assembly/Q_20803528.html
 เช่น p.asm จะได้ p.exe
 หยุดรอรับ String ลองพิมพ์ abcde เขาบอกว่า ไม่ใช่ PALINDROME
 หยุดรอรับ String ลองพิมพ์ bbbbb เขาบอกว่า เป็น PALINDROME
 
ตัวอย่างโปรแกรม เข้ารหัสแฟ้มไม่ให้ใครอ่านออก และถอดรหัสแฟ้มคืนจาก http://www.laynetworks.com/Assembly%20Program7.htm
 เช่น myfile.old จะได้ myfile.new
 ถ้า encrypt ก็กด e แล้วพิมพ์ชื่อแฟ้ม เช่น letter.txt จากนั้นก็พิมพ์ letter.en
 ถ้า decrypt ก็กด d แล้วพิมพ์ชื่อแฟ้ม เช่น letter.en จากนั้นก็พิมพ์ letter.txt
 ถูกพัฒนาโดย Kailas Jagtap
 ตัวอย่างนี้เป็นต้นฉบับ ผมได้ปรับเป็น macro และใส่ comment ภาษาไทยไว้ที่ macrobasic.htm
 
 
 |