Assembly Language

April 28, 2017 | Author: Oscar Said Villa Saucedo | Category: N/A
Share Embed Donate


Short Description

Download Assembly Language...

Description

Assembly Language W.P.fang

W.P.Fang Department of CSIE YUST, Copy Right Reserved

1

Reference • Ytha Yu, Charles Marut, Assembly Language Programming and Organziation of the IBM PC

W.P.Fang Department of CSIE YUST, Copy Right Reserved

2

requirement • • • •

Exercise+ Program homework 40% Midterm examination 30% Final examination 30% Ps: – Cheat and late hand out is not acceptable

W.P.Fang Department of CSIE YUST, Copy Right Reserved

3

Outline • Microcomputer Systems • Representation of Numbers and Characters • Organization of the IBM Personal Computers • Introduction to IBM PC Assembly Language • The Processor Status and the FLAGS Register W.P.Fang Department of CSIE YUST, Copy Right Reserved

4

Outline • • • • • • •

Flow Control Instructions Logic, Shift, ad Rotate Instructions The Stack and Introduction to Procedures Multiplication and Division Instructions Arrays and Addressing Modes The String Instructions Text Display and Keyboard Programming W.P.Fang Department of CSIE YUST, Copy Right Reserved

5

Outline • • • • • • • •

Macros Memory Management BIOS and DOS Interrupt Color Graphics Recursion Advanced Arithmetic Disk and File Operations Intel’s Advanced Microprocessors6 W.P.Fang Department of CSIE YUST, Copy Right Reserved

6

schdular 日期 1 2 3 4 5 6 7 8 9

進度

備註

W.P.Fang Department of CSIE YUST, Copy Right Reserved

7

schdular 日期 10 11 12 13 14 15 16 17 18

進度

備註

W.P.Fang Department of CSIE YUST, Copy Right Reserved

8

Microcomputer Systems

W.P.Fang Department of CSIE YUST, Copy Right Reserved

9

Microcomputer Systems • Overview – Introduction to the architeture of microcomputers in general and to the IBM PC in particular.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

10

Microcomputer System (component) • The components o a microcomputer system – – – –

System unit Keyboard display screen Disk drives

W.P.Fang Department of CSIE YUST, Copy Right Reserved

11

Microcomputer System (component) • Computer circuits – Central processing unit (CPU) – I/O circuits

• Microcomputer – CPU is a single-chip processor (microprocessor)

• System board – Microprocessor and memorycircuirs W.P.Fang Department of CSIE YUST, Copy Right Reserved

12

Microcomputer System (component) • Bytes – eight bits • Address – identified by a number • Content– the data stored in a memory byte. Address : 3 2 1 0

content : 01100001 01011110 00000000 01101110

W.P.Fang Department of CSIE YUST, Copy Right Reserved

13

Microcomputer System (component) • Example 1.1 Suppose a processor uses 20 bits for an address. How many memory bytes can be accessed?

W.P.Fang Department of CSIE YUST, Copy Right Reserved

14

Microcomputer System (component) • Word – typical microcomputer, two bytes form a word • Bit position Byte bit position 7

6

5

4

3

2

1 0

Word bit position 15 14 13 12

11 10 9

High byte

88 7

6

5

4

3

2

1 0

Low byte

W.P.Fang Department of CSIE YUST, Copy Right Reserved

15

Microcomputer system (component) • RAM and ROM • Bus

W.P.Fang Department of CSIE YUST, Copy Right Reserved

16

Microcomputer system (component) • In any case, each instruction that the CPU executes is a bit string • This language of 0’s and 1’s is called machine language • The instructions performed by a CPU are called instruction set. • The instruction set for each CPU is unique. • To keep the cost of computers down, machine language instructions are designed to be simple W.P.Fang Department of CSIE YUST, Copy Right Reserved

17

Microcomputer system (component) • EU (Execution Unit) – Arithmetic • +-*/

– Logic unit • AND, OR, NOT

– Registers • AX,BX,CXDX,SI,DI,BP,SP

W.P.Fang Department of CSIE YUST, Copy Right Reserved

18

Microcomputer system (component) • EIU ( Bus Interface Unit) – Communication between the EU and the memory or I/O circuits – Registers • CS,DS,ES,SS,IP

W.P.Fang Department of CSIE YUST, Copy Right Reserved

19

Microcomputer system (component) • I/O ports

W.P.Fang Department of CSIE YUST, Copy Right Reserved

20

Microcomputer system (instruction execution) • Instruction Execution • Machine instruction – Opcode : the type of operation – Operand : given as memory address to he data to be operated on.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

21

Microcomputer system (instruction execution) • Fetch-execute cycle – Fetch • Fetch an instruction from memory • Decde he inctruction to determine the operation • Fetch data from memory if necessary.

– Execute • Perform the operation on the data • Store the result in memory if needed.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

22

Microcomputer system (I/O Devices) • • • •

Magnetics disks Keyboard Display monitor printers

W.P.Fang Department of CSIE YUST, Copy Right Reserved

23

Microcomputer system (programming languages) • Machine language Machine instruction

operation

10100001 00000000 00000000

Fetch the content of memory word to and put it in register AX

00000101 00000100 00000000

Add 4 to AX

10100011 00000000 00000000

Store the content of AX in memory word 0

W.P.Fang Department of CSIE YUST, Copy Right Reserved

24

Microcomputer system (programming languages) • Assembly language Assembly language instruction

operation

MOV AX,A

Fetch the content of location A and put it in register AX

ADD AX,4

Add 4 to AX

MOV A,AX

move the content of AX into location A W.P.Fang Department of CSIE YUST, Copy Right Reserved

25

Microcomputer system (programming languages) • Assembler • compiler • High-level language

W.P.Fang Department of CSIE YUST, Copy Right Reserved

26

Microcomputer system (programming languages) • Advantage of high-level languages – Closer to natural language – Reduce Coding time – Executed on any machine

• Advantage of assembly language – Efficiency – Some operations can be done easily in assembly language but may be impossible at a higher level W.P.Fang Department of CSIE YUST, Copy Right Reserved

27

Microcomputer system (assembly languages) ch1.asm

TITLE PGM1_1: SAMPLE PROGRAM .MODEL SMALL .STACK 100H .DATA A DW 2 B DW 5 SUM DW ? .CODE MAIN PROC ;initialize DS MOV AX,@DATA MOV DS,AX ;add the numbers MOV AX,A ADD AX,B MOV SUM,AX ;exit to Dos MOV AX,4C00H INT 21H MAIN ENDP END MAIN

W.P.Fang Department of CSIE YUST, Copy Right Reserved

28

Microcomputer system (Debug) • • • • • • • • • • • •

assemble A [address] compare C range address dump D [range] enter E address [list] fill F range list go G [=address] [addresses] hex H value1 value2 input I port load L [address] [drive] [firstsector] [number] move M range address name N [pathname] [arglist] output O port byte

W.P.Fang Department of CSIE YUST, Copy Right Reserved

29

Microcomputer system (Debug) • • • • • • • • • • •

proceed P [=address] [number] quit Q register R [register] search S range list trace T [=address] [value] unassemble U [range] write W [address] [drive] [firstsector] [number] allocate expanded memory XA [#pages] deallocate expanded memory XD [handle] map expanded memory pages XM [Lpage] [Ppage] [handle] display expanded memory status XS

W.P.Fang Department of CSIE YUST, Copy Right Reserved

30

Microcomputer system (tasm) • • • • • • • • • • • • • • • • • • • • • • • •

Syntax: TASM [options] source [,object] [,listing] [,xref] /a,/s Alphabetic or Source-code segment ordering /c Generate cross-reference in listing /dSYM[=VAL] Define symbol SYM = 0, or = value VAL /e,/r Emulated or Real floating-point instructions /h,/? Display this help screen /iPATH Search PATH for include files /jCMD Jam in an assembler directive CMD (eg. /jIDEAL) /kh# Hash table capacity # symbols /l,/la Generate listing: l=normal listing, la=expanded listing /ml,/mx,/mu Case sensitivity on symbols: ml=all, mx=globals, mu=none /mv# Set maximum valid length for symbols /m# Allow # multiple passes to resolve forward references /n Suppress symbol tables in listing /os,/o,/op,/oi Object code: standard, standard w/overlays, Phar Lap, or IBM /p Check for code segment overrides in protected mode /q Suppress OBJ records not needed for linking /t Suppress messages if successful assembly /uxxxx Set version emulation, version xxxx /w0,/w1,/w2 Set warning level: w0=none, w1=w2=warnings on /w-xxx,/w+xxx Disable (-) or enable (+) warning xxx /x Include false conditionals in listing /z Display source line with error message /zi,/zd,/zn Debug info: zi=full, zd=line numbers only, zn=none

W.P.Fang Department of CSIE YUST, Copy Right Reserved

31

Microcomputer system (tlink) • • • • • • • • • • • • • • • • • •

Turbo Link Version 3.01 Copyright (c) 1987, 1990 Borland International Syntax: TLINK objfiles, exefile, mapfile, libfiles @xxxx indicates use response file xxxx Options: /m = map file with publics /x = no map file at all /i = initialize all segments /l = include source line numbers /s = detailed map of segments /n = no default libraries /d = warn if duplicate symbols in libraries /c = lower case significant in symbols /3 = enable 32-bit processing /v = include full symbolic debug information /e = ignore Extended Dictionary /t = create COM file /o = overlay switch /ye = expanded memory swapping /yx = extended memory swapping W.P.Fang Department of CSIE YUST, Copy Right Reserved

32

Glossary • • • • • • • •

Add-in board or card Address Address bus Arithmetic and logic unit, ALU Assembler Assembly language Binary digit Bit W.P.Fang Department of CSIE YUST, Copy Right Reserved

33

Glossary • • • • • • • •

Bus Bus interface unit, BIU Byte Central processing unit, CPU Clock period Clock pulse Clock rate Clock speed W.P.Fang Department of CSIE YUST, Copy Right Reserved

34

Glossary • • • • • • • •

Compiler Contents Control bus Data bus Digital circuits disk drive Execution unit ,EU Expansion slots Fetch-execute cycle W.P.Fang Department of CSIE YUST, Copy Right Reserved

35

Glossary • • • • • • • • •

Firmware Fixed disk Floppy diskhardcopy Hard disk I/O devices I/O ports Intruction pointer,IP Instruction set Kilobyte, KB W.P.Fang Department of CSIE YUST, Copy Right Reserved

36

Glossary • • • • • • • •

Machine language Mega Megabyte, MB Megahertz, MHz Memory byte (circuit) Memory location Memory word microprocessor W.P.Fang Department of CSIE YUST, Copy Right Reserved

37

Glossary • • • • • • • •

Motherboard Opcode Operand Peripheral (device) Random access memory RAM Read-only memory (ROM) register W.P.Fang Department of CSIE YUST, Copy Right Reserved

38

Glossary • System board • Video adapter • word

W.P.Fang Department of CSIE YUST, Copy Right Reserved

39

Representation of numbers and characters

W.P.Fang Department of CSIE YUST, Copy Right Reserved

40

Representation of numbers and characters • Please refer to bcc

W.P.Fang Department of CSIE YUST, Copy Right Reserved

41

• Binary number system

W.P.Fang Department of CSIE YUST, Copy Right Reserved

42

• • • • • • •

Ex: 8A2Dh=? 11101=? 2BD4h=? Convert 95 to binary Convert 2B3Ch to binary Convert 1110101010 to hex W.P.Fang Department of CSIE YUST, Copy Right Reserved

43

• • • • •

Add Subtraction Find one’s complement of 5 (16 bits) Find the two’s complement of 5 Show -97 in 8bit,16 bit

W.P.Fang Department of CSIE YUST, Copy Right Reserved

44

ASCII code • American Standard Code for Information Interchange

W.P.Fang Department of CSIE YUST, Copy Right Reserved

45

Homework • Chap 2 – – – –

2. 7.d 8.c The rule of ascii

W.P.Fang Department of CSIE YUST, Copy Right Reserved

46

Representation of numbers and characters • Number system – Binary – Hexadecimal

• Conversion • Addition and subtraction • Character representation

W.P.Fang Department of CSIE YUST, Copy Right Reserved

47

Representation of numbers and characters (Glossary)

• ASCII (American Standard Code for Information Interchange) code • Binary number system • Hexadecimal number system • Least significant bit lsb • most significant bit msb • One’s complement of a binarynmber • Scan code W.P.Fang Department of CSIE YUST, Copy Right Reserved

48

Representation of numbers and characters (Glossary)

• Signed integer • Two’s complement of a binary number • Unsigned integer

W.P.Fang Department of CSIE YUST, Copy Right Reserved

49

Organization of the IBM Personal Computers

W.P.Fang Department of CSIE YUST, Copy Right Reserved

50

Organization of the IBM Personal Computers • Overall structure of the IBM PC – – – –

The memory organization I/O ports DOS routines BIOS routines

W.P.Fang Department of CSIE YUST, Copy Right Reserved

51

The intel x86 family • 1978 8086 16 bit • 80186 extended instruction set • 80286 real address mode,protected virtual address mode,multitasking,more addressable memory,2^30 bytes • 80386 32 bit virtual 8086 mode/386 SX à16bit bus W.P.Fang Department of CSIE YUST, Copy Right Reserved

52

Register • • • • • •

Data register Address register Status register Segment Pointer Index register

W.P.Fang Department of CSIE YUST, Copy Right Reserved

53

• • • • • • • • • • • • • •

Ax Bx Cx Dx Cs Ds Ss Es Si Di Sp Bp Ip flag W.P.Fang Department of CSIE YUST, Copy Right Reserved

54

Data register • • • •

AX(accumulator) BX(base register) CX(count register) DX(data register)

W.P.Fang Department of CSIE YUST, Copy Right Reserved

55

Segment register • • • •

CS -- code segment DS – data segment SS – stack segment ES – access second data segment (extra segment)

W.P.Fang Department of CSIE YUST, Copy Right Reserved

56

• Memory segment • Segment:offset address – Because 20bit can not represent in 16 bit

W.P.Fang Department of CSIE YUST, Copy Right Reserved

57

Logical address • Ex: • A4FB:4872h • A4FB0+4872h=A9822h(20-bit physical address)

W.P.Fang Department of CSIE YUST, Copy Right Reserved

58

• For the memory location whose physical address is specified by 1256Ah,give the address in segment:offset form for segments 1256h and 1240h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

59

• • • •

1256Ah=12560h+X 1256Ah=12400h+Y X=Ah,Y=16Ah 1256Ah=1256:000A=1240:016A

W.P.Fang Department of CSIE YUST, Copy Right Reserved

60

• A memory location has physical address 80FD2h. In what segment does it have offset BFD2h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

61

• • • •

Physical address=segmentx10h+offset Segmentx10h=physical address-offset 80FD2h-BFD2h=75000h àsegment 7500h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

62

Pointer and index register • SP – stack pointer – Used in conjunction with SS for accessing stack segment.

• BP – Base pointer – Used primarily to access data on the stack. – Unlike SP, we can also use BP to access data in the other segment

W.P.Fang Department of CSIE YUST, Copy Right Reserved

63

• SI – Source Index – Point to memory locations in the data segment addressed by DS. – By incrementing the contents of SI, we can easily access consecutive memory locations

• DI – Destination Index – Same functions as SI – For string operation – Access memory locations addressed by ES W.P.Fang Department of CSIE YUST, Copy Right Reserved

64

Instruction Pointer • IP – – – –

Use register CS ad IP to access instruction CS à segment number of next instruction IP à offset Can not be directly manipulated by an instruction

W.P.Fang Department of CSIE YUST, Copy Right Reserved

65

Flags • Status flags – Ex:ZF(zero flag)

• Controls – Ex: IF(interrupt flag) =0 à input from the keyboard are ignored by the processor.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

66

The operating system • Reading and executing the commands typed by the user • Performing I/O operations • Generating error messages • Managing memory and other resources

W.P.Fang Department of CSIE YUST, Copy Right Reserved

67

• Dos routine that services user commands is called COMMAND.COM

W.P.Fang Department of CSIE YUST, Copy Right Reserved

68

BIOS • Machine specific • DOS I/O operations are ultimately carried out by the BIOS routines • Addresses of the BIOS routine – interrupt vectors

W.P.Fang Department of CSIE YUST, Copy Right Reserved

69

Memory Bios F0000h Reserved E0000h Reserved D0000h Reserved C0000h Video B0000h Video A0000h Application program area Dos Bios and dos data 00400h Department of CSIE Intreeupt vectors W.P.Fang 00000h YUST, Copy Right Reserved

70

Port address

description

20h-21h

Interrupt controller

60h-63h

Keyboard controller

200h-20fh

Game controller

2f8h-2ffh

Serial port (COM2)

320h-32fh

Hard disk

378h-37fh

Parallel printer port 1

3c0h-3cfh

EGA

3d0h-3dfh

CGA

3f8h-3ffh

Serial port (COM1) W.P.Fang Department of CSIE YUST, Copy Right Reserved

71

Start-up operation • • • • • •

Power up CS register is set to FFFFh IP is set to 0000h à FFFF0h (in ROM) First check for system and memory errors Initialize the interrupt vectors and BIOS data • Boot program W.P.Fang Department of CSIE YUST, Copy Right Reserved

72

Introduction to IBM PC Assembly Language

W.P.Fang Department of CSIE YUST, Copy Right Reserved

73

Introduction to IBM PC Assembly Language • Syntax – Name operation operand(s) comment

• Ex: – START: MOV CX,5 ;initialize counter – MAIN PROC

W.P.Fang Department of CSIE YUST, Copy Right Reserved

74

• Name Field – 1 to 31 characters long – Letters, digits, special characters ?.@_$% – Embeded blanks are not allowed

W.P.Fang Department of CSIE YUST, Copy Right Reserved

75

Operation feilds • Operands field – An instruction may have zero, one , or two operand – Ex: – NOP – INC AX – ADD WORD1,2

W.P.Fang Department of CSIE YUST, Copy Right Reserved

76

Comment fields • A semicolon marks the beginning of this field • The assembler ignores anything typed after the semicolon. • Ex: – MOV CX,0 ;move 0 to CX

• Permissible to make an entire line a comment W.P.Fang Department of CSIE YUST, Copy Right Reserved

77

Program data • The processor operates only on binary data • The assembler must translate all data representation into binary number

W.P.Fang Department of CSIE YUST, Copy Right Reserved

78

NUMBER 11011 11011B 64223 -21843D 1,234 1B4DH 1B4D FFFH 0FFFFH

TYPE Decimal Binary Decimal Decimal Illegal– contains a nondigit character Hex Illegal hex number – doesn’t end in “H” Illegal hex number – doesn’t begin with a decimal digit Hex W.P.Fang Department of CSIE YUST, Copy Right Reserved

79

characters Pseudo-op

Stands for

DB

Define byte

DW

Define word

DD

Define double word

DQ

Define quaword

DT

Define tenbytes W.P.Fang Department of CSIE YUST, Copy Right Reserved

80

• Byte variable – Name DB initial_value

• Ex: – ALPHA

DB

4

W.P.Fang Department of CSIE YUST, Copy Right Reserved

81

• Word variable – Name DW initial_value

• Ex: – WRD

DW -2

W.P.Fang Department of CSIE YUST, Copy Right Reserved

82

• Array – B_ARRAY

DB

10H,20H,30H

• String – LETTERS DB ‘ABC’ – LETTERS DB 41H,42H,43H – MSG DB ‘HELLO’,0AH,0DH,’$’

W.P.Fang Department of CSIE YUST, Copy Right Reserved

83

• Named Constanted – EQU (Equates) – Name EQU constant

• Ex: – LF EQU 0AH – PROMPT EQU ‘TYPE YOUR NAME’

W.P.Fang Department of CSIE YUST, Copy Right Reserved

84

variables • • • • •

Byte variables Name DB initial_value Ex: ALPHA DB BYT DB ?

W.P.Fang Department of CSIE YUST, Copy Right Reserved

85

Word variables • Name • Ex:

DW initial_value

– WRD DW -2

W.P.Fang Department of CSIE YUST, Copy Right Reserved

86

array • B_ARRAY DB 10H,20H,30H Symbol

Address

contents

B_ARRAY

200h

10h

B_ARRAY+1 201h

20h

B_ARRAY

30h

202h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

87

Character string • • • •

LETTERS DB ‘ABC’ LETTERS DB 41H,42H,43H MSG DB ‘HELLO’,0AH,0DH,’$’ MSG DB 48H,45H,4CH,4CH,4FH,0AH,0DH,24H

W.P.Fang Department of CSIE YUST, Copy Right Reserved

88

Names constants • Name • Ex: – – – – –

EQU

constant

LF EQU 0AH MOV DL,0AH MOV DL,LF Prompt EQU ‘TYPE YOUR NAME’ MSG DB PROMPT

• Note:no memory is allocated for EQU names W.P.Fang Department of CSIE YUST, Copy Right Reserved

89

A few basic instructions • • • • • • •

MOV XCHG ADD SUB INC DEC NEG W.P.Fang Department of CSIE YUST, Copy Right Reserved

90

• MOV destination,source General Destination register operand Source operand

Segment register

Memory location

constant

General register

yes

Yes

Yes

No

Segment register

Yes

No

Yes

No

Memory location

Yes

Yes

No

No

Constant

Yes

W.P.Fang Department of CSIE YUST, Copy Right Reserved

No

Yes

91

no

• XCHG destination, source Destination General operand register Source operand Generation Yes register

Memory location

Memory location

no

Yes W.P.Fang Department of CSIE YUST, Copy Right Reserved

Yes

92

• XCHG AH,BL • XCHG AX,WORD1

1A AH 00 BH

00 AL 05 BL

05 AH 00 BH W.P.Fang Department of CSIE YUST, Copy Right Reserved

00 AL 1A BL 93

Restrictions on MOV and XCHG • MOV or XCHG between memory location is not allowed • Ex: MOV WORD1,WORD2 ; illegal • MOV AX,WORD2 • MOV WORD1,AX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

94

• ADD destination, source • SUB destination, source Destination General register operand Source operand

Memory location

General register

Yes

Yes

Memory location

Yes

No

Constant

Yes

yes

W.P.Fang Department of CSIE YUST, Copy Right Reserved

95

• INC destination • DEC destination • Ex: – INC WORD1 – DEC BYTE1

W.P.Fang Department of CSIE YUST, Copy Right Reserved

96

• NEG destination • By two’s complement • Ex: – NEG BX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

97

Type agreement of operands • The operands of the preceding twooperand instruction must be of the same type.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

98

Translation of High-Level language to assembly labguage • B=A – MOV AX,A – MOV B,AX

• A=5-A – MOV AX,5 – SUB AX,A – MOV A,AX – NEG A – ADD A,5 W.P.Fang Department of CSIE YUST, Copy Right Reserved

99

• A=B-2*A – – – –

MOV SUB SUB MOV

AX,B AX,A AX,A A,AX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

100

• .MODEL

memory_model

W.P.Fang Department of CSIE YUST, Copy Right Reserved

101

Program structure (Memory model) Model

Description

SMALL

Code in one segment Data in one segment

MEDIUM

Code in more than one segment Data in one segment

COMPACT

Code one segment Data in more than one segment

LARGE

Code in more than one segment Data in more than one segment No array larger than 64k bytes

HUGE

Code in more than one segment Data in more one segment Arrays may be larger than 64k bytes

W.P.Fang Department of CSIE YUST, Copy Right Reserved

102

Data segment • • • • • •

Contains all the variable definitions .DATA WORD1 DW2 WORD2 DW 5 MSG DB ‘THIS IS A MESSAGE’ MASK EQU 10010010B

W.P.Fang Department of CSIE YUST, Copy Right Reserved

103

Stack segment • .STACK • Ex: – .STACK

size 100H

• Ps: if size is omitted 1KB is set aside for the stack data

W.P.Fang Department of CSIE YUST, Copy Right Reserved

104

Code segment • .CODE name • There is no need for a name in a SMALL program • Name PROC • ;body of the procedure • Name ENDP W.P.Fang Department of CSIE YUST, Copy Right Reserved

105

• Ex: – – – – –

.CODE MAIN PROC ;main procedure instructions MAIN ENDP ;other procedures go here

W.P.Fang Department of CSIE YUST, Copy Right Reserved

106

Putting it together • • • • • • • • • •

.MODEL SMALL .STACK 100H .DATA ;data definitions go here .CODE MAIN PROC ;instructions go here MAIN ENDP ;other procedures go here END MAIN W.P.Fang Department of CSIE YUST, Copy Right Reserved

107

Input and output instructions • IN • OUT • Most applications programs do not use IN and OUT because – Port addresses vary among computer model – It’s much easier to program I/O with the service routines provided by the manufacturer

W.P.Fang Department of CSIE YUST, Copy Right Reserved

108

INT instruction • INT interrupt_number

W.P.Fang Department of CSIE YUST, Copy Right Reserved

109

INT 21h Function number

routine

1

Single-key input

2

Single-character output

9

Character string outpur

W.P.Fang Department of CSIE YUST, Copy Right Reserved

110

INT 21h Function 1: • Single-key input • Input: – Ah=1

• Output: • AL=ASCII code if character key is pressed =0 if non-character key is pressed

W.P.Fang Department of CSIE YUST, Copy Right Reserved

111

• MOV AH,1 • INT 21h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

112

INT 21h Function 2: • Display a character or execute a control function: – Ah=2 – DL=ASCII code of the display character or control character

• Output: • AL=ASCII code of the display character or control character W.P.Fang Department of CSIE YUST, Copy Right Reserved

113

• MOV Ah,2 • MOV DL,’?’ • INT 21h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

114

ASCII code (HEX) 7

Symbol

Function

BEL

8

BS

Beep (sounds a tone) Backspace

9

HT

Tab

A

LF

D

CR W.P.Fang Department of CSIE

Line feed (new line) Carriage return115 (start of current

YUST, Copy Right Reserved

INT 21h Function 9: • Display a string: – DX=offset address of string – The string must end with a ‘$’character.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

116

• Ex: – MSG DB ‘Hello!$’

W.P.Fang Department of CSIE YUST, Copy Right Reserved

117

LEA • INT21h, function 9, expects the offset address of the character string to be DX • LEA destination, source • EX: – LEA DX,MSG

W.P.Fang Department of CSIE YUST, Copy Right Reserved

118

Program segment prefix • When a program is loaded in memory, DOS prefaces it with a 256 byte program segment prefix(PSP) • PSP contains information about the program, • DOS places its segment number in both DS and ES before executing the program • The result is that DS does not contain the segment number of the data segment W.P.Fang Department of CSIE YUST, Copy Right Reserved

119

• MOV AX,@DATA • MOV DS,AX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

120

Terminating a program • INT 32h function 4Ch

W.P.Fang Department of CSIE YUST, Copy Right Reserved

121

TITLE PGM4_1:ECHO PROGRAM .MODEL SMALL .STACK 100H .CODE MAIN PROC MOV AH,2 MOV DL, '?' INT 21H MOV AH,1 INT 21H MOV BL,AL MOV AH,2 MOV DL,0AH INT 21H MOV DL,BL INT 21h MOV AH,4CH INT 21h MAIN ENDP END MAIN W.P.Fang Department of CSIE YUST, Copy Right Reserved

122

TITLE PGM4_2:PRINT STRING PROGRAM .MODEL SMALL .STACK 100H .DATA MSG DB ‘HELLO!$’ .CODE MAIN PROC MOV AX,@DATA MOV DS,AX LEA DX,MSG MOV AH,9 INT 21h MOV AH,4CH INT 21h MAIN ENDP END MAIN

W.P.Fang Department of CSIE YUST, Copy Right Reserved

123

TITLE PGM4_3: CASE CONVERSION PROGRAM .MODEL SMALL .STACK 100H .DATA CR EQU 0DH LF EQU 0AH MSG1 DB ‘ENTER A LOWER CASE LETTER: $’ MSG2 DB 0DH,0AH,’IN UPPER CASE IT IS: ‘ CHAR DB ?,’$’ .CODE MAIN PROC MOV AX,@DATA MOV DS,AX LEA DX,MSG1 INT 21h MOV AH,1 INT 21h SUB AL,20h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

124

MOV CHAR,AL LEA DX,MSG2 MOV AH,9 INT 21h MOV AH,4CH INT 21h MAIN ENDP END MAIN

W.P.Fang Department of CSIE YUST, Copy Right Reserved

125

Exercise • Using only MOV,ADD,SUB,INC,DEC and NEG translate below into assembly (A,B,C are word) • A=B-A • A=-(A+1) • C=A+B • B=3*B+7 • A=B-A-1 W.P.Fang Department of CSIE YUST, Copy Right Reserved

126

homework • Ch4 –4 –8 – 12

• If programming exercise please print out code and result (hard copy) • If more than one page , please bind in left upper • Use A4 paper W.P.Fang Department of CSIE YUST, Copy Right Reserved

127

• Write a program to – Display a “?” – Read two decimal digits whose sum is less than 10 – Display them and their sum on the next line, with an appropriate message

• ?27 • THE SUM OF 2 AND 7 is 9 W.P.Fang Department of CSIE YUST, Copy Right Reserved

128

• Write a program to – Display “?” – Read three initials – Display them in the middle of an 11x11 box of asterisks – Beep the computer

W.P.Fang Department of CSIE YUST, Copy Right Reserved

129

The processor status and the FLAGS Register 15

14

13

12

11

10

9

8

7

6

OF

DF

IF

TF

SF

ZF

5

W.P.Fang Department of CSIE YUST, Copy Right Reserved

4

AF

3

2

PF

1

0

CF

130

• The status flags are located in bits 0,and 11 • Control flags are located in bits 8,9, and 10 • The other bits have no significance

W.P.Fang Department of CSIE YUST, Copy Right Reserved

131

Status flags • Reflect the result of an operation • Ex: – If SUB AX,AX is executed, the zero flag become 1

W.P.Fang Department of CSIE YUST, Copy Right Reserved

132

Status Flags bit

name

symbol

0

Carry flag

CF

2

Parity flag

PF

4

AF

6

Auxiliary carry flag Zero flag

7

Sign flag

SF

11

Overflow flag

OF

W.P.Fang Department of CSIE YUST, Copy Right Reserved

ZF

133

Control Flags bit

name

Symbol

8

Trap flag

TF

9

Interrupt flag

IF

10

Direction flag

DF

W.P.Fang Department of CSIE YUST, Copy Right Reserved

134

Carry flag • CF=1 if – There is a carry out from the most significant bit (msb) on addition – There is a borrow into themsb on subtraction – Affected by shift and rotate instruction

• Otherwise CF=0

W.P.Fang Department of CSIE YUST, Copy Right Reserved

135

Parity flags • PF=1 if – The low byte of a result has an even number of one bits

W.P.Fang Department of CSIE YUST, Copy Right Reserved

136

AuxiliRy carry flag • AF=1 if – There is a carry out from bit 3 on addition – Borrow into bit 3 on substraction – Used in binary-coded decimal (BCD) operations

W.P.Fang Department of CSIE YUST, Copy Right Reserved

137

Zero flag • ZF=1 for a zero result • ZF=0 for a nonezero result

W.P.Fang Department of CSIE YUST, Copy Right Reserved

138

Sign Flag • SF=1 if the msb of a result is 1 • Means the result is negative if giving a signed interpretation • SF=0 if the msb=0

W.P.Fang Department of CSIE YUST, Copy Right Reserved

139

Overflow Flag • OF=1 if signed overflow occurred

W.P.Fang Department of CSIE YUST, Copy Right Reserved

140

Overflow • Is associated with the fact that the range of numbers that be represented in a computer is limited.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

141

Overflow examples • Four possible outcome – – – –

No overflow Signed overflow Unsigned overflow Both signed and unsigned overflow

W.P.Fang Department of CSIE YUST, Copy Right Reserved

142

• Unsigned overflow but not signed overflow – AX=FFFFh – BX=0001h – ADD AX,BX 1111 1111 1111 1111 + 0000 0000 0000 0001 ----------------------------------1 0000 0000 0000 0000 Signed : -1 + 1=0 ,unsigned 65536 àoverflow W.P.Fang Department of CSIE YUST, Copy Right Reserved

143

• Signed but not unsigned overflow – AX=BX=7FFFh – ADD AX,BX 0111 1111 1111 1111 + 0111 1111 1111 1111 --------------------------------------1111 1111 1111 1110 32767+32767=65534 à -2 W.P.Fang Department of CSIE YUST, Copy Right Reserved

144

How the processor indicates overflow • OF=1 for signed overflow • CF=1 for unsigned overflow • In determining overflow, the processor does not interpret the result as either signed or unsigned. • Programmer shall interpreting the result

W.P.Fang Department of CSIE YUST, Copy Right Reserved

145

How the processor determines that overflow occurred • Limit the discussion to addition and substraction • Unsigned overflow – On addition,Means that the correct answer is larger than the biggest unsigned number – On substraction, means that the correct answer is smaller than 0

W.P.Fang Department of CSIE YUST, Copy Right Reserved

146

• Signed overflow – On addition • Signed overflow occurs when the sum has a different sign

– On substraction • • • • •

With different signed like adding number of the same sign. Ex: A-(-B)=A+B -A-(+B)=-A+ -B Occur if the result has a different sign than expected

W.P.Fang Department of CSIE YUST, Copy Right Reserved

147

• In addition of number with different signs, overflow is impossible • In substraction of numbers with the same sign cannot give overflow

W.P.Fang Department of CSIE YUST, Copy Right Reserved

148

• Processor uses the following method to set the OF – If the carries into and out of the msb don’t match – There is a carry out but no carry in – Then signed overflow has occurredà OF is set to 1

W.P.Fang Department of CSIE YUST, Copy Right Reserved

149

How Instructions affect the flags instruction

Affects flags

MOV/XCHG

None

ADD/SUB

All

INC/DEC

All except CF

NEG

All (CF=1 unless result is 0 OF=1 if word operand is 8000h Or byte operand is 80h W.P.Fang Department of CSIE YUST, Copy Right Reserved

150

example • ADD AX,BX, where AX contains FFFFh,BX contains FFFh

W.P.Fang Department of CSIE YUST, Copy Right Reserved

151

example • ADD AL,BL, where AL contains 80h,BL contains 80h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

152

example • SUB AX,BX, where AX contains 8000h and BX contains 0001h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

153

example • INC AL, where AL contain FFh

W.P.Fang Department of CSIE YUST, Copy Right Reserved

154

example • MOV AX,-5

W.P.Fang Department of CSIE YUST, Copy Right Reserved

155

example • NEG AX, where AX contains 8000h

W.P.Fang Department of CSIE YUST, Copy Right Reserved

156

The DEBUG Program • An environment in which a program may be tested.

W.P.Fang Department of CSIE YUST, Copy Right Reserved

157

TITLE PGM5_1:Check flags .MODELSMALL .STACK 100H .CODE MAIN PROC MOV AX,4000H ADD AX,AX SUB AX,0FFFFH NEG AX INC AX MOV AH,4CH INT 21H MAIN ENDP END MAIN

W.P.Fang Department of CSIE YUST, Copy Right Reserved

158

-r AX=0000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC 0B8E:0000 B80040 MOV AX,4000 -r AX=0000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC 0B8E:0000 B80040 MOV AX,4000 -t

AX=4000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0003 NV UP EI PL NZ NA PO NC 0B8E:0003 03C0 ADD AX,AX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

159

-t

AX=8000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0005 OV UP EI NG NZ NA PE NC 0B8E:0005 2DFFFF SUB AX,FFFF -t

AX=8001 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0008 NV UP EI NG NZ AC PO CY 0B8E:0008 F7D8 NEG AX -t

AX=7FFF BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000A NV UP EI PL NZ AC PE CY 0B8E:000A 40 INC AX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

160

AX=7FFF BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000A NV UP EI PL NZ AC PE CY 0B8E:000A 40 INC AX -t

AX=8000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000B OV UP EI NG NZ AC PE CY 0B8E:000B B44C MOV AH,4C -g

Program terminated normally -q

W.P.Fang Department of CSIE YUST, Copy Right Reserved

161

homework • Ch5 – – – –

1.e 2.b 3.d 4.e

W.P.Fang Department of CSIE YUST, Copy Right Reserved

162

Flow Control Instructions

W.P.Fang Department of CSIE YUST, Copy Right Reserved

163

Flow Control Instructions • The jump and loop instructions transfer control to another part of the program. • This transfer can be – Unconditional – Depend on a particular combination of status flag settings

W.P.Fang Department of CSIE YUST, Copy Right Reserved

164

Jump example • • • • • • • • • • • • • • • • •

TITLE PGM6_1:IBM CHARACTER DISPLAY .MODEL SMALL .STACK 100H .CODE MAIN PROC MOV AH,2 MOV CX,256 MOV DL,0 PRINT_LOOP: int 21h INC DL DEC CX JNE PRINT_LOOP MOV AH,4CH INT 21h MAIN ENDP END MAIN

W.P.Fang Department of CSIE YUST, Copy Right Reserved

165

W.P.Fang Department of CSIE YUST, Copy Right Reserved

166

• JNZ:jump if not zero • CX:loop counter • PRINT_LOOP:label

W.P.Fang Department of CSIE YUST, Copy Right Reserved

167

Conditional jump • Jxxx

destination_label

W.P.Fang Department of CSIE YUST, Copy Right Reserved

168

Conditional jumps • Range of a conditional jump – Precede no more than 126 bytes – Follow it by no more than 127 bytes

W.P.Fang Department of CSIE YUST, Copy Right Reserved

169

How the CPU implements a conditional jump • CPU looks at the flags register • If the conditions for the jump are true, the CPU adjusts the IP to point to the destination label. • If the condition is false, the IP is not altered

W.P.Fang Department of CSIE YUST, Copy Right Reserved

170

• JNZ – Inspecting ZF • If ZF=0,transfer to label • If ZF=1, next line

W.P.Fang Department of CSIE YUST, Copy Right Reserved

171

Conditional jumps • Three categories – The signed jumps – Unsigned jumps – Single-flag jumps

• Ps: jump instruction do not affect the flags

W.P.Fang Department of CSIE YUST, Copy Right Reserved

172

Signed jumps symbol

description

Condition for jumps

JG/JNLE

Jump if greater than Jump if not less than or equal to

ZF=0 and SF=OF

JGE/JNL

Jump if greater than or equal to Jump if not less than or equal to

SF=OF

JL/JNGE

Jump if less than Jump if not greater than or equal

SFOF

JLE/JNG

Jump if less than or equal W.P.Fang Department of CSIE Jump if not great than YUST, Copy Right Reserved

ZF=1 or SFOF 173

Unsigned conditional jumps Symbol

description

Condition for jumps

JA/JNBE

Jump if above Jump if not below or equal

CF=0 and ZF=0

JAE/JNB

Jump if above or equal Jump if not below

CF=0

JB/JNAE

Jump if below Jump if not above or equal

CF=1

JBE/JNA

Jump if equal Jump if not above

CF=1 or ZF=1

W.P.Fang Department of CSIE YUST, Copy Right Reserved

174

Single-Flag jumps symbol

description

Condition for jumps

JE/JZ

Jump if equal Jump if equal to zero

ZF=1

JNE/JNZ

jump if not equal Jump if not zero

ZF=0

JC

Jump if carry

CF=1

JNC

Jump if no carry

CF=0

JO

Jump if overflow

OF=1

JNO

Jump if no overflow

OF=0

JS

Jump if sign negative

SF=1

JNS

Jump if nonnegative sign SF=0

JP/JPE

Jump if parity even

PF=1

JNP/JPO

W.P.Fang Department Jump if parity odd of CSIE

PF=0

YUST, Copy Right Reserved

175

The CMP instruction • CMP destination , source • Compares destination and source by computing (destination)-(source) • the result is not stored, but flags are affected • May not both memory locations • Destination may not be a constant W.P.Fang Department of CSIE YUST, Copy Right Reserved

176

• • • • •

CMP AX,BX JG BELOW Where AX=7,BX=0001 Result is 7FFFh-0001=7FFEh ZF=SF=OF=0

W.P.Fang Department of CSIE YUST, Copy Right Reserved

177

• DEC AX • JL THERE

W.P.Fang Department of CSIE YUST, Copy Right Reserved

178

Signed versus unsigned jumps • Each of the signed jumps corresponds to an analogous unsigned jump • Using the wrong kind of jump can lead to incorrect results

W.P.Fang Department of CSIE YUST, Copy Right Reserved

179

• Ex: – – – –

If AX=7FFFh BX=8000h CMP AX,BX JA BELOW

• Because 7FFFh8000h W.P.Fang Department of CSIE YUST, Copy Right Reserved

180

Working with Characters • Example. Suppose AX and BX contain signed numbers, write some code to put the biggest one in CX • Solution: – – – – –

MOV CX,AX CMP BX,CX JLE NEXT MOVE CX,BX NEXT: W.P.Fang Department of CSIE YUST, Copy Right Reserved

181

The JMP instruction • JMP destination • JMP can be used to get around the range restriction of a conditional jump

W.P.Fang Department of CSIE YUST, Copy Right Reserved

182

• • • • •

TOP: DEC CX JNZ TOP MOV AX,BX

W.P.Fang Department of CSIE YUST, Copy Right Reserved

183

• TOP: – DEC CX – JNZ BOTTOM – JMP EXIT

• BOTTOM: – JMP TOP

• EXIT: – MOV AX,BX W.P.Fang Department of CSIE YUST, Copy Right Reserved

184

High-Level Language structure • • • • • •

IF-THEN IF-THEN-ELSE CASE Branch with compound conditions AND conditions OR conditions

W.P.Fang Department of CSIE YUST, Copy Right Reserved

185

IF-THEN • IF condition is true • THEN – Execute true-branch statements

• END_IF

W.P.Fang Department of CSIE YUST, Copy Right Reserved

186

• Ex :replace the number in AX by its absolute value

W.P.Fang Department of CSIE YUST, Copy Right Reserved

187

• IF AX
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF