msdos 16-bit programming

43
MSDOS 16-bit programming Old chpt 13 Current chapt 14

Upload: hasad

Post on 25-Feb-2016

61 views

Category:

Documents


4 download

DESCRIPTION

MSDOS 16-bit programming. Old chpt 13 Current chapt 14. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MSDOS 16-bit programming

MSDOS 16-bit programming

Old chpt 13Current chapt 14

Page 2: MSDOS 16-bit programming

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 3: MSDOS 16-bit programming

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 4: MSDOS 16-bit programming

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 5: MSDOS 16-bit programming

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 6: MSDOS 16-bit programming

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 7: MSDOS 16-bit programming

DOS ints

• Text describes a few DOS interrupts• I have a bunch of DOS and ROM-BIOS

referenced here

Page 8: MSDOS 16-bit programming

helloworldTITLE Hello World Program (Hello.asm)

; This program displays "Hello, world!"

.model small

.stack 100h

.386

.datamessage BYTE "Hello, world!",0dh,0ah

.codemain PROC mov ax,@data mov ds,ax

mov ah,40h ; write to file/device mov bx,1 ; output handle mov cx,SIZEOF message ; number of bytes mov dx,OFFSET message ; addr of buffer int 21h

.exitmain ENDPEND main

Page 9: MSDOS 16-bit programming

hello.asm

• C:\MASM615\EXAMPLES\CH13>hello• Hello, world!

• C:\MASM615\EXAMPLES\CH13>

Page 10: MSDOS 16-bit programming

Hello v2 uses startup to initialize DS

TITLE Hello World Program (Hello2.asm)

; This program displays "Hello, world!".model small.stack 100h.386.datamessage BYTE "Hello, world!",0dh,0ah.codemain PROC .STARTUP

mov ah,40h ; write to file/device mov bx,1 ; output handle mov cx,SIZEOF message ; number of bytes mov dx,OFFSET message ; addr of buffer int 21h

.exitmain ENDPEND main

Page 11: MSDOS 16-bit programming

Encrypt.asm: file encryption using XOR (char,value) and file redirect

• Int 21h function 6 reads chars without pausing or filtering control chars. This is good (only) if we are redirecting input

• (using DOS redirect > and <)• Text example arbitrarily uses 239 as the

value to XOR each char with. • Note that a 2nd XOR with the same value

will toggle back to the original value

Page 12: MSDOS 16-bit programming

Encrypt.asm: file encryption using XOR (char,value) and file redirect

• This is the same idea we encountered in an earlier chapter, but it combines xor encryption with file redirection. To run you might type:

C:\MASM615\EXAMPLES\CH13>encrypt<secret.txt>message.txt

Page 13: MSDOS 16-bit programming

Encrypt.asmTITLE Encryption Program (Encrypt.asm);; read and encrypt a file. Run it from the; command prompt, using redirection:; Encrypt < infile.txt > outfile.txt; Function 6 is also used for output, to avoid; filtering ASCII control characters.INCLUDE Irvine16.incXORVAL = 239 ; any value between 0-255.codemain PROC

mov ax,@datamov ds,ax

L1: mov ah,6 ; direct console inputmov dl,0FFh ; don't wait for characterint 21h ; AL = characterjz L2 ; quit if ZF = 1 (EOF)xor al,XORVALmov ah,6 ; write to outputmov dl,alint 21hjmp L1 ; repeat the loop

L2: exitmain ENDPEND main

Page 14: MSDOS 16-bit programming

uses dos redirection (< and >)C:\MASM615\EXAMPLES\CH13>encrypt<secret.txt>message.txt

C:\MASM615\EXAMPLES\CH13>

• secret.txtsecret message contained here• message.txt:œŠŒŠ›Ï‚ŠœœŽˆŠÏâ匀›Ž†Š‹Ï‡ŠŠ� � � �

Page 15: MSDOS 16-bit programming

Made some changes to this program

TITLE Buffered Keyboard Input (Keybd.asm); Test function 3Fh, read from file or device; with the keyboard. Flush the buffer.INCLUDE Irvine16.inc.datafirstName BYTE 15 DUP(?)terminal1 byte 0lastName BYTE 30 DUP(?)terminal2 byte 0.codemain PROC mov ax,@data mov ds,ax

Page 16: MSDOS 16-bit programming

Keybd continued; Input the first name:

mov ah,3Fhmov bx,0 ; keyboardmov cx,LENGTHOF firstNamemov dx,OFFSET firstNameint 21h

; Disable the following line to see what happens; when the buffer is not flushed:

call FlushBuffer; Input the last name:

mov ah,3Fhmov bx,0 ; keyboardmov cx,LENGTHOF lastNamemov dx,OFFSET lastNameint 21h

quit: call Crlfmov dx,offset firstnamecall writeStringcall Crlfmov dx,offset lastnamecall writeStringcall Crlf

Page 17: MSDOS 16-bit programming

Flush type-ahead buffer to eoln

;------------------------------------------FlushBuffer PROC;; Flush the standard input buffer.; Receives: nothing. Returns: nothing;-----------------------------------------.dataoneByte BYTE ?.code

pushaL1: mov ah,3Fh ; read file/device

mov bx,0 ; keyboard handle mov cx,1 ; one byte mov dx,OFFSET oneByte ; save it here int 21h ; call MS-DOS cmp oneByte,0Ah ; end of line yet? jne L1 ; no: read another

poparet

FlushBuffer ENDPEND main

Page 18: MSDOS 16-bit programming

Run keybd

C:\MASM615>keybdfirst has a max of 15 charslast has a max of

30xxxxxxxxxxxxxxxxxxxxxxxxfirst has a maxlast has a max of 30xxxxxxxxxx

Page 19: MSDOS 16-bit programming

read from file or device

C:\MASM615\EXAMPLES\CH13>keybd1234567890234567890123456789022344545566778899

C:\MASM615\EXAMPLES\CH13>

Page 20: MSDOS 16-bit programming

DateTime.asmInclude Irvine16.incWrite PROTO char:BYTE.datastr1 BYTE "Date: ",0str2 BYTE ", Time: ",0.codemain PROC

mov ax,@datamov ds,ax

; Display the date:mov dx,OFFSET str1call WriteStringmov ah,2Ah ; get system dateint 21hmovzx eax,dh ; monthcall WriteDecINVOKE Write,'-'movzx eax,dl ; daycall WriteDecINVOKE Write,'-'movzx eax,cx ; yearcall WriteDec

; Display the time:mov dx,OFFSET str2call WriteStringmov ah,2Ch ; get system timeint 21hmovzx eax,ch ; hourscall WritePaddedDecINVOKE Write,':'movzx eax,cl ; minutescall WritePaddedDecINVOKE Write,':'movzx eax,dh ; secondscall WritePaddedDeccall Crlf

exitmain ENDP

Page 21: MSDOS 16-bit programming

DateTime.asm;---------------------------------------------Write PROC char:BYTE; Display a single character.;---------------------------------------------

push eaxpush edxmov ah,2mov dl,charint 21hpop edxpop eaxret

Write ENDP;---------------------------------------------WritePaddedDec PROC; Display unsigned integer in EAX, padding; to two digit positions with a leading zero.;---------------------------------------------

.IF eax < 10 push eax push edx mov ah,2 mov dl,'0' int 21h pop edx pop eax.ENDIF

call WriteDecret

WritePaddedDec ENDPEND main

Page 22: MSDOS 16-bit programming

DateTime.asm

C:\MASM615\EXAMPLES\CH13>datetimeDate: 11-28-2005, Time: 17:55:19

C:\MASM615\EXAMPLES\CH13>

Page 23: MSDOS 16-bit programming

What is a binary file?

• If you write chars (or – in java- strings) to a file, it contains numerical values 16 (or 32) bits per char depending if it is ASCII or Unicode.

• If you write numbers to a file – say dword values- it contains 4 bytes per value.

• This latter could be said to be a binary file.

Page 24: MSDOS 16-bit programming

binfile.asmTITLE Binary File Program (Binfile.asm); Create a binary file containing an array; of doublewords.; Last update: 11/12/01INCLUDE Irvine16.inc.datamyArray DWORD 50 DUP(?)fileName BYTE "binary array file.bin",0fileName2 BYTE "file2.bin",0fileHandle WORD ?commaStr BYTE ", ",0; Set CreateFile to zero if you just want to; read and display the existing binary file.CreateFile = 1.codemain PROC mov ax,@data mov ds,ax.IF CreateFile EQ 1

call FillTheArraycall DisplayTheArraycall CreateTheFilecall WaitMsgcall Crlf

.ENDIFcall ReadTheFilecall DisplayTheArray

quit:call Crlf

exitmain ENDP

Page 25: MSDOS 16-bit programming

binfile.asm;------------------------------------------------------ReadTheFile PROC;; Open and read the binary file.; Receives: nothing. Returns: nothing;------------------------------------------------------

mov ax,716Ch ; extended file openmov bx,0 ; mode: read-onlymov cx,0 ; attribute: normalmov dx,1 ; open existing filemov si,OFFSET fileName ; filenameint 21h ; call MS-DOSjc quit ; quit if errormov fileHandle,ax ; save handle

; Read the input file, then close the file.mov ah,3Fh ; read filemov bx,fileHandle ; file handlemov cx,SIZEOF myArray ; max bytes to readmov dx,OFFSET myArray ; buffer pointerint 21hjc quit ; quit if errormov ah,3Eh ; function: close filemov bx,fileHandle ; output file handleint 21h ; call MS-DOS

quit:ret

ReadTheFile ENDP

Page 26: MSDOS 16-bit programming

binfile.asm;------------------------------------------------------DisplayTheArray PROC;; Display the array; Receives: nothing. Returns: nothing;------------------------------------------------------

mov CX,LENGTHOF myArraymov si,0

L1:mov eax,myArray[si] ; get a numbercall WriteHex ; display the numbermov edx,OFFSET commaStr ; display a commacall WriteStringadd si,TYPE myArray ; next array positionloop L1ret

DisplayTheArray ENDP

;------------------------------------------------------FillTheArray PROC;; Fill the array with random integers.; Receives: nothing. Returns: nothing;------------------------------------------------------

mov CX,LENGTHOF myArraymov si,0

L1:mov eax,1000h; generate random integerscall RandomRange ; between 0 - 999 in EAXmov myArray[si],eax ; store in the arrayadd si,TYPE myArray ; next array positionloop L1ret

FillTheArray ENDP

Page 27: MSDOS 16-bit programming

binfile.asm;------------------------------------------------------CreateTheFile PROC;; Create a file containing binary data; Receives: nothing. Returns: nothing;------------------------------------------------------

mov ax,716Ch ; create filemov bx,1 ; mode: write onlymov cx,0 ; normal filemov dx,12h ; action: create/truncatemov si,OFFSET fileName2 ; filenameint 21h ; call MS-DOSjc quit ; quit if errormov fileHandle,ax ; save handle

; Write integer array to the file.mov ah,40h ; write file or devicemov bx,fileHandle ; output file handlemov cx,SIZEOF myArray ; number of bytesmov dx,OFFSET myArray ; buffer pointerint 21hjc quit ; quit if error

; Close the file.mov ah,3Eh ; function: close filemov bx,fileHandle ; output file handleint 21h ; call MS-DOS

quit:ret

CreateTheFile ENDP

END main

Page 28: MSDOS 16-bit programming

binfile.asmC:\MASM615\EXAMPLES\CH13>binfile000009E2, 000003F6, 00000E87, 00000471, 000001DF, 00000C10, 0000060A, 00000E78,00000219, 00000072, 000009B4, 00000109, 000001B4, 00000BB0, 000009C9, 00000B59,00000315, 0000069E, 00000BCE, 00000CDB, 000007DF, 00000C51, 00000E86, 00000944,000004F6, 00000E1C, 00000DF5, 00000C86, 0000067E, 00000793, 0000075F, 00000ED7,000003DB, 0000028B, 00000D49, 000008AA, 000003B2, 00000B16, 00000B76, 000006B4,00000FE6, 0000038A, 00000AEA, 00000DE7, 0000099B, 0000087A, 000005E9, 00000F79,00000D36, 000004BB, Press any key to continue...

000009E2, 000003F6, 00000E87, 00000471, 000001DF, 00000C10, 0000060A, 00000E78,00000219, 00000072, 000009B4, 00000109, 000001B4, 00000BB0, 000009C9, 00000B59,00000315, 0000069E, 00000BCE, 00000CDB, 000007DF, 00000C51, 00000E86, 00000944,000004F6, 00000E1C, 00000DF5, 00000C86, 0000067E, 00000793, 0000075F, 00000ED7,000003DB, 0000028B, 00000D49, 000008AA, 000003B2, 00000B16, 00000B76, 000006B4,00000FE6, 0000038A, 00000AEA, 00000DE7, 0000099B, 0000087A, 000005E9, 00000F79,00000D36, 000004BB,

C:\MASM615\EXAMPLES\CH13>

Page 29: MSDOS 16-bit programming

fileio.asmTITLE Extended Open/Create (Fileio.asm)

; Demonstration of 16-bit FileIO under Windows 95/98/ME.; The complete program does not appear in the text, but; excerpts do appear.; Last update: 11/12/01

INCLUDE Irvine16.inc

.dataDate WORD ?handle WORD ?actionTaken WORD ?FileName BYTE "long_filename.txt",0NewFile BYTE "newfile.txt",0

.code

Page 30: MSDOS 16-bit programming

fileio.asmmain PROC

mov ax,@datamov ds,ax

;Create new file, fail if it already exists:mov ax,716Ch ; Extended Open/Createmov bx,2 ; read-writemov cx,0 ; normal attributemov dx,10h ; action: createmov si,OFFSET NewFileint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file

mov ax,cx call writeint call crlf;Open existing file

mov ax,716Ch ; Extended Open/Createmov bx,0 ; read-onlymov cx,0 ; normal attributemov dx,1 ; open existing filemov si,OFFSET Filenameint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file

mov ax,cx call writeint call crlf

Page 31: MSDOS 16-bit programming

fileio.asm;Create new file or truncate existing file:

mov ax,716Ch ; Extended Open/Createmov bx,2 ; read-writemov cx,0 ; normal attributemov dx,10h + 02h ; action: create + truncatemov si,OFFSET NewFileint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file

mov ax,cx call writeint call crlffailed:

exitmain ENDPEND main

Page 32: MSDOS 16-bit programming

fileio.asm

• C:\MASM615\EXAMPLES\CH13>fileio• +2• +1• +3

Page 33: MSDOS 16-bit programming

readfileTITLE Read a text file (Readfile.asm)

; Read, display, and copy a text file.; Last update: 9/11/01

INCLUDE Irvine16.inc

.dataBufSize = 5000infile BYTE "my_text_file.txt",0outfile BYTE "my_output_file.txt",0inHandle WORD ?outHandle WORD ?buffer BYTE BufSize DUP(?)bytesRead WORD ?

.codemain PROC mov ax,@data mov ds,ax

Page 34: MSDOS 16-bit programming

readfile; Open the input file

mov ax,716Ch ; extended create or openmov bx,0 ; mode = read-onlymov cx,0 ; normal attributemov dx,1 ; action: openmov si,OFFSET infileint 21h ; call MS-DOSjc quit ; quit if errormov inHandle,ax

; Read the input filemov ah,3Fh ; read file or devicemov bx,inHandle ; file handlemov cx,BufSize ; max bytes to readmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if errormov bytesRead,ax

; Display the buffermov ah,40h ; write file or devicemov bx,1 ; console output handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error

; Close the filemov ah,3Eh ; function: close filemov bx,inHandle ; input file handleint 21h ; call MS-DOSjc quit ; quit if error

Page 35: MSDOS 16-bit programming

readfile; Create the output file

mov ax,716Ch ; extended create or openmov bx,1 ; mode = write-onlymov cx,0 ; normal attributemov dx,12h ; action: create/truncatemov si,OFFSET outfileint 21h ; call MS-DOSjc quit ; quit if errormov outHandle,ax ; save handle

; Write buffer to new filemov ah,40h ; write file or devicemov bx,outHandle ; output file handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error

; Close the filemov ah,3Eh ; function: close filemov bx,outHandle ; output file handleint 21h ; call MS-DOS

quit:call Crlf

exitmain ENDPEND main

Page 36: MSDOS 16-bit programming

Run of readfileC:\MASM615>readfilehere is a sampledatafilewith severallines in it 345 6778122212224445

Page 37: MSDOS 16-bit programming

Findfirst…findnext: dir

Page 38: MSDOS 16-bit programming

Findfirst…findnext: dir.datafilename byte '*.asm',0DTA label bytesearch byte 21 dup(?)other byte 9 dup(?)fname byte 'namehereXXXX',0.codemain PROC mov ax,@data mov ds,ax ; must point ds to main mov es,ax mov dx, offset DTA mov ah, 1Ah int 21h ;;;set DTA to my buffer area mov cx, 1 ;;; file attribute read-only mov dx,offset filename mov ah, 4Eh int 21h xor cx,cx top: mov dx,offset fname inc cx test cx,3h jnz writeit call crlf writeit: call writestring blanks 5 mov ah,4fh int 21h jnc top quit: exit

Page 39: MSDOS 16-bit programming

Tabbing for the dir command: two macros blanks macro count local top push cx push ax push dx mov cx, count mov dl,' ' mov ah, 2top: int 21h loop top pop dx pop ax pop cx endm mygotoxy macro row, col push dx push bx push ax mov dh,row mov dl,col mov bh,0 mov ah,2 int 10h pop ax pop bx pop dx endm

Page 40: MSDOS 16-bit programming

Tabbing for the dir command

Page 41: MSDOS 16-bit programming

Tabbing data area: I made up some strings

INCLUDE Irvine16.inc.datarow byte 0col byte 0s1 byte 'some string',0s2 byte 'and another',0s3 byte 'shorty',0s4 byte 'xyz',0s5 byte 'bleh blah',0addresses label word word s1entry=($-addresses) word s2 word s3 word s4 word s5numentries=($-addresses)/entry

Page 42: MSDOS 16-bit programming

tabbing call clrscr mov row,0 mov col,0 mov bx, 0 nextword: mygotoxy row,col ;start in upper lh corner mov dx,addresses[bx] mov di,dx call writestring add col, 20 add bx,2 cmp bx,numentries*2 jb checkrow xor bx,bx checkrow: cmp col,65 jb nextword mov col,0 inc row cmp row,24 jb nextword quit: exitmain ENDP

Page 43: MSDOS 16-bit programming