Expert MS-DOS Programming
Expert MS-DOS Programming
Chapter Overview
Defining Segments Runtime Program Structure Interrupt Handling Hardware Control Using I/O Ports
Web site
Examples
Defining Segments
Simplified Segment Directives Explicit Segment Definitions Segment Overrides Combining Segments
Web site
Examples
Web site
Examples
Memory Models
Web site
Examples
FAR call
32-bit (cs:ip) : requires setting both segment and offset values slower execution than NEAR call
Web site
Examples
.STACK Directive
Syntax:
.STACK [stacksize]
Web site
Examples
Web site
Examples
SEGMENT Directive
Syntax:
name SEGMENT [align] [combine] ['class'] statements name ENDS
name identifies the segment; it can either be unique or the name of an existing segment. align can be BYTE, WORD, DWORD, PARA, or PAGE. combine can be PRIVATE, PUBLIC, STACK, COMMON, MEMORY, or AT address. class is an identifier used when identifying a particular type of segment such as CODE or STACK.
Web site
Examples
Segment Example
ExtraData SEGMENT PARA PUBLIC 'DATA' var1 BYTE 1 var2 WORD 2 ExtraData ENDS
name: ExtraData paragraph align type (starts on 16-bit boundary) public combine type: combine with all other public segments having the same name 'DATA' class: 'DATA' (load into memory along with other segments whose class is 'DATA')
Web site
Examples
10
ASSUME Directive
Tells the assembler how to calculate the offsets of labels
Examples:
ASSUME cs:myCode, ds:Data, ss:myStack ASSUME es:ExtraData
Web site
Examples
11
(1 of 2)
cseg SEGMENT 'CODE' ASSUME cs:cseg, ds:data1, es:data2, ss:mystack main PROC mov ax,data1 mov ds,ax mov ax,SEG val2 mov es,ax mov ax,val1 mov bx,val2 mov ax,4C00h int 21h main ENDP cseg ENDS
; DS points to data1 ; ES points to data2 ; data1 segment assumed ; data2 segment assumed ; (same as .exit)
Web site
Examples
12
(1 of 2)
mystack SEGMENT PARA STACK 'STACK' BYTE 100h DUP('S') mystack ENDS
END main
Web site
Examples
13
Segment Overrides
A segment override instructs the processor to use a different segment from the default when calculating an effective address Syntax: segreg:segname
segname:label
cseg SEGMENT 'CODE' ASSUME cs:cseg, ss:mystack main PROC ... mov ax,ds:val1 mov bx,OFFSET AltSeg:var2
Web site
Examples
14
Combining Segments
Segments can be merged into a single segment by the linker, if . . .
their names are the same, and they both have combine type PUBLIC, . . . even when they appear in different source code modules
Example:
cseg SEGMENT PUBLIC 'CODE'
Web site
Examples
15
What's Next
Defining Segments Runtime Program Structure Interrupt Handling Hardware Control Using I/O Ports
Web site
Examples
16
Web site
Examples
17
Web site
Examples
18
Web site
Examples
19
COM Programs
Unmodified binary image of a program PSP created at offset 0 by loader Code, data, stack all in the same segment Code entry point is at offset 0100h, data follows immediately after code Stack located at the end of the segment All segments point to base of PSP Based on TINY memory model Linker uses the /T option Can only run under MS-DOS
Web site
Examples
20
Web site
Examples
21
EXE Programs
Use memory more efficiently than COM programs Stored on disk in two parts:
EXE header record load module (code and data)
PSP created when loaded into memory DS and ES set to the load address CS and IP set to code entry point SS set to the beginning of the stack segment, and SP set to the stack size
Web site
Examples
22
EXE Programs
Sample EXE structure shows overlapping code, data, and stack segments:
Offset
00 code data stack 20 30 130 (64K) (64K) (64K)
Web site
Examples
23
Web site
Examples
24
What's Next
Defining Segments Runtime Program Structure Interrupt Handling Hardware Control Using I/O Ports
Web site
Examples
25
Interrupt Handling
Overview Hardware Interrupts Interrupt Control Instructions Writing a Custom Interrupt Handler Terminate and Stay Resident Programs The No_Reset Program
Web site
Examples
26
Overview
Interrupt handler (interrrupt service routine) performs common I/O tasks
can be called as functions can be activated by hardware events
Examples:
video output handler critical error handler keyboard handler divide by zero handler Ctrl-Break handler serial port I/O
Web site
Examples
27
Web site
Examples
28
Hardware Interrupts
Generated by the Intel 8259 Programmable Interrupt Contoller (PIC)
in response to a hardware signal
Web site
Examples
29
Web site
Examples
30
Web site
Examples
31
Web site
Examples
32
What's Involved
Write a new handler Load it into memory Replace entry in interrupt vector table Chain to existing interrupt hander (usually)
Web site
Examples
33
Web site
Examples
34
Web site
Examples
35
Web site
Examples
36
Web site
Examples
37
Keyboard example
replace the INT 9 vector so it points to our own handler check, or filter certain keystroke combinations, using our handler forward-chain to the existing INT 9 handler to do normal keyboard processing
Web site
Examples
38
(1 of 5)
checks for the Ctrl and Alt keys permits a system reset only if the Right shift key is also held down
Insert mode on Caps lock is on Num lock is on Scroll lock is on ALT key held down CTRL key held down Left shift key held down Right shift key held down
The keyboard status byte indicates the current state of special keys:
1 7 1 6 1 5 1 4 1 3 1 2 1 1 1 0
(bit position)
Web site
Examples
39
(2 of 5)
Web site
Examples
40
(3 of 5)
Check to see if the Ctrl and Alt keys are held down:
L2: test jz test jz ah,ctrl_key L5 ah,alt_key L5 ; ; ; ; Ctrl key held down? no: exit ALT key held down? no: exit
Web site
Examples
41
(4 of 5)
read keyboard port Del key pressed? no: exit right shift key pressed? yes: allow system reset
Turn off the Ctrl key and write the keyboard flag byte back to memory:
L4: and mov ah,NOT ctrl_key es:[di],ah ; turn off bit for CTRL ; store keyboard_flag
Web site
Examples
42
(5 of 5)
Pop the flags and registers off the stack and execute a far jump to the existing BIOS INT 9h routine:
L5: pop pop pop popf jmp di ax es ; restore regs & flags
cs:[old_interrupt9]
Web site
Examples
43
What's Next
Defining Segments Runtime Program Structure Interrupt Handling Hardware Control Using I/O Ports
Web site
Examples
44
port based
data written to port using the OUT instruction data read from port using the IN instruction
Web site
Examples
45
Input-Ouput Ports
ports numbered from 0 to FFFFh keyboard controller chip sends 8-bit scan code to port 60h
triggers a hardware interrupt 9
accumulator is AL, AX, or EAX port is a constant between 0 and FFh, or a value in DX betweeen 0 and FFFFh
Web site
Examples
46
PC Sound Program
Generates sound through speaker speaker control port: 61h Intel 8255 Programmable Peripheral Interface chip turns the speaker on and off Intel 8253 Timer chip controls the frequency Source code
Web site
Examples
47
Boot Process
ROM beginning at FFFF0h. Power on: set CS to FFFF[0] and IP to 0. The first instruction to execute is at location FFFF0h which is the entry point of BIOS. BIOS then establishes the Interrupt Vector Table among others.
Web site
Examples
48
Summary
Explicit segment definitions used often in custom code libraries Directives: SEGMENT, ENDS, ASSUME Transient programs Program segment prefix (PSP) Interrupt handlers, interrupt vector table Hardware interrupt, 8259 Programmable Interrupt Controller, interrupt flag Terminate and Stay Resident (TSR) Memory-mapped and port-based I/O
Web site
Examples
49
The End
Web site
Examples
50