MASM Program Example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; ;
; đây là chương trình in chuỗi ký tự “Hello World” sử dụng chương ; ; trình phục vụ ngắt của DOS – hàm 09 ngắt 21H ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT BYTE STACK 'STACK' ;Định nghĩa đoạn ngăn xếp
DB 100h DUP(?) ;định nghĩa kích thước ngăn xếp là 256 bytes (100h) hellostk ENDS
hellodat SEGMENT BYTE 'DATA' ;định nghĩa đoạn dữ liệu
dos_print EQU 9 ;định nghĩa hằng số bằng lệnh EQU strng DB 'Hello World',13,10,'$' ;định nghĩa chuỗi ký tự
hellodat ENDS
hellocod SEGMENT BYTE 'CODE' ;Định nghĩa đoạn lệnh
START: mov ax, SEG hellodat ;ax <-- địa chỉ bắt đầu của đoạn dữ liệu
mov ds, ax ;ds <-- khởi động thanh ghi đoạn dữ liệu mov ah, dos_print ;ah <-- hàm 9 ngắt 21h.
mov dx,OFFSET strng ;dx <-- địa chỉ bắt đầu của chuỗi dữ liệu
int 21h ;gọi chương trình ngắt của DOS
mov ax, 4c00h ;ax <-- hàm 4c ngắt 21h để dừng chương trình
int 21h ;DOS service interrupt hellocod ENDS
Một cách khác để định nghĩa các đoạnAnother Way to define Segments Another Way to define Segments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Use 'assume' directive to define segment types ; ; Use 'assume' directive to define segment types ;
; Sử dụng chỉ thị assume để định nghĩa các đoạn ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT ;Define a segment DB 100h DUP(?)
hellostk ENDS
hellodat SEGMENT ;define a segment dos_print EQU 9 ;define a constant
strng DB 'Hello World',13,10,'$' ;Define the character string hellodat ENDS
hellocod SEGMENT ;define a segment assume cs:hellocod, ds:hellodat, ss: hellostk
START: mov ax, hellodat ;ax <-- data segment start address
mov ds, ax ;ds <-- initialize data segment register mov ah, dos_print ;ah <-- 9 DOS 21h string function
mov dx,OFFSET strng ;dx <-- beginning of string int 21h ;DOS service interrupt
mov ax, 4c00h ;ax <-- 4c DOS 21h program halt function int 21h ;DOS service interrupt
hellocod ENDS END START
Thêm một cách khác để định nghĩa các đoạnYet another way to define Segs Yet another way to define Segs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Use .stack,.data,.code directives to define segment types ; ; Use .stack,.data,.code directives to define segment types ;
; Sử dụng các chỉ thị .stack, .data, .code để định nghĩa các đoạn ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.stack 100h ; reserve 256 bytes of stack space
.data
dos_print EQU 9 ;define a constant
strng DB 'Hello World',13,10,'$' ;Define the character string .code
START: mov ax, SEG strng ;ax <-- data segment start address
mov ds, ax ;ds <-- initialize data segment register mov ah, dos_print ;ah <-- 9 DOS 21h string function
lea dx,strng ;dx <-- beginning of string int 21h ;DOS service interrupt
mov ax, 4c00h ;ax <-- 4c DOS 21h program halt function int 21h ;DOS service interrupt
.stack 100h.data .data x db ‘0’ .code St:Mov ax,@data Mov ds,ax Mov ah,02 Mov dl,x Int 21h Mov ah,4ch Int 21h End st
.stack 100h.data .data X db ‘0’ .code St:Mov ax,@data Mov ds,ax Mov ah,01 Int 21h Mov ah,02 Mov dl,al Int 21h Mov ah,4ch Int 21h
.stack 100h.data .data X db ‘0’ .code St:Mov ax,@data Mov ds,ax Mov ah,01 Int 21h Mov bl,al Mov ah,01 Int 21h Add al,bl Aaa Add ax,3030h Mov ah,02 Mov dl,al Int 21h Mov ah,4ch Int 21h End st
Thêm một cách khác để định nghĩa các đoạnYet another way to define Segs Yet another way to define Segs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Use .stack,.data,.code directives to define segment types ; ; Use .stack,.data,.code directives to define segment types ;
; Sử dụng các chỉ thị .stack, .data, .code để định nghĩa các đoạn ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.stack 100h ; reserve 256 bytes of stack space .code
START: jmp begin