Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 8 Programming the Microprocessor 8.1 Modular Programming 8.1 Modular Programming • The linker program: • programming modules are linked together into a complete program. 8.1.1 The Assembler and Linker 8.1.1 The Assembler and Linker • The assembler program converts a symbolic source module (file) into a hexadecimal object file. • Example 8­1: • The assembler program (ML) requires the source file name following ML • The /Fl switch is used to create a listing file named NEW.LST. • The source listing file (.LST) contains − the assembled version of the source file − its hexadecimal machine language equivalent. • If a file is short enough (less than 64K) , it can be converted from an execution file to a command file (.COM) – – – Must be originated at location 100H before it can execute Must be no larger than 64K­100H in length. The ML program generates a command file if the tiny model is used with a starting address of 100H. • Example 8­2 shows the linker program protocol when it is used to link the files NEW, WHAT, and DONUT. – To involve the linker, type LINK at the DOS command prompt. – ML not only links the files, but it also assembles them prior to linking. – Note that before files are linked, they must first be assembled and they must be error­free. 8.1.2 PUBLIC and EXTRN 8.1.2 PUBLIC and EXTRN • The PUBLIC and EXTRN directives are very important to modular programming. • PUBLIC – to declare that code labels , data labels , or segments name are available to other program modules. • EXTRN (external) declares that labels are external to a module. – The EXTRN statement appears in both data and code segments to define labels as external to the segment. – If data are defined as external, their sizes must be defined as BYTE, WORD, or DWORD. – If a jump or call address is external, it must be defined as NEAR or FAR. – Example 7­4 Example 8­4 Example 8­4 Example 8­5 本本本本本本本 DATA3 本 DATA4 本本本 本 ES 本 DF 本本本本本本本本本本本 ES 本 DS 本 DF 本 0 DI 8.1.3 Macros 8.1.3 Macros • A macro is a group of instructions that perform one task. • The difference between macro and procedure – procedure is accessed via a CALL instruction ; – a macro, and all the instructions defined in the macro, is inserted in the program at the point of usage. • Macro sequences execute faster than procedures – because there are no call and RET instruction to execute. • The MACRO and ENDM directives delineate a macro sequence. • Macro sequence must be defined before they are used in a program. • Local Variables in a Macro – A local variable is one that appears in the macro, but is not available outside the macro. – Use the LOCAL directive to define a local variable. – Example 7­9 . – The LOCAL directive must immediately follow the MACRO directive. label: must begin label: must begin with a letter or @ $ _ ? 7.2 Using The Keyboard And Video Display 7.2 Using The Keyboard And Video Display 7.2.1 Reading the Keyboard with DOS Functions • This section uses INT 21H with various DOS function calls to read the keyboard. • Data read from the keyboard are either in ASCII­coded form or in extended ASCII­coded form. • Table 7­3 lists most of the extended ASCII codes. • Table 1­7 in section 1.4 list ASCII codes. 7.2.1 Reading the Keyboard with DOS Functions 7.2.1 Reading the Keyboard with DOS Functions • There are three ways to read the keyboard: The first method ( DOS Function 01H) reads a key and echoes (or displays) the key on the video screen. – A second way ( DOS Function 06H) simply tests to see if a key is pressed. • If it is, it reads the key • Otherwise, it returns without any key – The third way ( DOS Function 09H) allows an entire character string to be read from the keyboard. • Reading a Key with Echo – Example 7­15 show a key is read from the keyboard and echoed (sent) back out to the video display by using a procedure called KEY. – DOS Function 01H • entry: AH=01H, • exit: AL=ASCII character. if AL=0, the function call must be invoked again to read the extended ASCII character. – It always echoes the character to the screen, even if it is an unwanted character. – It responds to the control­C key combination, and exits to DOS if it is typed. – The procedure of Example 7­15 returns • with carry set (1) to indicate an extended ASCII character • with carry cleared (0) to indicate a normal ASCII character. – When this procedure is called, the CALL instruction might be followed by a JC EXTENDED to process the extended ASCII character. • Reading a Key without an Echo – Function number 06H. This function reads a key without an echo to the screen. • entry: AH=06H, DL=FFH (for key input ) or DL=ASCII char (for char display) • exit: when DL=FFH, AL=ASCII char ; if AL=0, the function call must be invoked again to read the extended ASCII character. – Also allows extended ASCII character. – Does not respond to the control­C key combination. – Example 7­16 shows a procedure that uses function number 06H to read the keyboard. – This performs as shown in Example 7­15, except to character is echoed to the video display. – difference between Function call number 06H and 01H • Function call number 06H returns the INT 21H, even if no key is typed; • Function call number 01H waits for a key to be typed. 7.2.2 Writing to the Video Display with DOS 7.2.2 Writing to the Video Display with DOS Functions • Video data can be displayed in a number of different ways: – use function 02H or 06H for displaying one character at a time. – or function 09H for displaying an entire string of characters • Displaying One ASCII Character – Both DOS functions 02H and 06H are explained together because they are identical for displaying ASCII data – Example 7­18 shows how this function displays a carriage return (0DH) and a line feed (0AH) Macro substitution Macro substitution • Displaying a Character String ; DOS Function number 09H ; – entry: • DS:DX address the character string. • A character string is a series of ASCII­coded characters • It ends with a $ (24H). – exit: no – Example 7­19 shows how a message is displayed at the current cursor position on the video display. 7.2.3 Using BIOS Video Function Calls 7.2.3 Using BIOS Video Function Calls • Video BIOS (basic I/O system) function call at INT 10H – BIOS function calls vs DOS function calls • allow more control over the video display. • be faster. – Video BIOS function number 03H: • read the cursor position. • entry: AH=03H, BH=page number; • exit: DH=row, DL=column – Video BIOS function number 02H • allows the cursor to be placed at any screen position. • entry: BH=page number, DH=row, DL=column • exit: no – The page number in register BH should be 0 before setting the cursor position. • Most software does not normally access the other pages (1­7) of the video display. – The cursor position: 0­79, 0­24 • left­hand page column is column 0, progressing across a line to column 79. • Row 0 is the uppermost line, while row 24 is the last line on the screen. – Example 7­20: use INT 10H and INT 21H to clear screen. 8.3 DATA CONVERSIONS 8.3 8.3.1 Converting from Binary to ASCII • Conversion from binary to ASCII is accomplished in two ways: – by the AAM instruction if the number is less than 100, or – by a series of decimal divisions (divide by 10) • e.g. 07BFH­­­­>1983, 2A3CH­­­­>10812 • The AAM instruction converts the value in AX into a two­digit unpacked BCD number in AX. – If the number in AX is 0062H (98 decimal), AX contains a 0908H after AAM executes. – It is converted to ASCII code by adding a 3030H to AX • Example 7­27 illustrates a program that uses the procedure DISP, which processes the binary value in AL (0­99) and displays it on the video screen as decimal • Example 7­28: the unsigned 16­bit content of AX is converted to ASCII and displayed on the video screen. • The algorithm for converting from binary to ASCII code is: – Divide by the 10, then save the remainder on the stack as a significant BCD digit. – Repeat step 1 until the quotient is a 0. – Retrieve each remainder and add a 30H to convert to ASCII before displaying or printing. • e.g. AX=7BFH=1983, – DX=0, DX:AX/10, ­­> DX=3, AX=198, push 3 – DX=0, DX:AX/10, ­­> DX=8, AX=19, push 8 – DX=0, DX:AX/10, ­­> DX=9, AX=1, push 9 – DX=0, DX:AX/10, ­­> DX=1, AX=0, push 1; pop 1,9,8,3 • This same scheme of dividing by 10 can be expanded to 2, 8,16 etc. – For example, if AX is divided by 8 instead of 10, the number is displayed in octal. • e.g. AX=7BFH=1983, – DX=0, DX:AX/10, ­­> DX=3, AX=198, push 3 – DX=0, DX:AX/10, ­­> DX=8, AX=19, push 8 – DX=0, DX:AX/10, ­­> DX=9, AX=1, push 9 – DX=0, DX:AX/10, ­­> DX=1, AX=0, push 1; – pop 1,9,8,3 CX=4 8.3.2 Converting from ASCII to Binary 8.3.2 本本本 • The algorithm for converting from ASCII to binary is: – 1. BX<­­0 – 2. read a char to AL, AL<­­ AL­30H – 3. BX<­­ BX*10+AL – 4. Repeat steps 2 and 3 until the character typed is not an ASCII­coded number • E.g. type ‘9’, ‘8’, ‘3’ – BX=9 – BX=98 – BX=983=3D7H • Example 7­29 illustrates a procedure (READN) used this algorithm. 本本本 本本本 BX<­­BX*10 BX<­­BX*10 BX<­­BX*10+AL 7.4 DISK FILES 7.4 DISK FILES • Data are stored on the disk in the form of files 7.4.3 Sequential File Access • All DOS files are sequential files • A sequential file is stored and accessed from the beginning of the file toward the end. • File Creation: the INT 21H function call number 3CH • entry: – AH = 3CH. – DS:DX points to the file name; • File name is a ASCII­Z string. e.g. “c:\dread\my.bak”, \0 • ASCII­Z string is a string ended with a null char (0). – CX = the attribute of the file (or subdirectory). • exit: if CF is set, AX= error code; otherwise, AX=file handle. – Before data can be written to this new file, the file must first be created. – Example 7­38 lists a short procedure that creates this new file on the disk. – Whenever a file is created, CX=attributes of the file. – Table 7­6 lists and defines the attribute bit positions. • A logic 1 in a bit selects the attribute, • while a logic 0 does not. • Writing to a File(Function number 40H ) – Before writing to a file, the file must have been created or opened. – entry: AH=40H • BX = the file handle. • CX = the number of bytes to be written. • DS:DX = the address of the buffer. – exit: • If an error occurs, the carry flag is set. • If no error occurs, the carry flag is cleared, AX = number of bytes written to the file. – Example 7­39: write all 256 bytes of BUFFER to the file. – Errors that occur for writes usually indicate that: • the disk is full • there is some type of media error • Opening, Reading, and Closing a File – To read a file, it must be opened first – The DOS file handle must be used for reading, writing, and closing a file – Example 7­40 lists a sequence of instructions: • opens a file, • reads 256 bytes from the file into memory area BUFFER, • closes the file. • Open a file (Function number 3DH) – entry: DS:DX=address of ASCII­Z string file name, AL=access code • If AL = 00H, the file is opened for a read • If AL = 01H, the file is opened for a write • If AL = 02H, the file is opened for a read or a write – exit: if CF=0, AX=file handle; otherwise, an error occurs. • Reading a File (Function number 3FH) – Entry: • BX = the file handle • CX = number of bytes to be read • DS:DX = the address of a memory buffer to store file data. – Exit: • if carry flag = 1, an error occurs. • otherwise, the AX = the number of bytes read from the file. • Closing a File (Function number 3EH) – Entry: BX = the file handle; – Exit: if carry flag = 1, AX=error code. • Always be certain to close a file after it is read or written – If a file is left open, some serious problems can occur that can actually destroy the disk and all its data • The File Pointer – When a file is opened, written, or read, the file pointer addresses the current location in the sequential file. • When a file is opened, the file pointer always addresses the first byte of the file • If a file is 1024 bytes long, and a read function reads 1023 bytes, – the file pointer addresses the last byte of the file, but not the end of the file. – The file pointer is a 32­bit number that addresses any byte in a file. • Move file pointer (function number 42H) – entry: BX=file handle; CX:DX=number of bytes to move; • AL=move technique: – from the start of the file (AL = 00H), – from the current location (AL = 01H), – Or from the end of the file (AL = 02H). – exit: • if CF is set, AX=error code; • otherwise, CX:DX=number of bytes that pointer moved. • Suppose that you want to append the file with 256 bytes of new information. – If you attempt to write without moving the file pointer to the end of the file, the new data will overwrite the first 256 bytes of the file. • Example 7­41 shows a program: – It opens a file, moves the file pointer to the end of the file, – It writes 256 bytes of data, and then closes the file. – This appends the file with 256 new bytes of data. 7.6 Interrupt Hooks 7.6 Interrupt Hooks ; 7 ;;;; • Hook are used to tap into or intercept the interrupt structure of the microprocessor. 7.6.1 Intercepting an Interrupt • In order to intercept an interrupt, we must installing an interrupt handle through a hook ; • DOS function call number 35 ; read the current interrupt vector. – entry: AL = vector type number – exit: ES:BX=address stored at vector. • DOS function call number 25 ; change the address of the current vector. – entry: • AL = vector type number; • DS:DX=address of new interrupt procedure; • Example 7­48: installing an interrupt handle through a hook. Example 7­48 Example 7­48 ; 7 ;;;; ; A sequence of instructions that show the installation ; or a new interrupt for vector 0 (divide error). ; Note this is not a complete program .MODEL TINY .CODE .STARTUP JMP MAIN ; skip ADDR DD ? ; old interrupt vector NEW PROC FAR ; new interrupt procedure IRET ; do nothing interrupt NEW ENDP MAIN: MAIN: DS ; 7 ;;;; MOV AX, CS ; address CS with MOV DS, AX ; get vector 0 address MOV AX, 3500H INT 21H ; save vector address at ADDR MOV WORD PTR ADDRESS, BX MOV WORD PTR ADDRESS+2, ES ; install new interrupt vector 0 address MOV AX, 2500H MOV DX, OFFSET NEW INT 21H ; other installation software continues here ...
View Full Document

Ask a homework question - tutors are online