Top Banner
Chapter16
44

Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Chapter16

Page 2: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

A program with no include filesTITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams to use.datamsg1 db "simple assembly example",0dh,0ah,'$'

.codemain PROC

mov ax,@datamov ds,axmov dx,offset msg1mov ah,9int 21hmov ax,4c00hint 21h

main ENDPEND main

Page 3: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Running simple without batch file: DOS screen

C:\MASM615>ml simple.asmMicrosoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: simple.asm

Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

Object Modules [.obj]: simple.objRun File [simple.exe]: "simple.exe"List File [nul.map]: NULLibraries [.lib]:Definitions File [nul.def]:

C:\MASM615>simplesimple assembly example

C:\MASM615>

Page 4: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Invoking the assembler and linker separately

• By invoking the assembler (ml.exe) using masm.exe and the linker (link) separately, you can define the name of the exe and other output files instead of using default naming.

• You can also link multiple modules into a single executable using:

Link modA ModB modC• Where the first file named will be the “main”

Page 5: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Running simple without batch file: DOS screen, version 2…

C:\MASM615>masm simpleMicrosoft (R) MASM Compatibility DriverCopyright (C) Microsoft Corp 1993. All rights reserved.

Invoking: ML.EXE /I. /Zm /c simple.asm

Microsoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: simple.asm

C:\MASM615>link simple

Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

Run File [simple.exe]:List File [nul.map]:Libraries [.lib]:Definitions File [nul.def]:

C:\MASM615>simplesimple assembly example

Page 6: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Explicit segment definition: running explicit

C:\MASM615>masm explicitMicrosoft (R) MASM Compatibility DriverCopyright (C) Microsoft Corp 1993. All rights reserved. Invoking: ML.EXE /I. /Zm /c explicit.asmMicrosoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: explicit.asmC:\MASM615>C:\MASM615>link explicitMicrosoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994Copyright (C) Microsoft Corp 1984-1993. All rights reserved.Run File [explicit.exe]:List File [nul.map]:Libraries [.lib]:Definitions File [nul.def]:

C:\MASM615>explicitsimple assembly example

Page 7: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Explicit.asmTITLE Explicit (Explicit.asm)

CSEG SEGMENT 'CODE'ASSUME CS:CSEG,DS:DSEG,SS:SSEG

main PROCmov ax,DSEGmov ds,axmov dx,offset msg1mov ah,9int 21hmov ax,4c00hint 21h

main ENDP

CSEG ENDSSSEG SEGMENT PARA STACK 'STACK'byte 100 dup(0)SSEG ENDS

DSEG SEGMENT 'DATA'msg1 db "simple assembly example",0dh,0ah,'$'DSEG ENDSEND main

Page 8: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

About ASSUME

• The assume directive does not point registers (ds, es) where they are supposed to go. The programmer must still do that.

• It tells the assembler to assemble code assuming that’s where the segments will be.

Page 9: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Multiple code segments

• A call to a proc in a different code segment is a far call, requiring both cs and ip be saved (pushed), changed to point to the called proc, then recovered (popped). Of course the system does the pushing and popping.

• The programmer must define procs as far when appropriate.

Page 10: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Multcode example from text: note far procTITLE Multiple Code Segments (MultCode.asm).model small,stdcall.stack 100hWriteString PROTO.datamsg1 db "First Message",0dh,0ah,0msg2 db "Second Message",0dh,0ah,"$".codemain PROC

mov ax,@datamov ds,axmov dx,OFFSET msg1call WriteString ; NEAR callcall Display ; FAR call.exit

main ENDP.code OtherCodeDisplay PROC FAR mov ah,9 mov dx,offset msg2 int 21h retDisplay ENDPEND main

Page 11: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Assemble and run…C:\MASM615>make16 multcode Assembling: multcode.asm Volume in drive C has no label. Volume Serial Number is 07D2-0208

Directory of C:\masm615

11/29/2006 01:09 PM 457 MultCode.asm11/29/2006 01:11 PM 714 multcode.obj11/29/2006 01:11 PM 2,597 multcode.lst11/29/2006 01:11 PM 5,948 multcode.exe 4 File(s) 9,716 bytes 0 Dir(s) 4,028,284,928 bytes freePress any key to continue . . .

C:\MASM615>multcodeFirst MessageSecond Message

Page 12: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

About small model

• When you use model small the assembler automatically defines DGROUP for near data, accessible via DS or SS. .DATA and .DATA? Both create near data segments, placed in DGROUP

• .FARDATA and .FARADATA? Create far data segments in the small memory model.

• DS (or ES) must point to a sement before a variable in the segment can be accessed.

• The segment defined by .CODE in the small model is named _TEXT.

• You can view the naming conventions by examining a lst file.

Page 13: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Here’s part of the lst for multcodeSegments and Groups:

N a m e Size Length Align Combine Class

DGROUP . . . . . . . . . . . . . GROUP_DATA . . . . . . . . . . . . . 16 Bit 0021 Word Public 'DATA'STACK . . . . . . . . . . . . . 16 Bit 0100 Para Stack 'STACK' OtherCode . . . . . . . . . . . 16 Bit 0008 Word Public 'CODE'_TEXT . . . . . . . . . . . . .16 Bit 0014 Word Public 'CODE'

Procedures, parameters and locals:

N a m e Type Value Attr

Display . . . . . . . . . . . . P Far 0000 OtherCode Length= 0008 Public STDCALL

WriteString . . . . . . . . . . P Near 0000 Length= 0000 External STDCALLmain . . . . . . . . . . . . . . P Near 0000 _TEXT Length= 0014 Public STDCALL

Page 14: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Modified Multdata from text: multiple data segmentsTITLE Multiple Data Segments (MultData.asm); This program shows how to explicitly declare; multiple data segments.; Last update: 2/1/02cseg SEGMENT 'CODE' ASSUME cs:cseg, ds:data1, ss:mystackmain PROC

mov ax,data1 ; point DS to data1 segmentmov ds,axmov ax,seg val2 ; point ES to data2 segmentmov es,axmov ah,2mov dx,val1 ; requires ASSUME for DSint 21hmov dx,es:val2 ; ASSUME not requiredint 21h

mov ax,4C00h ; return to MS-DOSint 21h

main ENDPcseg ENDSdata1 SEGMENT 'DATA' ; specify class type

val1 dw 65;;;letter 'A;data1 ENDSdata2 SEGMENT 'DATA'

val2 dw 57;;;digit char 9data2 ENDSmystack SEGMENT para STACK 'STACK'

BYTE 100h dup('S')mystack ENDSEND main

Page 15: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

My version has output (A9)C:\MASM615>make16 multdata Assembling: multdata.asm Volume in drive C has no label. Volume Serial Number is 07D2-0208

Directory of C:\masm615

11/29/2006 01:31 PM 830 MultData.asm11/29/2006 01:32 PM 533 multdata.obj11/29/2006 01:32 PM 1,964 multdata.exe11/29/2006 01:32 PM 2,297 multdata.lst 4 File(s) 5,624 bytes 0 Dir(s) 4,027,875,328 bytes freePress any key to continue . . .

C:\MASM615>multdataA9

Page 16: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Segment overrides

• If you plan to access a value (like [bx] or [si]) which has a default segment, via a different segment, you must use an override.

• Default segment assignments include: es:di, ds:si, ds:bx, cs:ip, ss:sp

Page 17: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Segment overrides

• You can access a variable without an assume by using a segment override, that is, explicitly providing a segment with an offset.

• This has the form

someseg:someofs

Page 18: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Programs with multiple modules

• One module (B) may define code or data symbols used in another module (A).

• In order to assemble module A the assembler needs to know what type the symbols will have (near, far, byte, word, dword, etc). An EXTERN directive assures the assembler the symbols are defined elsewhere. (It is the programmer’s responsibility to do this).

Page 19: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Programs with multiple modules

• Module B needs to be assembled so that some symbols are made available at link time so that their addresses can be resolved by the linker.

• The PUBLIC directive indicates that a variable defined in this module may be needed in another module.

• It is NOT an error if a variable “declared” PUBLIC is not used elsewhere, but it is an error if a variable declared “EXTERN” is NOT defined in another module and declared “PUBLIC” there.

Page 20: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Programs with multiple modules

• Different directives tell the assembler how to combine segments with the same name.

• COMMON means put them at the same start address (over each other)

• PUBLIC means combine into a single segment• PRIVATE is the default, meaning segments with the

same name in different modules will NOT be combined into a single segment.

• ALIGN tells the assembler on what address boundary to begin. PARA means paragraph (address ends in 0000). Higher align types mean faster variable access.

Page 21: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Programs may have multiple segments which are combined

TITLE Segment Example (submodule, SEG2A.ASM)PUBLIC subroutine_1, var2cseg SEGMENT BYTE PUBLIC 'CODE'ASSUME cs:cseg, ds:dseg

subroutine_1 PROC ; called from MAINmov ah,9mov dx,OFFSET msgint 21hret

subroutine_1 ENDPcseg ENDSdseg SEGMENT WORD PUBLIC 'DATA'var2 WORD 2000h ; accessed by MAINmsg BYTE 'Now in Subroutine_1' BYTE 0Dh,0Ah,'$'dseg ENDSEND

Page 22: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Seg2 from textTITLE Segment Example (main module, Seg2.asm)EXTRN var2:WORDsubroutine_1 PROTOcseg SEGMENT BYTE PUBLIC 'CODE'ASSUME cs:cseg,ds:dseg, ss:ssegmain PROC

mov ax,dseg ; initialize DSmov ds,ax

mov ax,var1 ; local variablemov bx,var2 ; external variablecall subroutine_1 ; external procedure

mov ax,4C00h ; exit to OSint 21h

main ENDPcseg ENDSdseg SEGMENT WORD PUBLIC 'DATA' ; local data segment

var1 WORD 1000hdseg endssseg SEGMENT STACK 'STACK' ; stack segment

BYTE 100h dup('S')sseg ENDSEND main

Page 23: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Assemble each module separately

C:\MASM615\EXAMPLES\CH16>masm seg2.asmMicrosoft (R) MASM Compatibility DriverCopyright (C) Microsoft Corp 1993. All rights reserved.

Invoking: ML.EXE /I. /Zm /c /Ta seg2.asm

Microsoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: seg2.asmMASM : fatal error A1000: cannot open file : seg2.asm

C:\MASM615\EXAMPLES\CH16>cd seg2

C:\MASM615\EXAMPLES\CH16\SEG2>masm seg2Microsoft (R) MASM Compatibility DriverCopyright (C) Microsoft Corp 1993. All rights reserved.

Invoking: ML.EXE /I. /Zm /c seg2.asm

Microsoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: seg2.asm

Page 24: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Note how to link (list of modules) with main first

C:\MASM615\EXAMPLES\CH16\SEG2>link seg2 seg2a

Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 199Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

Run File [seg2.exe]:List File [nul.map]:Libraries [.lib]:Definitions File [nul.def]:

C:\MASM615\EXAMPLES\CH16\SEG2>seg2Now in Subroutine_1

C:\MASM615\EXAMPLES\CH16\SEG2>

Page 25: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

COM and EXE etc

C:\>blah•Command.COM first checks a command line instruction to see if it is internal (like DIR).•After this, it looks for a matching exe file (blah.exe). •If this fails, it looks for a com file (blah.com)•Failing to find one it searches for a batch file. (blah.bat)•After this it searches directories in the path for the program.

Page 26: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Com vs exe

• A com is a binary file. It is loaded by ms-dos at the lowest available memory location and a PSP (program segment prefix) is created at offset 0. Code, data and stack are in the same physical & logical segment, which may be up to 64K minus the psp. COM programs use the tiny memory model.

• All the segment registers are set to the psp address. The stack area is allocated after the code and any data.

• You may not explicitly declare a stack segment

Page 27: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Com vs exe

• Com files must start at 100h and so require code:

Org 100h

• Org instruction tells the assembler what address to use. Using org, for example, you can “re-assemble” over previous assembly by org-ing back to a previous address.

Page 28: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Hellocom from textTITLE Hello Program in COM format (HelloCom.asm)

.model tiny

.codeorg 100h ; must be before mainmain PROC

mov ah,9mov dx,OFFSET hello_messageint 21hmov ax,4C00hint 21h

main ENDP

hello_message BYTE 'Hello, world!',0dh,0ah,'$'

END main

Page 29: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Creating .com instead of .exe

• Link with /t parameter to create a com file.

• there is also a program around named exe2bin which will convert exe to com

Page 30: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Assembling & running a com fileC:\MASM615>masm hellocomMicrosoft (R) MASM Compatibility DriverCopyright (C) Microsoft Corp 1993. All rights reserved.

Invoking: ML.EXE /I. /Zm /c hellocom.asm

Microsoft (R) Macro Assembler Version 6.15.8803Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

Assembling: hellocom.asm

C:\MASM615>link /t hellocom

Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994Copyright (C) Microsoft Corp 1984-1993. All rights reserved.

Run File [hellocom.com]:List File [nul.map]:Libraries [.lib]:Definitions File [nul.def]:LINK : warning L4045: name of output file is 'hellocom.com'

C:\MASM615>hellocomHello, world!

Page 31: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

interrupts

• You can customize interrupt handling for DOS and BIOS by installing you own handlers

• The hardware was designed with this possibility of tinkering, or customizing.

• You can replace any service routine with your own.

Page 32: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Interrupt handlers

• You might want your own program to handle a special key-press

• A default handler could be replaced by one with more service. For example, the clock tick handler which does nothing, could update a digital or analog clock display on the screen.

• Buffering could be provided for the pc’s asynchronous communication handler (int 14h)

Page 33: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

The vector table: pg 598

• The vector table is in the first K of RAM, locations 0:0 to 0:03FF

• The entries in the table, called “interrupt vectors” are 32 bit seg:ofs values that point to existing service routines.

• The specific values vary from one computer to another due to different BIOS or DOS versions

Page 34: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

The vector table

• The interrupt vectors correspond to interrupt numbers

• The address of int 0 handler (div by 0) is 02C1:5186h.

• The offset for a vector can be found by multiplying the interrupt number by 4 (shl 2)

Page 35: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

interrupts

• You can execute an interrupt by performing an

int XXX

• This is called a software interrupt

• A hardware interrupt may also call the handler (timer, ports, keyboard and so on all send signals to the PIC).

Page 36: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

interrupts

• A hardware interrupt is generated by the Intel 8259 PIC which signals the cpu to suspend execution of the current program and execute an interrupt service handler.

• Note that the current program stack is used to save the return address.

• The instructions CLI clear interrupt flag (disable) and STI (enable) set interrupt flag enable a program to temporarily disable/re-enable interrupts if sensitive operations are underway, like changing the value of a segment register.

Page 37: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

IRQ

• Interrupts have specific request levels, given in text table pg 599.

• A low-level interrupt can’t break into a high level one.

Page 38: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

A custom handler: ctrl breakTITLE Control-Break Handler (Ctrlbrk.asm)

; This program installs its own Ctrl-Break handler and; prevents the user from using Ctrl-Break (or Ctrl-C); to halt the program. The program inputs and echoes; keystrokes until the Esc key is pressed.; Last update: 2/1/02

INCLUDE Irvine16.inc

.databreakMsg BYTE "BREAK",0msg BYTE "Ctrl-Break demonstration."

BYTE 0dh,0ahBYTE "This program disables Ctrl-Break (Ctrl-C). Press any"BYTE 0dh,0ahBYTE "keys to continue, or press ESC to end the program."BYTE 0dh,0ah,0

Page 39: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

ctrl break continued.codemain PROC

mov ax,@datamov ds,axmov dx,OFFSET msg ; display greeting messagecall Writestring

install_handler:push ds ; save DSmov ax,@code ; initialize DSmov ds,axmov ah,25h ; set interrupt vectormov al,23h ; for interrupt 23hmov dx,OFFSET break_handlerint 21hpop ds ; restore DS

L1: mov ah,1 ; wait for a key, echo itint 21hcmp al,1Bh ; ESC pressed?jnz L1 ; no: continue

exitmain ENDP; The following procedure executes when; Ctrl-Break (Ctrl-C) is pressed.

break_handler PROCpush axpush dxmov dx,OFFSET breakMsgcall Writestringpop dxpop axiret

break_handler ENDPEND main

Page 40: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Tsr programs

• Terminate-stay-resident

Page 41: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

Text exampleTITLE Reset-Disabling program (No_Reset.asm)

; This program disables the usual DOS reset command; (Ctrl-Alt-Del), by intercepting the INT 9 keyboard; hardware interrupt. It checks the shift status bits; in the MS-DOS keyboard flag and changes any Ctrl-Alt-Del; to Alt-Del. The computer can only be rebooted by; typing Ctrl+Alt+Right shift+Del. Assemble, link,; and convert to a COM program by including the /T; command on the Microsoft LINK command line.; Last update: 10/12/01

.model tiny

.codert_shift EQU 01h ; Right shift key: bit 0ctrl_key EQU 04h ; CTRL key: bit 2alt_key EQU 08h ; ALT key: bit 3del_key EQU 53h ; scan code for DEL keykybd_port EQU 60h ; keyboard input port

ORG 100h ; this is a COM programstart:

jmp setup ; jump to TSR installation

Page 42: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

continued

; Memory-resident code begins hereint9_handler PROC FAR

sti ; enable hardware interruptspushf ; save regs & flagspush espush axpush di

; Point ES:DI to the DOS keyboard flag byte:L1: mov ax,40h ; DOS data segment is at 40h

mov es,axmov di,17h ; location of keyboard flagmov ah,es:[di] ; copy keyboard flag into AH

; Test for the CTRL and ALT keys:L2: test ah,ctrl_key ; CTRL key held down?

jz L5 ; no: exittest ah,alt_key ; ALT key held down?jz L5 ; no: exit

Page 43: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

More…; Test for the DEL and Right-shift keys:L3: in al,kybd_port ; read keyboard port

cmp al,del_key ; DEL key pressed?jne L5 ; no: exittest ah,rt_shift ; right shift key pressed?jnz L5 ; yes: allow system reset

L4: and ah,NOT ctrl_key ; no: turn off bit for CTRLmov es:[di],ah ; store keyboard_flag

L5: pop di ; restore regs & flagspop axpop espopfjmp cs:[old_interrupt9] ; jump to INT 9 routine

old_interrupt9 DWORD ?

int9_handler ENDPend_ISR label BYTE

Page 44: Chapter16. A program with no include files TITLE Simple (Simple.asm).model small,stdcall.stack 100h ;;256 bytes of stack space for this or other prgrams.

And a bit more…; --------------- (end of TSR program) ------------------; Save a copy of the original INT 9 vector, and set up; the address of our program as the new vector. Terminate; this program and leave the int9_handler procedure in memory.

setup:mov ax,3509h ; get INT 9 vectorint 21hmov word ptr old_interrupt9,bx ; save INT 9 vectormov word ptr old_interrupt9+2,es

mov ax,2509h ; set interrupt vector, INT 9mov dx,offset int9_handlerint 21h

mov ax,3100h ; terminate and stay residentmov dx,OFFSET end_ISR ; point to end of resident codeshr dx,4 ; multiply by 16inc dx ; round upward to next paragraphint 21h ; execute MS-DOS function

END start