Presentation is loading. Please wait.

Presentation is loading. Please wait.

MSDOS 16-bit programming

Similar presentations


Presentation on theme: "MSDOS 16-bit programming"— Presentation transcript:

1 MSDOS 16-bit programming
chpt 13

2 helloworld TITLE Hello World Program (Hello.asm)
; This program displays "Hello, world!" .model small .stack 100h .386 .data message BYTE "Hello, world!",0dh,0ah .code main PROC mov 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 .exit main ENDP END main

3 hello.asm C:\MASM615\EXAMPLES\CH13>hello Hello, world!

4 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

5 Encrypt.asm TITLE 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.inc XORVAL = 239 ; any value between 0-255 .code main PROC mov mov ds,ax L1: mov ah,6 ; direct console input mov dl,0FFh ; don't wait for character int 21h ; AL = character jz L2 ; quit if ZF = 1 (EOF) xor al,XORVAL mov ah,6 ; write to output mov dl,al int 21h jmp L1 ; repeat the loop L2: exit main ENDP END main

6 uses dos redirection (< and >)
C:\MASM615\EXAMPLES\CH13>encrypt<secret.txt>message.txt C:\MASM615\EXAMPLES\CH13> secret.txt secret message contained here message.txt: œŠŒŠ›Ï‚ŠœœŽˆŠÏâ匀›Ž†Š‹Ï‡ŠŠ

7 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 .data firstName BYTE 15 DUP(?) terminal1 byte 0 lastName BYTE 30 DUP(?) terminal2 byte 0 .code main PROC mov mov ds,ax

8 Keybd continued ; Input the first name: mov ah,3Fh mov bx,0 ; keyboard
mov cx,LENGTHOF firstName mov dx,OFFSET firstName int 21h ; Disable the following line to see what happens ; when the buffer is not flushed: call FlushBuffer ; Input the last name: mov cx,LENGTHOF lastName mov dx,OFFSET lastName quit: call Crlf mov dx,offset firstname call writeString call Crlf mov dx,offset lastname

9 Flush type-ahead buffer to eoln
; FlushBuffer PROC ; ; Flush the standard input buffer. ; Receives: nothing. Returns: nothing ; .data oneByte BYTE ? .code pusha L1: 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 popa ret FlushBuffer ENDP END main

10 Run keybd C:\MASM615>keybd first has a max of 15 chars
last has a max of 30xxxxxxxxxxxxxxxxxxxxxxxx first has a max last has a max of 30xxxxxxxxxx

11 read from file or device
C:\MASM615\EXAMPLES\CH13>keybd C:\MASM615\EXAMPLES\CH13>

12 DateTime.asm Include Irvine16.inc Write PROTO char:BYTE .data
str1 BYTE "Date: ",0 str2 BYTE ", Time: ",0 .code main PROC mov mov ds,ax ; Display the date: mov dx,OFFSET str1 call WriteString mov ah,2Ah ; get system date int 21h movzx eax,dh ; month call WriteDec INVOKE Write,'-' movzx eax,dl ; day movzx eax,cx ; year ; Display the time: mov dx,OFFSET str2 mov ah,2Ch ; get system time movzx eax,ch ; hours call WritePaddedDec INVOKE Write,':' movzx eax,cl ; minutes movzx eax,dh ; seconds call Crlf exit main ENDP

13 DateTime.asm ;---------------------------------------------
Write PROC char:BYTE ; Display a single character. push eax push edx mov ah,2 mov dl,char int 21h pop edx pop eax ret Write ENDP WritePaddedDec PROC ; Display unsigned integer in EAX, padding ; to two digit positions with a leading zero. .IF eax < 10 mov dl,'0' .ENDIF call WriteDec WritePaddedDec ENDP END main

14 DateTime.asm C:\MASM615\EXAMPLES\CH13>datetime

15 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.

16 binfile.asm TITLE Binary File Program (Binfile.asm)
; Create a binary file containing an array ; of doublewords. ; Last update: 11/12/01 INCLUDE Irvine16.inc .data myArray DWORD 50 DUP(?) fileName BYTE "binary array file.bin",0 fileName2 BYTE "file2.bin",0 fileHandle WORD ? commaStr BYTE ", ",0 ; Set CreateFile to zero if you just want to ; read and display the existing binary file. CreateFile = 1 .code main PROC mov mov ds,ax .IF CreateFile EQ 1 call FillTheArray call DisplayTheArray call CreateTheFile call WaitMsg call Crlf .ENDIF call ReadTheFile quit: exit main ENDP

17 binfile.asm ;------------------------------------------------------
ReadTheFile PROC ; ; Open and read the binary file. ; Receives: nothing. Returns: nothing mov ax,716Ch ; extended file open mov bx,0 ; mode: read-only mov cx, ; attribute: normal mov dx,1 ; open existing file mov si,OFFSET fileName ; filename int 21h ; call MS-DOS jc quit ; quit if error mov fileHandle,ax ; save handle ; Read the input file, then close the file. mov ah,3Fh ; read file mov bx,fileHandle ; file handle mov cx,SIZEOF myArray ; max bytes to read mov dx,OFFSET myArray ; buffer pointer int 21h mov ah,3Eh ; function: close file mov bx,fileHandle ; output file handle int 21h ; call MS-DOS quit: ret ReadTheFile ENDP

18 binfile.asm ;------------------------------------------------------
DisplayTheArray PROC ; ; Display the array ; Receives: nothing. Returns: nothing mov CX,LENGTHOF myArray mov si,0 L1: mov eax,myArray[si] ; get a number call WriteHex ; display the number mov edx,OFFSET commaStr ; display a comma call WriteString add si,TYPE myArray ; next array position loop L1 ret DisplayTheArray ENDP FillTheArray PROC ; Fill the array with random integers. mov eax,1000h ; generate random integers call RandomRange ; between in EAX mov myArray[si],eax ; store in the array FillTheArray ENDP

19 binfile.asm ;------------------------------------------------------
CreateTheFile PROC ; ; Create a file containing binary data ; Receives: nothing. Returns: nothing mov ax,716Ch ; create file mov bx,1 ; mode: write only mov cx, ; normal file mov dx,12h ; action: create/truncate mov si,OFFSET fileName2 ; filename int 21h ; call MS-DOS jc quit ; quit if error mov fileHandle,ax ; save handle ; Write integer array to the file. mov ah,40h ; write file or device mov bx,fileHandle ; output file handle mov cx,SIZEOF myArray ; number of bytes mov dx,OFFSET myArray ; buffer pointer int 21h ; Close the file. mov ah,3Eh ; function: close file mov bx,fileHandle ; output file handle int 21h ; call MS-DOS quit: ret CreateTheFile ENDP END main

20 binfile.asm C:\MASM615\EXAMPLES\CH13>binfile
000009E2, F6, 00000E87, , DF, 00000C10, A, 00000E78, , , B4, , B4, 00000BB0, C9, 00000B59, , E, 00000BCE, 00000CDB, DF, 00000C51, 00000E86, , 000004F6, 00000E1C, 00000DF5, 00000C86, E, , F, 00000ED7, 000003DB, B, 00000D49, AA, B2, 00000B16, 00000B76, B4, 00000FE6, A, 00000AEA, 00000DE7, B, A, E9, 00000F79, 00000D36, BB, Press any key to continue... 00000D36, BB, C:\MASM615\EXAMPLES\CH13>

21 fileio.asm TITLE 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 .data Date WORD ? handle WORD ? actionTaken WORD ? FileName BYTE "long_filename.txt",0 NewFile BYTE "newfile.txt",0 .code

22 fileio.asm main PROC mov ax,@data mov ds,ax
;Create new file, fail if it already exists: mov ax,716Ch ; Extended Open/Create mov bx,2 ; read-write mov cx, ; normal attribute mov dx,10h ; action: create mov si,OFFSET NewFile int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file mov ax,cx call writeint call crlf ;Open existing file mov bx,0 ; read-only mov dx,1 ; open existing file mov si,OFFSET Filename

23 fileio.asm ;Create new file or truncate existing file:
mov ax,716Ch ; Extended Open/Create mov bx,2 ; read-write mov cx, ; normal attribute mov dx,10h + 02h ; action: create + truncate mov si,OFFSET NewFile int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file mov ax,cx call writeint call crlf failed: exit main ENDP END main

24 fileio.asm C:\MASM615\EXAMPLES\CH13>fileio +2 +1 +3

25 readfile TITLE Read a text file (Readfile.asm)
; Read, display, and copy a text file. ; Last update: 9/11/01 INCLUDE Irvine16.inc .data BufSize = 5000 infile BYTE "my_text_file.txt",0 outfile BYTE "my_output_file.txt",0 inHandle WORD ? outHandle WORD ? buffer BYTE BufSize DUP(?) bytesRead WORD ? .code main PROC mov mov ds,ax

26 readfile ; Open the input file mov ax,716Ch ; extended create or open
mov bx, ; mode = read-only mov cx,0 ; normal attribute mov dx,1 ; action: open mov si,OFFSET infile int 21h ; call MS-DOS jc quit ; quit if error mov inHandle,ax ; Read the input file mov ah,3Fh ; read file or device mov bx,inHandle ; file handle mov cx,BufSize ; max bytes to read mov dx,OFFSET buffer ; buffer pointer int 21h mov bytesRead,ax ; Display the buffer mov ah,40h ; write file or device mov bx,1 ; console output handle mov cx,bytesRead ; number of bytes ; Close the file mov ah,3Eh ; function: close file mov bx,inHandle ; input file handle int 21h ; call MS-DOS

27 readfile ; Create the output file
mov ax,716Ch ; extended create or open mov bx, ; mode = write-only mov cx,0 ; normal attribute mov dx,12h ; action: create/truncate mov si,OFFSET outfile int 21h ; call MS-DOS jc quit ; quit if error mov outHandle,ax ; save handle ; Write buffer to new file mov ah,40h ; write file or device mov bx,outHandle ; output file handle mov cx,bytesRead ; number of bytes mov dx,OFFSET buffer ; buffer pointer int 21h ; Close the file mov ah,3Eh ; function: close file mov bx,outHandle ; output file handle int 21h ; call MS-DOS quit: call Crlf exit main ENDP END main

28 Run of readfile C:\MASM615>readfile here is a sample data file
with several lines in it 1222 4445

29 Findfirst…findnext: dir

30 Findfirst…findnext: dir
.data filename byte '*.asm',0 DTA label byte search byte 21 dup(?) other byte 9 dup(?) fname byte 'namehereXXXX',0 .code main PROC mov 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 jnc top quit: exit

31 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, 2 top: int 21h loop top pop dx pop ax pop cx endm mygotoxy macro row, col push bx mov dh,row mov dl,col mov bh,0 mov ah,2 int 10h pop bx

32 Tabbing for the dir command

33 Tabbing data area: I made up some strings
INCLUDE Irvine16.inc .data row byte 0 col byte 0 s1 byte 'some string',0 s2 byte 'and another',0 s3 byte 'shorty',0 s4 byte 'xyz',0 s5 byte 'bleh blah',0 addresses label word word s1 entry=($-addresses) word s2 word s3 word s4 word s5 numentries=($-addresses)/entry

34 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 inc row cmp row,24 quit: exit main ENDP


Download ppt "MSDOS 16-bit programming"

Similar presentations


Ads by Google