• 沒有找到結果。

A Sample Program

在文檔中 THE INTEL MICROPROCESSORS (頁 169-175)

Example 4–20 provides a sample program, using full-segment definitions, that reads a character from the keyboard and displays it on the CRT screen. Although this program is trivial, it illus-trates a complete workable program that functions on any personal computer using DOS, from the earliest 8088-based system to the latest Core2-based system. This program also illustrates the use of a few DOS function calls. (Appendix A lists the DOS function calls with their parame-ters.) The BIOS function calls allow the use of the keyboard, printer, disk drives, and everything else that is available in your computer system.

This example program uses only a code segment because there is no data. A stack segment should appear, but it has been left out because DOS automatically allocates a l28-byte stack for all programs. The only time that the stack is used in this example is for the INT 21H instructions that call a procedure in DOS. Note that when this program is linked, the linker signals that no stack segment is present. This warning may be ignored in this example because the stack is fewer than 128 bytes.

Notice that the entire program is placed into a far procedure called MAIN. It is good pro-gramming practice to write all software in procedural form, which allows the program to be used as a procedure at some future time if necessary. It is also fairly important to document register use and any parameters required for the program in the program header, which is a section of comments that appear at the start of the program.

The program uses DOS functions 06H and 4CH. The function number is placed in AH before the INT 21H instruction executes. The 06H function reads the keyboard if , or displays the ASCII contents of DL if it is not 0FFH. Upon close examination, the first section of the program moves 06H into AH and 0FFH into DL, so that a key is read from the keyboard.

The INT 21H tests the keyboard; if no key is typed, it returns equal. The JE instruction tests the equal condition and jumps to MAIN if no key is typed.

When a key is typed, the program continues to the next step, which compares the contents of AL with an @ symbol. Upon return from the INT 21H, the ASCII character of the typed key is found in AL. In this program, if an @ symbol is typed, the program ends. If the @ symbol is not typed, the program continues by displaying the character typed on the keyboard with the next INT 21H instruction.

The second INT 21H instruction moves the ASCII character into DL so it can be displayed on the CRT screen. After displaying the character, a JMP executes. This causes the program to continue at MAIN, where it repeats reading a key.

If the @ symbol is typed, the program continues at MAIN1, where it executes the DOS function code number 4CH. This causes the program to return to the DOS prompt so that the computer can be used for other tasks.

More information about the assembler and its application appears in Appendix A and in the next several chapters. Appendix A provides a complete overview of the assembler, linker, and DOS functions. It also provides a list of the BIOS (basic I/O system) functions. The information provided in the following chapters clarifies how to use the assembler for certain tasks at different levels of the text.

DL = 0FFH

EXAMPLE 4–20

;An example DOS full-segment program that reads a key and

;displays it. Note that an @ key ends the program.

;

0000 CODE_SEG SEGMENT 'CODE' ASSUME CS:CODE_SEG 0000 MAIN PROC FAR

0000 B4 06 MOV AH,06H ;read a key 0002 B2 FF MOV DL,0FFH

0004 CD 21 INT 21H

0006 74 F8 JE MAIN ;if no key typed 0008 3C 40 CMP AL,'@'

000A 74 08 JE MAIN1 ;if an @ key

000C B4 06 MOV AH,06H ;display key (echo) 000E 8A D0 MOV DL,AL

0010 CD 21 INT 21H

0012 EB EC JMP MAIN ;repeat 0014 MAIN1:

0014 B4 4C MOV AH,4CH ;exit to DOS 0016 CD 21 INT 21H

0018 MAIN ENDP 0018 END MAIN

EXAMPLE 4–21

;An example DOS model program that reads a key and displays

;it. Note that an @ key ends the program.

;

.MODEL TINY 0000 .CODE

.STARTUP 0100 MAIN:

0100 B4 06 MOV AH,6 ;read a key 0102 B2 FF MOV DL,0FFH

0104 CD 21 INT 21H

0106 74 F8 JE MAIN ;if no key typed 0108 3C 40 CMP AL, '@'

010A 74 08 JE MAIN1 ;if an @ key

010C B4 06 MOV AH,06H ;display key (echo) 010E 8A D0 MOV DL,AL

0110 CD 21 INT 21H

0112 EB EC JMP MAIN ;repeat 0114 MAIN1:

.EXIT ;exit to DOS END

Example 4–21 shows the program listed in Example 4–20, except models are used instead of full-segment descriptions. Please compare the two programs to determine the differences.

Notice how much shorter and cleaner looking the models can make a program.

4–8 SUMMARY

1. Data movement instructions transfer data between registers, a register and memory, a regis-ter and the stack, memory and the stack, the accumulator and I/O, and the flags and the stack. Memory-to-memory transfers are allowed only with the MOVS instruction.

2. Data movement instructions include MOV, PUSH, POP, XCHG, XLAT, IN, OUT, LEA, LOS, LES, LSS, LGS, LFS, LAHF, SAHF, and the following string instructions: LODS, STOS, MOVS, INS, and OUTS.

3. The first byte of an instruction contains the opcode. The opcode specifies the operation per-formed by the microprocessor. The opcode may be preceded by one or more override pre-fixes in some forms of instructions.

4. The D-bit, located in many instructions, selects the direction of data flow. If , the data flow from the REG field to the R/M field of the instruction. If , the data flow from the R/M field to the REG field.

5. The W-bit, found in most instructions, selects the size of the data transfer. If , the data are byte-sized; if , the data are word-sized. In the 80386 and above, specifies either a word or doubleword register.

6. MOD selects the addressing mode of operation for a machine language instruction’s R /M field. If , there is no displacement; if , an 8-bit sign-extended dis-placement appears; if , a 16-bit displacement occurs; and if , a regis-ter is used instead of a memory location. In the 80386 and above, the MOD bits also specify a 32-bit displacement.

7. A 3-bit binary register code specifies the REG and R/M fields when the MOD = 11. The 8-bit registers are AH, AL, BH, BL, CH, CL, DH, and DL. The l6-8-bit registers are AX, BX, CX, DX, SP, BP, DI, and SI. The 32-bit registers are EAX, EBX, ECX, EDX, ESP, EBP, EDI, and ESI. To access the 64-bit registers, a new prefix is added called the REX prefix that contains a fourth bit for accessing registers R8 through R15.

8. When the R/M field depicts a memory mode, a 3-bit code selects one of the following

modes: , [BX], [BP], [DI], or [SI] for 16-bit

instructions. In the 80386 and above, the R/M field specifies EAX, EBX, ECX, EDX, EBP, EDI, and ESI or one of the index modes of addressing memory data. If the scaled-index mode is selected , an additional byte (scaled-index byte) is added to the instruction to specify the base register, index register, and the scaling factor.

9. By default, all memory-addressing modes address data in the data segment unless BP or EBP addresses memory. The BP or EBP register addresses data in the stack segment.

10. The segment registers are addressed only by the MOV, PUSH, or POP instructions. The instruction may transfer a segment register to a 16-bit register, or vice versa. MOV CS,reg or POP CS instructions are not allowed because they change only a portion of the address. The 80386 through the Pentium 4 include two additional segment registers, FS and GS.

11. Data are transferred between a register or a memory location and the stack by the PUSH and POP instructions. Variations of these instructions allow immediate data to be pushed onto the stack, the flags to be transferred between the stack, and all 16-bit registers can be trans-ferred between the stack and the registers. When data are transtrans-ferred to the stack, 2 bytes (8086–80286) always move. The most significant byte is placed at the location addressed by , and the least significant byte is placed at the location addressed by . After placing the data on the stack, SP is decremented by 2. In the 80386–Core2, 4 bytes of data from a memory location or register may also be transferred to the stack.

12. Opcodes that transfer data between the stack and the flags are PUSHF and POPF. Opcodes that transfer all the 16-bit registers between the stack and the registers are PUSHA and POPA. In the 80386 and above, PUSHFD and POPFD transfer the contents of the EFLAGS between the microprocessor and the stack, and PUSHAD and POPAD transfer all the 32-bit registers. The PUSHA and POPA instructions are invalid in the 64-bit mode.

13. LEA, LDS, and LES instructions load a register or registers with an effective address. The LEA instruction loads any bit register with an effective address; LDS and LES load any 16-bit register and either DS or ES with the effective address. In the 80386 and above, additional instructions include LFS, LGS, and LSS, which load a 16-bit register and FS, GS, or SS.

SP - 2

14. String data transfer instructions use either or both DI and SI to address memory. The DI off-set address is located in the extra segment, and the SI offoff-set address is located in the data segment. If the 80386–Core2 is operated in protected mode, ESI and EDI are used with the string instructions.

15. The direction flag (D) chooses the auto-increment or auto-decrement mode of operation for DI and SI for string instructions. To clear D to 0, use the CLD instruction to select the auto-increment mode; to set D to 1, use the STD instruction to select the auto-decrement mode.

Either or both DI and SI increment/decrement by 1 for a byte operation, by 2 for a word operation, and by 4 for a doubleword operation.

16. LODS loads AL, AX, or EAX with data from the memory location addressed by SI; STOS stores AL, AX, or EAX in the memory location addressed by DI; and MOVS transfers a byte, a word, or a doubleword from the memory location addressed by SI into the location addressed by DI.

17. INS inputs data from an I/O device addressed by DX and stores it in the memory location addressed by DI. The OUTS instruction outputs the contents of the memory location addressed by SI and sends it to the I/O device addressed by DX.

18. The REP prefix may be attached to any string instruction to repeat it. The REP prefix repeats the string instruction the number of times found in register CX.

19. Arithmetic and logic operators can be used in assembly language. An example is MOV AX,34*3, which loads AX with 102.

20. Translate (XLAT) converts the data in AL into a number stored at the memory location addressed by BX plus AL.

21. IN and OUT transfer data between AL, AX, or EAX and an external I/O device. The address of the I/O device is either stored with the instruction (fixed-port addressing) or in register DX (variable-port addressing).

22. The Pentium Pro–Core2 contain a new instruction called CMOV, or conditional move. This instruction only performs the move if the condition is true.

23. The segment override prefix selects a different segment register for a memory location than the default segment. For example, the MOV AX,[BX] instruction uses the data segment, but the MOV AX,ES:[BX] instruction uses the extra segment because of the ES: override prefix.

Using the segment override prefix is the only way to address the FS and GS segments in the 80386 through the Pentium 4.

24. The MOVZX (move and zero-extend) and MOVSX (move and sign-extend) instructions, found in the 80386 and above, increase the size of a byte to a word or a word to a double-word. The zero-extend version increases the size of the number by inserting leading zeros.

The sign-extend version increases the size of the number by copying the sign-bit into the more significant bits of the number.

25. Assembler directives DB (define byte), DW (define word), DD (define doubleword), and DUP (duplicate) store data in the memory system.

26. The EQU (equate) directive allows data or labels to be equated to labels.

27. The SEGMENT directive identifies the start of a memory segment and ENDS identifies the end of a segment when full-segment definitions are in use.

28. The ASSUME directive tells the assembler what segment names you have assigned to CS, DS, ES, and SS when full-segment definitions are in effect. In the 80386 and above, ASSUME also indicates the segment name for FS and GS.

29. The PROC and ENDP directives indicate the start and end of a procedure. The USES direc-tive (MASM version 6.x) automatically saves and restores any number of registers on the stack if they appear with the PROC directive.

30. The assembler assumes that software is being developed for the 8086/8088 microprocessor unless the .286, .386, .486, .586, or .686 directive is used to select one of these other micro-processors. This directive follows the .MODEL statement to use the 16-bit instruction mode and precedes it for the 32-bit instruction mode.

31. Memory models can be used to shorten the program slightly, but they can cause problems for larger programs. Also be aware that memory models are not compatible with all assem-bler programs.

4–9 QUESTIONS AND PROBLEMS

1. The first byte of an instruction is the ____________, unless it contains one of the override prefixes.

2. Describe the purpose of the D- and W-bits found in some machine language instructions.

3. In a machine language instruction, what information is specified by the MOD field?

4. If the register field (REG) of an instruction contains 010 and , what register is selected, assuming that the instruction is a 16-bit mode instruction?

5. How are the 32-bit registers selected for the Pentium 4 microprocessor?

6. What memory-addressing mode is specified by with for a 16-bit instruction?

7. Identify the default segment registers assigned to the following:

(a) SP (b) EBX (c) DI (d) EBP (e) SI

8. Convert an 8B07H from machine language to assembly language.

9. Convert an 8B9E004CH from machine language to assembly language.

10. If a MOV SI,[BX+2] instruction appears in a program, what is its machine language equivalent?

11. If a MOV ESI,[EAX] instruction appears in a program for the Core2 microprocessor operat-ing in the 16-bit instruction mode, what is its machine language equivalent?

12. What is the purpose of REX?

13. What is wrong with a MOV CS,AX instruction?

14. Form a short sequence of instructions that load the data segment register with a 1000H.

15. The PUSH and POP instructions always transfer a(n) ____________ -bit number between the stack and a register or memory location in the 80386–Core2 microprocessors when oper-ated in the 32-bit mode.

16. Create an instruction that places RAX onto the stack in the 64-bit mode for the Pentium 4.

17. What segment register may not be popped from the stack?

18. Which registers move onto the stack with the PUSHA instruction?

19. Which registers move onto the stack for a PUSHAD instruction?

20. Describe the operation of each of the following instructions:

(a) PUSH AX (b) POP ESI (c) PUSH [BX]

(d) PUSHFD (e) POP DS (f) PUSHD 4

21. Explain what happens when the PUSH BX instruction executes. Make sure to show where BH and BL are stored. (Assume that SP = 0100H and .)

22. Repeat question 21 for the PUSH EAX instruction.

23. The 16-bit POP instruction (except for POPA) increments SP by ____________.

24. What values appear in SP and SS if the stack is addressed at memory location 02200H?

SS = 0200H

MOD = 00 R>M = 001

W = 0

25. Compare the operation of a MOV DI,NUMB instruction with an LEA DI,NUMB instruction.

26. What is the difference between an LEA SI,NUMB instruction and a MOV SI,OFFSET NUMB instruction?

27. Which is more efficient, a MOV with an OFFSET or an LEA instruction?

28. Describe how the LDS BX,NUMB instruction operates.

29. What is the difference between the LDS and LSS instructions?

30. Develop a sequence of instructions that moves the contents of data segment memory loca-tions NUMB and into BX, DX, and SI.

31. What is the purpose of the direction flag?

32. Which instructions set and clear the direction flag?

33. Which string instruction(s) use both DI and SI to address memory data?

34. Explain the operation of the LODSB instruction.

35. Explain the operation of the LODSQ instruction for the 64-bit mode of the Pentium 4 or Core2.

36. Explain the operation of the OUTSB instruction.

37. Explain the operation of the STOSW instruction.

38. Develop a sequence of instructions that copy 12 bytes of data from an area of memory addressed by SOURCE into an area of memory addressed by DEST.

39. What does the REP prefix accomplish and what type of instruction is it used with?

40. Select an assembly language instruction that exchanges the contents of the EBX register with the ESI register.

41. Where is the I/O address (port number) stored for an INSB instruction?

42. Would the LAHF and SAHF instructions normally appear in software?

43. Write a short program that uses the XLAT instruction to convert the BCD numbers 0–9 into ASCII-coded numbers 30H–39H. Store the ASCII-coded data in a TABLE located within the data segment.

44. Explain how the XLAT instruction transforms the contents of the AL register.

45. Explain what the IN AL,12H instruction accomplishes.

46. Explain how the OUT DX,AX instruction operates.

47. What is a segment override prefix?

48. Select an instruction that moves a byte of data from the memory location addressed by the BX register in the extra segment into the AH register.

49. Develop a sequence of instructions that exchanges the contents of AX with BX, ECX with EDX, and SI with DI.

50. What is an assembly language directive?

51. What is accomplished by the CMOVNE CX,DX instruction in the Pentium 4 microprocessor?

52. Describe the purpose of the following assembly language directives: DB, DW, and DD.

53. Select an assembly language directive that reserves 30 bytes of memory for array LIST1.

54. Describe the purpose of the EQU directive.

55. What is the purpose of the .686 directive?

56. What is the purpose of the .MODEL directive?

57. If the start of a segment is identified with .DATA, what type of memory organization is in effect?

58. If the SEGMENT directive identifies the start of a segment, what type of memory organiza-tion is in effect?

59. What does the INT 21H accomplish if AH contains a 4CH?

60. What directives indicate the start and end of a procedure?

61. Explain the purpose of the USES statement as it applies to a procedure with version 6.x of MASM.

62. Develop a near procedure that stores AL in four consecutive memory locations within the data segment, as addressed by the DI register.

63. How is the Pentium 4 microprocessor instructed to use the 16-bit instruction mode?

64. Develop a far procedure that copies contents of the word-sized memory location CS:DATA4 into AX, BX, CX, DX, and SI.

NUMB + 1

156

INTRODUCTION

In this chapter, we examine the arithmetic and logic instructions. The arithmetic instructions include addition, subtraction, multiplication, division, comparison, negation, increment, and decrement. The logic instructions include AND, OR, Exclusive-OR, NOT, shifts, rotates, and the logical compare (TEST). This chapter also presents the 80386 through the Core2 instruc-tions XADD, SHRD, SHLD, bit tests, and bit scans. The chapter concludes with a discussion of string comparison instructions, which are used for scanning tabular data and for comparing sections of memory data. Both comparison tasks are performed efficiently with the string scan (SCAS) and string compare (CMPS) instructions.

If you are familiar with an 8-bit microprocessor, you will recognize that the 8086 through

If you are familiar with an 8-bit microprocessor, you will recognize that the 8086 through

在文檔中 THE INTEL MICROPROCESSORS (頁 169-175)