1 / 50

Chapter 13: 16- Bit MS-DOS Programming

Chapter 13: 16- Bit MS-DOS Programming. Assembly Language for Intel-Based Computers , Kip R. Irvine 4th edition. 3/17/2000 Last revision 4/11/05. Chapter 13: 16-Bit MS-DOS Programming.

ikia
Download Presentation

Chapter 13: 16- Bit MS-DOS Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13: 16- Bit MS-DOS Programming Assembly Language for Intel-Based Computers, Kip R. Irvine 4th edition 3/17/2000 Last revision 4/11/05

  2. Chapter 13: 16-Bit MS-DOS Programming • 13.1 MS-DOS and the IBM-PC History Use of the segment registers Redirecting output Memory organization Software (and hardware) interrupts • 13.2 MS-DOS Function calls (INT 21h) • 13.3 Standard MS-DOS File I/O services

  3. 13.1 History • The Intel 8088 and 8086 were some of the first microprocessors with 16 bit registers. • The only significant difference: 8086 used a 16 bit memory bus - faster memory access 8088 used a 8 bit memory bus - cheaper memory system • They could address 1 MByte of memory. Previous chips typically only could use 64KByte of memory • 80386 has the first 80x86 with a 32 bit bus

  4. 0 Memory address 05ADCh 13.1 Segment registers • While the 8088 and 8086 16 bit registers, they used 20 bit addresses • How? • Segment registers • Example • CS (code segment) 0123 h • Instruction offset 48ACh

  5. 13.1 Software InterruptsRedirection of I/O • DOS permits UNIX style redirection of the standard I/O devices. MyProg < COM1 > PTRThis means get the input from COM1, send the output to PTR. • We can use files as the standard input or output devices. MyProg < Input.txt > Output.txt

  6. 13.1 Software InterruptsStandard DOS Device Names • The standard input and output device is CON, the console which consists of the keyboard and monitor. DOS can be instructed to redirect I/O from the standard input and output devices to other units.

  7. 13.1 Software InterruptsStandard DOS Device Names • Device NameDescription • CON console keyboard and monitor • LPT1 or PTR 1st parallel printer • LPT2 2nd parallel printer • AUX or COM1 1st serial port • COM2, COM3, COM4 2nd – 4th serial port • NUL Dummy device Redirection may not work properly with some 32 bit Windows programs.

  8. 13.1 Memory organization • CS - Points to code segment • SS - Points to stack segment • DS - Points to data segment • ES - Points to extra segment • Each segment could have 64K of memory • ES could be used to supplement DS so 128 KBytes of data memory could be accessed at the same • If programs used more than 64K of code memory, the CS registers had to be changed to allow access. If program used more than 128 K of data memory, either DS or ES had to be modified FS and GS first appeared in the 386

  9. Your Turn • Suppose that CS=1298h, DS = 8235h, ES = 03463h, and SS = 92A3h. In a 16-bit program, the instruction inc axis at offset 0E51. What is the actual memory address for this instruction?

  10. 13.1 Memory organization • The original IBM-PC had 1 MB of memory space • The hardware and operating system specified how that memory space was organized • In theory, the PC could have run in only 64K of program RAM. Fortunately 256K was typically the smallest people bought.

  11. ROM BIOSReservedVideo text and GraphicsVideo GraphicsTransient Command processorApplication ProgramsResident command processorDOS Kernel, Device driversSoftware BIOSBIOS and DataInterrupt vector table 13.1MS-DOSMemoryMap • FFFFFF0000C0000B8000A00000040000000

  12. 13.1A InterruptsInterrupts: • Hardware interrupt: in response to a request by a hardware device that needs attention. Hardware interrupts occur at "unexpected" times. • Software interrupt: A call to DOS or BIOS in response to a interrupt instruction (INT) in the program being processed. • Exception: An automatically generated trap in response to an exceptional condition such as division by zero.

  13. 13.1A InterruptsHardware Interrupts • Device Program Interrupt table Interrupt Code • Stack 0 4 4 3 3 1 1 1 2 2 5 5 7 6 6

  14. 13.1A InterruptsHardware Interrupt Steps • 0. The program is executing in the CPU • 1. The hardware device needs attention and requests an interrupt. • 2. The CPU finishes processing the current instruction. It the saves its "state" (flags and CS:IP) on the stack. • 3. The address of the interrupt handler is obtained from the Interrupt Vector Table (located in the first 400h bytes of memory).

  15. 13.1A InterruptsHardware Interrupt Steps • 4. The CPU loads the address of the interrupt handler into the IP. • 5. The interrupt handler code is processed. • 6. When the interrupt handler is finished, the CPU pops the "state" (CS:IP and Flags) back from the stack. • 7. Execution of original program continues beginning at the next instruction.

  16. 13.1A InterruptsHardware Interrupt Comments • Some interrupt operations are so critical that the interrupt driver may disable other interrupts until the critical operation is completed. • In IBM terminology, hardware interrupts are known as an IRQ. Each device is assigned a number which determines the Interrupt Vector entry of the appropriate interrupt handler.

  17. 13.1 Software InterruptsSoftware Interrupt • Software interrupt. Software interrupts are something like a procedure call except the name of the procedure is not known to the caller. • Works much the same way as hardware interrupts but steps 1 and 2 are replaced by an INT xxinstruction in the code. The number xx determines the table entry specifying location of the interrupt handler desired.

  18. 13.1 Software InterruptsSoftware Interrupt • In many respects interrupts are like procedure calls. • Difference - In procedures, the address of the procedure is specified in the program. In interrupts, the address is specified in the interrupt vector table. • In IBM design, the interrupt vector table is stored in the lowest 1K of memory. Each entry is a 4 byte segment/offset address. (That allows 100h = 256d entries.)

  19. Interrupt Table • Stored in bytes numbered 0-1023 • 256 four byte entries • Entries are the segment/offset address of the interrupt handler (the code used to process that interrupt)

  20. Interrupts See Appendix C • 0 , 2, 4, 6, 7 – Processor and memory errors • 1, 3 – used by debuggers • 5 – print screen • 8-0Fh – Hardware interrupts (IRQ0 – IRQ7) • 10h-20h – Various BIOS interrupts • 20h – 33h, 3F-7F – MS-DOS • 34-3E – Floating point emulation • 80-F0 – Reserved for ROM BASIC • F1-FF – Available for application programs

  21. 13.1 Software InterruptsCommon Software Interrupts • INT 10h Video services (Chap 15.3) • INT 16h Keyboard services (Chap 15.2) • INT 17h Printer services (Chap 13.1.4.2) • INT 1Ah Time of day (Chap 13.1.4.2) • INT 1Ch User Timer Interrupt (Chap 13.1.4.2) • INT 21h DOS services - "DOS function calls" • Most interrupts have multiple functions and use AH to specify the desired operation (function)

  22. Your Turn • Interrupt 21h is a common interrupt. Where is the address of the interrupt handler stored?

  23. Programs Languages DOS – operating system BIOS 13.1B Layers of I/OI/O Model Normally restricted in multitasking machines Hardware

  24. 13.1B Layers of I/OI/O Model • DOS: Redirectable, slow, device independent, easiest to use, treats I/O in a generic fashion • BIOS: Not redirectable, faster, device independent, provides much more control, understands video output • Hardware: Not redirectable, fastest, device dependent, hardest to use. Inappropriate use can cause serious problems.

  25. Or include Irvine16.inc 13.2 MS-DOS Function Calls (int 21H)Simple 16 bit program • TITLE Simple 16 bit program Simple16.asm; Uses interrupts instead of IRVIN I/O methods; Prints Hello World.model small.stack 200h.8086 ; restrict code to .8086 code.dataoutBuffer BYTE "Hello World!", 13, 10, "$"

  26. Compiling and linking 16 bit programs • A primative 16-bit linker is included in ml • The linker does not understand • Long file names • Names of paths or files with blanks in them • Use C:\MASM615\ML filename.asmto assemble and link. Add –Fl before file name if you want a listing • In MultiEd use “Assemble 16-bit program” button. It uses mlbat615.bat to assemble and link

  27. Uses function 9h of interrupt 21h Simple 16 bit program(con’t) .codemain PROC; set up data segment mov ax, @data mov ds, ax; print the string "Hello" mov dx, OFFSET outBuffer mov ah, 9h int 21h; terminate exit mov ax, 4C00h int 21h main ENDPEND main

  28. 13.2 MS-DOS Function Calls (int 21H)Function 02h: character output • Function 2h: Single character output • DL: Character to be printed • (Caution) AX: May be modified • mov DL, aChar; character to be printed mov AH, 2h ; output characterint 21h

  29. 13.2 MS-DOS Function Calls (int 21H)Function 06h: Direct Output • Can do both input and output single characters • For output DL holds character to be printed • mov DL, 'B'mov AH, 6h ; print characterint 21h

  30. 13.2 MS-DOS Function Calls (int 21H)Function 09h: String output • prints "$" terminated string • DX holds offset of string to be printed • aString BYTE "String", 0Dh, 0Ah, "$"...mov dx, OFFSET aStringmov ah, 09hint 21h

  31. 13.2 MS-DOS Function Calls (int 21H)Function 40h: Write to a File or Device • Designed to write to a file • Put 0 in BX to specify "standard output" • Output cannot be redirected successfully and tends to get lost if input is redirected

  32. 13.2 MS-DOS Function Calls (int 21H)Function 40h: Write to a File or Device • hello BYTE "Hello "HELLO_LENGTH = $ - hello...mov dx, OFFSET hello ; and offset of hellomov cx, HELLO_LENGTH ; set lengthmov ah, 40h ; print to file or devicemov bx, 0 ; device = Std. outputint 21h ; print string

  33. 13.2 MS-DOS Function Calls (int 21H)Keyboard Input functions • INT 1 7 8 6 0AhWait Y Y Y N YFilters Y N N N YEcho Y N N N YCtrl-Break Y N Y N YStrings - - - - Y • Also see function 3Fh

  34. 13.2 MS-DOS Function Call (int 21H)Functions 1, 7, 8: Input characters • mov ah,n ; n = 01h, 07h, 08hint 21hmov aChar, al • Notes: • Character input is put in AL • All wait for a key press • Functions 7 and 8 do not echo • Function 7 does not recognize ctrl-break • Function 1 is closest to what we generally expect • ^G, ^H, ^I, ^J, ^M react as specified by ASCII, other control characters print special characters

  35. 13.2 MS-DOS Function Calls (int 21H)Function 06h: Input character/no wait • Sample application: Games where the game proceeds until user presses a key • mov ah, 06h ; Input char/no waitmov dl, 0FFhint 21hjz NoCharWaitingmov aChar, al • This function is also used for output

  36. 13.2 MS-DOS Function Calls (int 21H)Function 0Ah: Input string • The input buffer 0 1 2 … characters input number of characters input (excluding <CR>)max characters allowed (including <CR>)

  37. Your Turn • Your are writing a game program for DOS with lots of action. The user presses “<“ or “,” to move left, “>” or “.” to move right, “Q” or “q” to quit the game. Write a code segment that checks the keyboard and jumps to labels MoveLeft, MoveRight, and Quit if an appropriate key has been pressed or to Continue otherwise.

  38. 13.2 MS-DOS Function Calls (int 21H)Function 0Ah: Input string • Input up to 80 characters (plus <CR>)numChar = 81 KEYBOARD STRUCT maxInput BYTE numChar inputCount BYTE ? buffer BYTE numChar DUP (?)KEYBOARD ENDS • .datainput KEYBOARD <> • … mov ah, 0Ah ; string input mov dx, OFFSET input int 21h

  39. 13.2 MS-DOS Function Calls (int 21H)Function 3Fh: Read from a Fileor Device • Used to input a string • This function is designed to read from a disk file but can also read from the keyboard. • (Keyboard input) 0Ah like editing operations enabled. Reads until buffer full or until CR pressed. • Put 0 in BX to specify "standard input" • Returns count of characters input in AX including CR (0Dh) and LF (0Ah) • 0Dh and 0Ah are stored in the input buffer and included in the count • The input can be redirected from a file. (Reads until buffer full or end of file.)

  40. 13.2 MS-DOS Function Calls (int 21H)Function 3Fh: Read from a File or Device • aName BYTE 82 DUP(?); 80 char. plus CR & LFMAX_LENGTH = $ - aNamelenName WORD ? ; number of characters typed...mov ah, 3Fh; read from file or devicemov bx, 0 ; device = std. inputmov cx, MAX_LENGTH ; set lengthmov dx, OFFSET aName ; and offset of helloint 21h ; read stringsub ax, 2 ; if appropriate: find string lengthmov lenName, ax ; and save it

  41. 13.3 MS-DOS File I/O ServicesFile Processing • We have already looked int 21h functions for reading writing files • Function 3Fh: Read a file • Function 40h: Write a file • Files must also be opened and closed • MS-DOS and Windows keep track of files with a file handle

  42. 13.3 MS-DOS File I/O ServicesFile Handle • A file HANDLE is a 16 bit WORD that identifies a file. • Predeclared “file” numbers in 16 bit coding • 0 - Keyboard • 1 - Monitor • 2 - Error output • 3 - Auxiliary asynchronous device • 4 - Printer • Open files to get handles for other files

  43. 13.3 MS-DOS File I/O ServicesFunction 716C: Create or open a file • Used to create a new file or open existing file • mov ax, 716Chmov bx, 0 ; 0 = read onlymov cx, 0 ; 0 = normal file attributemov dx, 1 ; 1 = open an existing filemov si, OFFSET FileNameint 21h ; open file for readingjc failedmov handle, axmov actionTaken, cx • Notes:bx: access mode (0: read, 1: write, 2: read/write)cx: attributes(0: normal, 1: read only, 2: hidden, 3: system, 8 volume ID, 20h archive (add values)dx: action (1: open, 2: truncate, 10h: create This code opens a file for reading

  44. 13.3 MS-DOS File I/O ServicesFunction 3Eh: Close a file • mov ah, 3Ehmov bx, fileHandleint 21hjc failed • Note:For output files, it flushes the files write buffer by copying any remaining characters to the file • Failure to close an output file may cause the loss of characters at the end of the file

  45. 13.3 MS-DOS File I/O ServicesFunction 3Fh: Read from file • MAX_LENGTH = 5000 buffer BYTE MAX_LENGTH DUP(?)numChar WORD ? ; number of characters in file...mov ah, 3Fh; read to file or devicemov bx, fileHandle ; set filemov cx, MAX_LENGTH ; set lengthmov dx, OFFSET buffer ; and offset of bufferint 21h ; read filejc failed mov numChar, ax ; and save it

  46. 13.3 MS-DOS File I/O ServicesFunction 40h: Write file • BU_LENGTH = 10000buffer BYTE BUF_LENGTH DUP (?)charsInBuf WORD ? ; number of characters ...mov ah, 40h ; print to file or devicemov bx, fileHandle ; set file handlemov cx, charsInBuf ; set lengthmov dx, OFFSET buffer ; and offset of bufferint 21h ; write to filejc failed

  47. 13.3 MS-DOS File I/O ServicesError codes • Some of the error codes returned in AX include: • 02 – file not found03 – Path not found05 – Access denied06 – Invalid handle0C – Invalid access code0D – Invalid dataSee pages 477 – 478 for complete list

  48. 13.3 MS-DOS File I/O ServicesCommand tail • The command handle are characters typed after the exe’s file name • Example:myProg this is the tailthe command tail isthis is the the tail

  49. 13.3 MS-DOS File I/O ServicesCommand tail • .datatailBuffer BYTE 129 DUP (?).code …mov dx, OFFSET tailBuffercall GetCommandtail(no underline) • tailBuffer is terminated with a 0

More Related