Documente Academic
Documente Profesional
Documente Cultură
Assembly Languages
MS-DOS & BIOS-level Programming
Pu-Jen Cheng
Adapted from the slides prepared by Kip Irvine for the book,
Assembly Language for Intel-Based Computers, 5th Ed
Chapter Overview
Real-Address Mode
MS-DOS Memory Organization
MS-DOS Memoryy Map
p
Redirecting Input-Output
Software Interrupts
INT Instruction
Interrupt Vectoring Process
Common Interrupts
Real-Address Mode
Memory Models
NEAR segment
FAR segment
.MODEL Directive
.model small
.model medium
.MODEL Directive
.model compact
.model flat
ROM BIOS
F0000
Reserved
C0000
VRAM
Video Graphics
A0000
00400
00000
640K RAM
Redirecting Input-Output
(1 of 2)
Redirecting Input-Output (2 of 2)
Software Interrupts
The Interrupt Vector Table (IVT) holds a 32-bit segmentoffset address for each possible interrupt handler.
Interrupt Service Routine (ISR) is another name for interrupt
handler.
Calling program
mov...
int 10h
add...
3069
F000:F065
F000:F065
F066
F067
F068
.
.
F000:AB62
sti 3
cld
push es
.
.
IRET
Common Interrupts
INT
INT
INT
INT
INT
INT
Hardware Interrupts
priority-based
i it b d interrupt
i t
t scheduler
h d l
brokers simultaneous interrupt requests
prevents low-priority interrupt from interrupting a highpriority interrupt
0
1
2
3
4
5
6
7
System timer
Keyboard
Programmable Interrupt Controller
COM2 (serial)
COM1 (serial)
LPT2 (printer)
Floppy disk controller
LPT1 (printer)
disables
di
bl externall interrupts
i
used before critical code sections that cannot be
interrupted
suspends the system timer
What's Next
; terminate process
; return code
ah,40h
bx,1
cx,LENGTHOF message
dx,OFFSET message
21h
bytesWritten,ax
; keyboard input
; don't wait for input
; no character? repeat loop
; character pressed: save it
; display registers
(2 of 2)
ah,0Bh
21h
al,0
L1
ah,1
21h
char,al
;
;
;
;
; write to output
; keyboard handle
; max bytes to read
; target location
; save character count
Date/Time Functions
ah,2Ah
21h
year,cx
year cx
month,dh
day,dl
dayOfWeek,al
ah,2Bh
cx,year
dh month
dh,month
dl,day
21h
al,0
failed
ah,2Ch
21h
hours,ch
minutes,cl
seconds,dh
ah,2Dh
ch,hours
cl,minutes
dh,seconds
21h
al,0
failed
Time: 23:01:23
What's Next
AX = 716Ch
BX = access mode (0 = read, 1 = write, 2 = read/write)
CX = attributes (0 = normal, 1 = read only, 2 = hidden,
y
, 8 = volume ID,, 20h = archive))
3 = system,
DX = action (1 = open, 2 = truncate, 10h = create)
DS:SI = segment/offset of filename
DI = alias hint (optional)
ax,716Ch
bx,2
cx,0
dx,10h + 02h
si,OFFSET Filename
21h
failed
handle,ax
actionTaken,cx
;
;
;
;
extended open/create
read-write
normal attribute
action: create + truncate
; file handle
; action taken to open file
ax,716Ch
bx,0
cx,0
dx,1
si,OFFSET Filename
21h
failed
handle,ax
actionTaken,cx
;
;
;
;
extended open/create
read-only
normal attribute
open existing file
; file handle
; action taken to open file
ah,42h
al,0
bx,handle
cx
cx,offsetHi
offsetHi
dx,offsetLo
21h
ax,5706h
bx,handle
; handle of open file
21h
error
date,dx
time,cx
milliseconds,si
ReadString Procedure
The ReadString procedure from the Irvine16 library reads a
string from standard input and returns a null-terminated string.
When calling it, pass a pointer to a buffer in DX. Pass a count
of the maximum number of characters to input, plus 1, in CX.
Writestring inputs the string from the user, returning when either
of the following events occurs:
1.CX 1 characters were entered.
2.The user pressed the Enter key.
.data
buffer BYTE 20 DUP(?)
.code
mov dx,OFFSET buffer
mov cx,LENGTHOF buffer
call ReadString
ReadString Implementation
ReadString PROC
push cx
; save registers
push si
push cx
; save character count
mov si,dx
; point to input buffer
dec cx
; save room for null byte
L1:mov ah,1
; function: keyboard input
int 21h
; returns character in AL
cmp al,0Dh
; end of line?
je
L2
; yes: exit
mov [si],al
; no: store the character
inc si
; increment buffer pointer
loop L1
; loop until CX=0
L2:mov BYTE PTR [si],0 ; insert null byte
pop ax
; original digit count
sub ax,cx
; AX = size of input string
pop si
; restore registers
pop cx
ret
ReadString ENDP
; returns AX = size of string
PC-BIOS
Contents:
Timer data
What's Next
Introduction
Keyboard Input with INT 16h
VIDEO Programming
g
g with INT 10h
Drawing Graphics Using INT 10h
Memory-Mapped Graphics
Mouse Programming
sc = scan code
ac = ASCII code
Keyboard
sc
sc
sc, ac
typeahead buffer
sc, ac
ac
INT 9h handler
Keyboard Flags
16-bits, located at 0040:0017h 0018h.
ah,11h
16h
noKey
ah,10h
ah 10h
16h
ah,scanCode
quit
L1
;
;
;
;
al,1
; no key pressed
; clear zero flag
What's Next
Introduction
Keyboard Input with INT 16h
VIDEO Programming with INT 10h
Drawing Graphics Using INT 10h
Memory-Mapped Graphics
Mouse Programming
Basic Background
Controlling the Color
INT 10h Video Functions
Library Procedure Examples
Video Modes
character
h
output, using
i hardware
h d
or software-based
f
b d
font table
mode 3 (color text) is the default
default range of 80 columns by 25 rows.
color attribute byte contains foreground and background
colors
Examples:
Attribute byte:
Light
Li
ht gray text
t t on a bl
blue background:
b k
d
(blue SHL 4) OR lightGray
White text on a red background:
(red SHL 4) OR white
01h:
02h:
03h:
06h:
07h:
08h:
;
;
;
;
;
;
write character/attribute
character to display
video page 0
attribute
set blink/intensity bit
display it one time
Gotoxy Procedure
;------------------------------------------------Gotoxy PROC
;
; Sets the cursor position on video page 0.
; Receives: DH,DL = row, column
; Returns:
R t
nothing
thi
;-------------------------------------------------pusha
mov ah,2
mov bh,0
int 10h
popa
ret
Gotoxy ENDP
Clrscr Procedure
Clrscr PROC
pusha
mov
ax,0600h
mov
cx,0
mov
dx,184Fh
(24,79)
mov
bh 7
bh,7
int
10h
mov
ah,2
mov
bh,0
mov
dx,0
int
10h
popa
ret
Clrscr ENDP
; scroll window up
; upper left corner (0,0)
; lower right corner
;
;
;
;
;
normal
l attribute
tt ib t
call BIOS
locate cursor at 0,0
video page 0
row 0, column 0
What's Next
Introduction
Keyboard Input with INT 16h
VIDEO Programming
g
g with INT 10h
Drawing Graphics Using INT 10h
Memory-Mapped Graphics
Mouse Programming
Slow performance
Easy to program
0Ch: Write graphics pixel
0Dh: Read graphics pixel
DrawLine Program
ah,0Ch
; write pixel
al,color
; pixel color
bh,0
; video page 0
cx,currentX
10h
DrawHorizLine
DrawVerticalLine
sx = (sOrigX + X)
sy = (sOrigY Y)
What's Next
Introduction
Keyboard Input with INT 16h
VIDEO Programming
g
g with INT 10h
Drawing Graphics Using INT 10h
Memory-Mapped Graphics
Mouse Programming
Memory-Mapped Graphics
OUT Instruction
mov
mov
out
dx,3c8h
al,20h
dx,al
; port address
; value to be sent
; send to the port
Color Indexes
RGB Colors
Additive mixing of light (red, green, blue). Intensities vary
from 0 to 255.
Examples:
What's Next
Introduction
Keyboard Input with INT 16h
VIDEO Programming
g
g with INT 10h
Drawing Graphics Using INT 10h
Memory-Mapped Graphics
Mouse Programming
Mouse Programming
INT 33h, AX = 0
Example:
mov
int
cmp
je
mov
ax,0
33h
ax,0
MouseNotAvailable
numberOfButtons,bx
Show/Hide Mouse
ax,1
33h
ax,2
33h
; show
; hide
INT 33h, AX = 4
Example:
mov
mov
mov
int
ax,4
cx,200
dx,100
33h
; X-position
; Y-position
INT 33h, AX = 5
Example:
mov
mov
int
test
jz
mov
mov
ax,5
bx,0
33h
ax,1
skip
X_coord,cx
Y_coord,dx
; button ID
; left button down?
; no - skip
; yes: save coordinates
INT 33h, AX = 3
Example:
mov
int
test
jne
test
jne
test
jne
mov
mov
ax,3
33h
bx 1
bx,1
Left_Button_Down
bx,2
Right_Button_Down
bx,4
Center_Button_Down
Xcoord,cx
yCoord,dx
Summary