mov dx,OFFSET MyMessage
mov ax,SEG MyMessage
mov ds,ax
mov ax,@data
mov ds,ax
mov dx,OFFSET MyMessage
.model small
.stack
.data
Message db "Hello World!$" ; message to be display
.code
start proc
mov ax,@data
mov ds,ax
mov dx,OFFSET Message
mov ah,9 ; function 9 - display string
int 21h ; call dos service
mov ax,4c00h ; return to dos DOS
int 21h
start endp
end start
mov ax,data
; a program to demonstrate program flow and input/output
.model tiny
.code
org 100h
start:
mov dx,OFFSET Message ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
mov dx,OFFSET Prompt ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
jmp First_Time
Prompt_Again:
mov dx,OFFSET Another ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
First_Time:
mov dx,OFFSET Again ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
xor ah,ah ; function 00h of
int 16h ; interrupt 16h gets a character
mov bl,al ; save to bl
mov dl,al ; move al to dl
mov ah,02h ; function 02h - display character
int 21h ; call DOS service
cmp bl,'Y' ; is al=Y?
je Prompt_Again ; if yes then display it again
cmp bl,'y' ; is al=y?
je Prompt_Again ; if yes then display it again
theEnd:
mov dx,OFFSET GoodBye ; print goodbye message
mov ah,9 ; using function 9
int 21h ; of interrupt 21h
mov ah,4Ch ; terminate program
int 21h
.DATA
CR equ 13 ; enter
LF equ 10 ; line-feed
Message DB "A Simple Input/Output Program$"
Prompt DB CR,LF,"Here is your first prompt.$"
Again DB CR,LF,"Do you want to be prompted again? $"
Another DB CR,LF,"Here is another prompt!$"
GoodBye DB CR,LF,"Goodbye then.$"
end start
; a program to demonstrate program flow and input/output
.model small
.stack
.code
start:
mov ax,@data
mov ds,ax
mov dx,OFFSET Message ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
mov dx,OFFSET Prompt ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
jmp First_Time
Prompt_Again:
mov dx,OFFSET Another ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
First_Time:
mov dx,OFFSET Again ; display a message on the screen
mov ah,9 ; using function 09h
int 21h ; of interrupt 21h
xor ah,ah ; function 00h of
int 16h ; interrupt 16h gets a character
mov bl,al ; save to bl
mov dl,al ; move al to dl
mov ah,02h ; function 02h - display character
int 21h ; call DOS service
cmp bl,'Y' ; is al=Y?
je Prompt_Again ; if yes then display it again
cmp bl,'y' ; is al=y?
je Prompt_Again ; if yes then display it again
theEnd:
mov dx,OFFSET GoodBye ; print goodbye message
mov ah,9 ; using function 9
int 21h ; of interrupt 21h
mov ah,4Ch ; terminate program
int 21h
.DATA
CR equ 13 ; enter
LF equ 10 ; line-feed
Message DB "A Simple Input/Output Program$"
Prompt DB CR,LF,"Here is your first prompt.$"
Again DB CR,LF,"Do you want to be prompted again? $"
Another DB CR,LF,"Here is another prompt!$"
GoodBye DB CR,LF,"Goodbye then.$"
end start
tlink16 filename.obj/t
jmp ALabel
.
.
.
ALabel:
CMP register or variable, value
cmp ax,3; is AX = 3?
je correct; yes
cmp al,'Y' ; compare the value in al with Y
je ItsYES ; if it is equal then jump to ItsYES
ADD operand1,operand2
SUB operand1,operand2
MUL register or variable
IMUL register or variable
DIV register or variable
IDIV register or variable
PROC AProcedure
.
. ; some code to do something
.
ret; if this is not here then your computer will crash
ENDP Aprocedure
call Aprocedure
.model tiny
.code
org 100h
Start:
call Display_Hi ; Call the procedure
mov ax,4C00h ; return to DOS
int 21h
Display_Hi PROC
mov dx,OFFSET HI
mov ah,9
int 21h
ret
Display_Hi ENDP
HI DB "Hello World!$" ; define a message
end Start
.model tiny
.code
org 100h
Start:
mov dh,4 ; row to print character on
mov dl,5 ; column to print character on
mov al,254 ; ascii value of block to display
mov bl,4 ; colour to display character
call PrintChar ; print our character
mov ax,4C00h ; terminate program
int 21h
PrintChar PROC NEAR
push bx ; save registers to be destroyed
push cx
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
int 10h ; row and col are already in dx
pop bx ; restore bx
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop cx ; restore registers
ret ; return to where it was called
PrintChar ENDP
end Start
; this a procedure to print a block on the screen using memory
; to pass parameters (cursor position of where to print it and
; colour).
.model tiny
.code
org 100h
Start:
mov Row,4 ; row to print character
mov Col,5 ; column to print character on
mov Char,254 ; ascii value of block to display
mov Colour,4 ; colour to display character
call PrintChar ; print our character
mov ax,4C00h ; terminate program
int 21h
PrintChar PROC NEAR
push ax cx bx ; save registers to be destroyed
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
mov dh,Row
mov dl,Col
int 10h ; call Bios service
mov al,Char
mov bl,Colour
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop bx cx ax ; restore registers
ret ; return to where it was called
PrintChar ENDP
; variables to store data
Row db ?
Col db ?
Colour db ?
Char db ?
end Start
; this a procedure to print a block on the screen using the
; stack to pass parameters (cursor position of where to print it
; and colour).
.model tiny
.code
org 100h
Start:
mov dh,4 ; row to print string on
mov dl,5 ; column to print string on
mov al,254 ; ascii value of block to display
mov bl,4 ; colour to display character
push dx ax bx ; put parameters onto the stack
call PrintString ; print our string
pop bx ax dx ;restore registers
mov ax,4C00h ;terminate program
int 21h
PrintString PROC NEAR
push bp ; save bp
mov bp,sp ; put sp into bp
push cx ; save registers to be destroyed
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
mov dx,[bp+8] ; restore dx
int 10h ; call bios service
mov ax,[bp+6] ; character
mov bx,[bp+4] ; attribute
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop cx ; restore registers
pop bp
ret ; return to where it was called
PrintString ENDP
end Start
.MODEL MemoryModel
.model tiny
Name_of_macro macro
;
;a sequence of instructions
;
endm
SaveRegs macro
push ax
push bx
push cx
push dx
endm
RestoreRegs macro
pop dx
pop cx
pop bx
pop ax
endm
SaveRegs
; some other instructions
RestoreRegs
LOCAL name
OutMsg macro SomeText
local PrintMe,SkipData
jmp SkipData
PrintMe db SomeText,'$'
SkipData:
push ax dx cs
mov dx,OFFSET cs:PrintMe
mov ah,9
int 21h
pop cs dx ax
endm
AddMacro macro num1,num2,result
push ax ; save ax from being destroyed
mov ax,num1 ; put num1 into ax
add ax,num2 ; add num2 to it
mov result,ax ; move answer into result
pop ax ; restore ax
endm
movsb ; move byte
movsw ; move word
movsd ; move double word
cmpsb ; compare byte
cmpsw ; compare word
cmpsd ; compare double word
DS:SI to ES:DI
scasb ; search for AL
scasw ; search for AX
scasd ; search for EAX
stosb ; move AL into ES:DI
stosw ; move AX into ES:DI
stosd ; move EAX into ES:DI
lodsb ; move ES:DI into AL
lodsw ; move ES:DI into AX
lodsd ; move ES:DI into EAX
.model small
.stack
.code
mov ax,@data ; ax points to of data segment
mov ds,ax ; put it into ds
mov es,ax ; put it in es too
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message1 ; ds:dx points to message
int 21h ; call dos function
cld ; clear direction flag
mov si,OFFSET String1 ; make ds:si point to String1
mov di,OFFSET String2 ; make es:di point to String2
mov cx,18 ; length of strings
rep movsb ; copy string1 into string2
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message2 ; ds:dx points to message
int 21h ; call dos function
mov dx,OFFSET String1 ; display String1
int 21h ; call DOS service
mov dx,OFFSET Message3 ; ds:dx points to message
int 21h ; call dos function
mov dx,OFFSET String2 ; display String2
int 21h ; call DOS service
mov si,OFFSET Diff1 ; make ds:si point to Diff1
mov di,OFFSET Diff2 ; make es:di point to Diff2
mov cx,39 ; length of strings
repz cmpsb ; compare strings
jnz Not_Equal ; jump if they are not the same
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message4 ; ds:dx points to message
int 21h ; call dos function
jmp Next_Operation
Not_Equal:
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message5 ; ds:dx points to message
int 21h ; call dos function
Next_Operation:
mov di,OFFSET SearchString ; make es:di point to string
mov cx,36 ; length of string
mov al,'H' ; character to search for
repne scasb ; find first match
jnz Not_Found
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message6 ; ds:dx points to message
int 21h ; call dos function
jmp Lodsb_Example
Not_Found:
mov ah,9 ; function 9 - display string
mov dx,OFFSET Message7 ; ds:dx points to message
int 21h ; call dos function
Lodsb_Example:
mov ah,9 ; function 9 - display string
mov dx,OFFSET NewLine ; ds:dx points to message
int 21h ; call dos function
mov cx,17 ; length of string
mov si,OFFSET Message ; DS:SI - address of string
xor bh,bh ; video page - 0
mov ah,0Eh ; function 0Eh - write character
NextChar:
lodsb ; AL = next character in string
int 10h ; call BIOS service
loop NextChar
mov ax,4C00h ; return to DOS
int 21h
.data
CR equ 13
LF equ 10
NewLine db CR,LF,"$"
String1 db "This is a string!$"
String2 db 18 dup(0)
Diff1 db "This string is nearly the same as Diff2$"
Diff2 db "This string is nearly the same as Diff1$"
Equal1 db "The strings are equal$"
Equal2 db "The strings are not equal$"
Message db "This is a message"
SearchString db "1293ijdkfjiu938uHello983fjkfjsi98934$"
Message1 db "Using String instructions example program.$"
Message2 db CR,LF,"String1 is now: $"
Message3 db CR,LF,"String2 is now: $"
Message4 db CR,LF,"Strings are equal!$"
Message5 db CR,LF,"Strings are not equal!$"
Message6 db CR,LF,"Character was found.$"
Message7 db CR,LF,"Character was not found.$"
end
mov ah,30h ; function 30h - get MS-DOS version
int 21h ; call DOS function
mov ah,33h ; function 33h - actual DOS version
mov al,06h ; subfunction 06h
int 21h ; call interrupt 21h
push ax bx cx dx ; save registers
pop dx cx bx ax ; restore registers
temp = SP
push ax
push cx
push dx
push bx
push temp
push bp
push si
push di
SHL Unsigned multiple by two
SHR Unsigned divide by two
SAR Signed divide by two
SAL same as SHL
SHL operand1,operand2
mov cx,100 ; 100 times to loop
Label:
.
.
.
Loop Label: ; decrement CX and loop to Label
mov cx,100 ; 100 times to loop
Label:
dec cx ; CX = CX-1
jnz Label ; continue until done
; example program to demonstrate how to use a debugger
.model tiny
.code
org 100h
start:
push ax ; save value of ax
push bx ; save value of bx
push cx ; save value of cx
mov ax,10 ; first parameter is 10
mov bx,20 ; second parameter is 20
mov cx,3 ; third parameter is 3
Call ChangeNumbers
pop cx ; restore cx
pop bx ; restore bx
pop ax ; restore dx
mov ax,4C00h ; exit to dos
int 21h
ChangeNumbers PROC
add ax,bx ; adds number in bx to ax
mul cx ; multiply ax by cx
mov dx,ax ; return answer in dx
ret
ChangeNumbers ENDP
end start
td name of file
cs:0000 50 push ax
cs:0001 53 push bx
cs:0002 51 push cx
.model tiny
.code
org 100h
start:
mov dh,12 ; cursor col
mov dl,32 ; cursor row
mov ah,02h ; move cursor to the right place
xor bh,bh ; video page 0
int 10h ; call bios service
mov dx,OFFSET Text; DS:DX points to message
mov ah,9 ; function 9 - display string
int 21h ; all dos service
mov ax,4C00h ; exit to dos
int 21h
Text DB "This is some text$"
end start
.model small
.stack
.code
mov ax,@data ; set up ds as the segment for data
mov ds,ax
mov ah,40h ; function 40h - write file
mov bx,1 ; handle = 1 (screen)
mov cx,17 ; length of string
mov dx,OFFSET Text ; DS:DX points to string
int 21h ; call DOS service
mov ax,4C00h ; terminate program
int 21h
.data
Text DB "This is some text"
End
.model small
.stack
.code
mov ax,@data ; set up ds as the segment for data
mov es,ax ; put this in es
mov bp,OFFSET Text ; ES:BP points to message
mov ah,13h ; function 13 - write string
mov al,01h ; attrib in bl,move cursor
xor bh,bh ; video page 0
mov bl,5 ; attribute - magenta
mov cx,17 ; length of string
mov dh,5 ; row to put string
mov dl,5 ; column to put string
int 10h ; call BIOS service
mov ax,4C00h ; return to DOS
int 21h
.data
Text DB "This is some text"
end
.model small
.stack
.code
mov ax,0B800h ; segment of video buffer
mov es,ax ; put this into es
xor di,di ; clear di, ES:DI points to video memory
mov ah,4 ; attribute - red
mov al,"G" ; character to put there
mov cx,4000 ; amount of times to put it there
cld ; direction - forwards
rep stosw ; output character at ES:[DI]
mov ax,4C00h ; return to DOS
int 21h
end
; write a string direct to video memory
.model small
.stack
.code
mov ax,@data
mov ds,ax
mov ax,0B800h ; segment of video buffer
mov es,ax ; put this into es
mov ah,3 ; attribute - cyan
mov cx,17 ; length of string to print
mov si,OFFSET Text ; DX:SI points to string
xor di,di
Wr_Char:
lodsb ; put next character into al
mov es:[di],al ; output character to video memory
inc di ; move along to next column
mov es:[di],ah ; output attribute to video memory
inc di
loop Wr_Char ; loop until done
mov ax,4C00h ; return to DOS
int 21h
.data
Text DB "This is some text"
end
mov es:[di],al
.model small
.stack
.data
NoSupport db "Mode 13h is not supported on this computer."
db ,"You need either a MCGA or VGA video"
db ,"card/monitor.$"
Supported db "Mode 13h is supported on this computer.$"
.code
mov ax,@data ; set up DS to point to data segment
mov ds,ax ; use ax
call Check_Mode_13h ; check if mode 13h is possible
jc Error ; if cf=1 there is an error
mov ah,9 ; function 9 - display string
mov dx,OFFSET Supported ; DS:DX points to message
int 21h ; call DOS service
jmp To_DOS ; exit to DOS
Error:
mov ah,9 ; function 9 - display string
mov dx,OFFSET NoSupport ; DS:DX points to message
int 21h ; call DOS service
To_DOS:
mov ax,4C00h ; exit to DOS
int 21h
Check_Mode_13h PROC ; Returns: CF = 1 Mode 13h not possible
mov ax,1A00h ; Request video info for VGA
int 10h ; Get Display Combination Code
cmp al,1Ah ; Is VGA or MCGA present?
je Mode_13h_OK ; mode 13h is supported
stc ; mode 13h isn't supported CF=1
Mode_13h_OK:
ret
Check_Mode_13h ENDP
end
; example of plotting pixels in mode 13 using bios services -
; INT 10h
.model tiny
.code
org 100h
start:
mov ax,13 ; mode = 13h
int 10h ; call bios service
mov ah,0Ch ; function 0Ch
mov al,4 ; color 4 - red
mov cx,160 ; x position = 160
mov dx,100 ; y position = 100
int 10h ; call BIOS service
inc dx ; plot pixel downwards
int 10h ; call BIOS service
inc cx ; plot pixel to right
int 10h ; call BIOS service
dec dx ; plot pixel up
int 10h ; call BIOS service
xor ax,ax ; function 00h - get a key
int 16h ; call BIOS service
mov ax,3 ; mode = 3
int 10h ; call BIOS service
mov ax,4C00h ; exit to DOS
int 21h
end start
Set Pixel
Call
AH = 0Ch
AL = Pixel Value
If bit 7 is set, then value is XOR-ed (not in 256 color mode)
BH = Page Number
CX = Pixel Column
DX = Pixel Row
Return
N/A
Offset = X + ( Y * 320 )
mov es:[di], color