Assembly Language
-
Upload
oscar-said-villa-saucedo -
Category
Documents
-
view
32 -
download
2
Transcript of Assembly Language
W.P.Fang Department of CSIE YUST, Copy Right Reserved
1
Assembly Language
W.P.fang
W.P.Fang Department of CSIE YUST, Copy Right Reserved
2
Reference
• Ytha Yu, Charles Marut, Assembly Language Programming and Organziationof the IBM PC
W.P.Fang Department of CSIE YUST, Copy Right Reserved
3
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
4
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
5
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
6
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
7
schdular
987654321
備註進度日期
W.P.Fang Department of CSIE YUST, Copy Right Reserved
8
schdular
181716151413121110
備註進度日期
W.P.Fang Department of CSIE YUST, Copy Right Reserved
9
Microcomputer Systems
W.P.Fang Department of CSIE YUST, Copy Right Reserved
10
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
11
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
12
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
13
Microcomputer System(component)
• Bytes – eight bits• Address – identified by a number • Content– the data stored in a memory byte.
content:
0 1 1 0 0 0 0 10 1 0 1 1 1 1 00 0 0 0 0 0 0 00 1 1 0 1 1 1 0
Address:3210
W.P.Fang Department of CSIE YUST, Copy Right Reserved
14
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
15
Microcomputer System(component)
• Word – typical microcomputer, two bytes form a word
• Bit position
Byte bit position
Word bit position
7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
0
08
High byte Low byte
W.P.Fang Department of CSIE YUST, Copy Right Reserved
16
Microcomputer system(component)
• RAM and ROM• Bus
W.P.Fang Department of CSIE YUST, Copy Right Reserved
17
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
18
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
19
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
20
Microcomputer system(component)
• I/O ports
W.P.Fang Department of CSIE YUST, Copy Right Reserved
21
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
22
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
23
Microcomputer system(I/O Devices)
• Magnetics disks• Keyboard• Display monitor• printers
W.P.Fang Department of CSIE YUST, Copy Right Reserved
24
Microcomputer system(programming languages)
• Machine language
Store the content of AX in memory word 0
10100011 00000000 00000000
Add 4 to AX00000101 00000100 00000000
Fetch the content of memory word to and put it in register AX
10100001 00000000 00000000
operationMachine instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
25
Microcomputer system(programming languages)
• Assembly language
move the content of AX into location A
MOV A,AX
Add 4 to AXADD AX,4
Fetch the content of location A and put it in register AX
MOV AX,A
operationAssembly language instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
26
Microcomputer system(programming languages)
• Assembler• compiler• High-level language
W.P.Fang Department of CSIE YUST, Copy Right Reserved
27
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
28
Microcomputer system(assembly languages)
ch1.asmTITLE PGM1_1: SAMPLE PROGRAM.MODEL SMALL.STACK 100H.DATAA DW 2B DW 5SUM DW ?.CODEMAIN PROC;initialize DS
MOV AX,@DATAMOV DS,AX
;add the numbersMOV AX,AADD AX,BMOV SUM,AX
;exit to DosMOV AX,4C00HINT 21H
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
Glossary
• System board• Video adapter• word
W.P.Fang Department of CSIE YUST, Copy Right Reserved
40
Representation of numbers and characters
W.P.Fang Department of CSIE YUST, Copy Right Reserved
41
Representation of numbers and characters
• Please refer to bcc
W.P.Fang Department of CSIE YUST, Copy Right Reserved
42
• Binary number system
W.P.Fang Department of CSIE YUST, Copy Right Reserved
43
• 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
44
• 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
45
ASCII code
• American Standard Code for Information Interchange
W.P.Fang Department of CSIE YUST, Copy Right Reserved
46
Homework
• Chap 2– 2.– 7.d– 8.c– The rule of ascii
W.P.Fang Department of CSIE YUST, Copy Right Reserved
47
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
48
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
49
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
50
Organization of the IBM Personal Computers
W.P.Fang Department of CSIE YUST, Copy Right Reserved
51
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
52
The intel x86 family
• 1978 8086 16 bit• 80186 extended instruction set• 80286 real address mode,protected virtual
address mode,multitasking,moreaddressable memory,2^30 bytes
• 80386 32 bit virtual 8086 mode/386 SX à16bit bus
W.P.Fang Department of CSIE YUST, Copy Right Reserved
53
Register
• Data register• Address register• Status register• Segment• Pointer• Index register
W.P.Fang Department of CSIE YUST, Copy Right Reserved
54
• Ax• Bx• Cx• Dx• Cs• Ds• Ss• Es• Si• Di• Sp• Bp• Ip• flag
W.P.Fang Department of CSIE YUST, Copy Right Reserved
55
Data register
• AX(accumulator)• BX(base register)• CX(count register)• DX(data register)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
56
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
57
• Memory segment• Segment:offset address
– Because 20bit can not represent in 16 bit
W.P.Fang Department of CSIE YUST, Copy Right Reserved
58
Logical address
• Ex:• A4FB:4872h• A4FB0+4872h=A9822h(20-bit physical
address)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
59
• 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
60
• 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
61
• 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
62
• Physical address=segmentx10h+offset• Segmentx10h=physical address-offset• 80FD2h-BFD2h=75000h• àsegment 7500h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
63
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
64
• 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
65
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
66
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
67
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
68
• Dos routine that services user commands is called COMMAND.COM
W.P.Fang Department of CSIE YUST, Copy Right Reserved
69
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
70
Memory
00000hIntreeupt vectors00400hBios and dos data
Dos
Application program area
A0000hVideoB0000hVideoC0000hReservedD0000hReservedE0000hReservedF0000hBios
W.P.Fang Department of CSIE YUST, Copy Right Reserved
71
Serial port (COM1)3f8h-3ffh
CGA3d0h-3dfh
EGA3c0h-3cfh
Parallel printer port 1378h-37fh
Hard disk320h-32fh
Serial port (COM2)2f8h-2ffh
Game controller200h-20fh
Keyboard controller60h-63h
Interrupt controller20h-21h
descriptionPort address
W.P.Fang Department of CSIE YUST, Copy Right Reserved
72
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
73
Introduction to IBM PC Assembly Language
W.P.Fang Department of CSIE YUST, Copy Right Reserved
74
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
75
• 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
76
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
77
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
78
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
79
Hex0FFFFH
Illegal hex number – doesn’t begin with a decimal digit
FFFH
Illegal hex number – doesn’t end in “H”
1B4DHex1B4DHIllegal– contains a nondigit character1,234Decimal-21843DDecimal64223Binary11011BDecimal11011TYPENUMBER
W.P.Fang Department of CSIE YUST, Copy Right Reserved
80
characters
Define tenbytesDT
Define quawordDQ
Define double wordDD
Define wordDW
Define byteDB
Stands forPseudo-op
W.P.Fang Department of CSIE YUST, Copy Right Reserved
81
• Byte variable– Name DB initial_value
• Ex:– ALPHA DB 4
W.P.Fang Department of CSIE YUST, Copy Right Reserved
82
• Word variable– Name DW initial_value
• Ex:– WRD DW -2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
83
• 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
84
• 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
85
variables
• Byte variables• Name DB initial_value• Ex:• ALPHA DB• BYT DB ?
W.P.Fang Department of CSIE YUST, Copy Right Reserved
86
Word variables
• Name DW initial_value• Ex:
– WRD DW -2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
87
array
• B_ARRAY DB 10H,20H,30H
30h202hB_ARRAY
20h201hB_ARRAY+1
10h200hB_ARRAY
contentsAddressSymbol
W.P.Fang Department of CSIE YUST, Copy Right Reserved
88
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
89
Names constants
• Name EQU constant• Ex:
– 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
90
A few basic instructions
• MOV• XCHG• ADD• SUB• INC• DEC• NEG
W.P.Fang Department of CSIE YUST, Copy Right Reserved
91
• MOV destination,source
noYesNoYesConstant
NoNoYesYesMemory location
NoYesNoYesSegment register
NoYesYesyesGeneral register
constantMemory location
Segment register
General registerDestination
operandSourceoperand
W.P.Fang Department of CSIE YUST, Copy Right Reserved
92
• XCHG destination, source
noYesMemory location
YesYesGeneration register
Memory location
General register
Destination operand
Source operand
W.P.Fang Department of CSIE YUST, Copy Right Reserved
93
• XCHG AH,BL• XCHG AX,WORD1
1A 00
00 05
AH AL
BH BL
05 00
00 1A
AH AL
BH BL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
94
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
95
• ADD destination, source• SUB destination, source
yesYesConstant
NoYesMemory location
YesYesGeneral register
Memory locationGeneral registerDestination operand
Source operand
W.P.Fang Department of CSIE YUST, Copy Right Reserved
96
• INC destination• DEC destination• Ex:
– INC WORD1– DEC BYTE1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
97
• NEG destination• By two’s complement• Ex:
– NEG BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
98
Type agreement of operands
• The operands of the preceding two-operand instruction must be of the same type.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
99
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
100
• A=B-2*A– MOV AX,B– SUB AX,A– SUB AX,A– MOV A,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
101
• .MODEL memory_model
W.P.Fang Department of CSIE YUST, Copy Right Reserved
102
Program structure (Memory model)
Code in more than one segmentData in more one segmentArrays may be larger than 64k bytes
HUGE
Code in more than one segmentData in more than one segmentNo array larger than 64k bytes
LARGE
Code one segmentData in more than one segment
COMPACT
Code in more than one segmentData in one segment
MEDIUM
Code in one segmentData in one segment
SMALL
DescriptionModel
W.P.Fang Department of CSIE YUST, Copy Right Reserved
103
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
104
Stack segment
• .STACK size• Ex:
– .STACK 100H
• Ps: if size is omitted 1KB is set aside for the stack data
W.P.Fang Department of CSIE YUST, Copy Right Reserved
105
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
106
• Ex:– .CODE– MAIN PROC– ;main procedure instructions– MAIN ENDP– ;other procedures go here
W.P.Fang Department of CSIE YUST, Copy Right Reserved
107
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
108
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
109
INT instruction
• INT interrupt_number
W.P.Fang Department of CSIE YUST, Copy Right Reserved
110
INT 21h
Character string outpur9
Single-character output2
Single-key input1
routineFunction number
W.P.Fang Department of CSIE YUST, Copy Right Reserved
111
INT 21hFunction 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
112
• MOV AH,1• INT 21h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
113
INT 21hFunction 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
114
• MOV Ah,2• MOV DL,’?’• INT 21h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
115Carriage return (start of current
CRD
Line feed (new line)
LFA
TabHT9
BackspaceBS8
Beep (sounds a tone)
BEL7
FunctionSymbolASCII code (HEX)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
116
INT 21hFunction 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
117
• Ex:– MSG DB ‘Hello!$’
W.P.Fang Department of CSIE YUST, Copy Right Reserved
118
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
119
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
120
• MOV AX,@DATA• MOV DS,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
121
Terminating a program
• INT 32h function 4Ch
W.P.Fang Department of CSIE YUST, Copy Right Reserved
122
TITLE PGM4_1:ECHO PROGRAM.MODEL SMALL.STACK 100H.CODEMAIN PROC
MOV AH,2MOV DL, '?'
INT 21HMOV AH,1INT 21HMOV BL,ALMOV AH,2MOV DL,0AHINT 21HMOV DL,BLINT 21hMOV AH,4CHINT 21h
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
123
TITLE PGM4_2:PRINT STRING PROGRAM.MODEL SMALL.STACK 100H.DATAMSG DB ‘HELLO!$’.CODEMAIN PROC
MOV AX,@DATAMOV DS,AXLEA DX,MSGMOV AH,9INT 21hMOV AH,4CHINT 21h
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
124
TITLE PGM4_3: CASE CONVERSION PROGRAM.MODEL SMALL.STACK 100H.DATA
CR EQU 0DHLF EQU 0AH
MSG1 DB ‘ENTER A LOWER CASE LETTER: $’MSG2 DB 0DH,0AH,’IN UPPER CASE IT IS: ‘CHAR DB ?,’$’.CODEMAIN PROC
MOV AX,@DATAMOV DS,AXLEA DX,MSG1INT 21hMOV AH,1INT 21hSUB AL,20h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
125
MOV CHAR,ALLEA DX,MSG2MOV AH,9INT 21hMOV AH,4CHINT 21h
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
126
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
127
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
128
• 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
129
• 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
130
The processor status and the FLAGS Register
CF
0
PFAFZFSFTFIFDFOF
123456789101112131415
W.P.Fang Department of CSIE YUST, Copy Right Reserved
131
• 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
132
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
133
Status Flags
OFOverflow flag11
SFSign flag7
ZFZero flag6
AFAuxiliary carry flag
4
PFParity flag2
CFCarry flag0
symbolnamebit
W.P.Fang Department of CSIE YUST, Copy Right Reserved
134
Control Flags
DFDirection flag10
IFInterrupt flag9
TFTrap flag8
Symbolnamebit
W.P.Fang Department of CSIE YUST, Copy Right Reserved
135
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
136
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
137
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
138
Zero flag
• ZF=1 for a zero result• ZF=0 for a nonezero result
W.P.Fang Department of CSIE YUST, Copy Right Reserved
139
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
140
Overflow Flag
• OF=1 if signed overflow occurred
W.P.Fang Department of CSIE YUST, Copy Right Reserved
141
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
142
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
143
• 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
144
• Signed but not unsigned overflow– AX=BX=7FFFh– ADD AX,BX
0111 1111 1111 1111+ 0111 1111 1111 1111---------------------------------------
1111 1111 1111 111032767+32767=65534 à -2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
145
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
146
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
147
• 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
148
• 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
149
• 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
150
How Instructions affect the flags
All (CF=1 unless result is 0OF=1 if word operand is 8000hOr byte operand is 80h
NEG
All except CFINC/DEC
AllADD/SUB
NoneMOV/XCHG
Affects flagsinstruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
151
example
• ADD AX,BX, where AX contains FFFFh,BX contains FFFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
152
example
• ADD AL,BL, where AL contains 80h,BL contains 80h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
153
example
• SUB AX,BX, where AX contains 8000h and BX contains 0001h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
154
example
• INC AL, where AL contain FFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
155
example
• MOV AX,-5
W.P.Fang Department of CSIE YUST, Copy Right Reserved
156
example
• NEG AX, where AX contains 8000h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
157
The DEBUG Program
• An environment in which a program may be tested.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
158
TITLE PGM5_1:Check flags.MODELSMALL.STACK 100H.CODEMAIN PROC
MOV AX,4000HADD AX,AXSUB AX,0FFFFHNEG AXINC AX
MOV AH,4CHINT 21H
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
159
-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
160
-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
161
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
162
homework
• Ch5– 1.e– 2.b– 3.d– 4.e
W.P.Fang Department of CSIE YUST, Copy Right Reserved
163
Flow Control Instructions
W.P.Fang Department of CSIE YUST, Copy Right Reserved
164
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
165
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
166
W.P.Fang Department of CSIE YUST, Copy Right Reserved
167
• JNZ:jump if not zero• CX:loop counter• PRINT_LOOP:label
W.P.Fang Department of CSIE YUST, Copy Right Reserved
168
Conditional jump
• Jxxx destination_label
W.P.Fang Department of CSIE YUST, Copy Right Reserved
169
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
170
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
171
• JNZ– Inspecting ZF
• If ZF=0,transfer to label• If ZF=1, next line
W.P.Fang Department of CSIE YUST, Copy Right Reserved
172
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
173
Signed jumps
ZF=1 or SF<>OFJump if less than or equalJump if not great than
JLE/JNG
SF<>OFJump if less thanJump if not greater than or equal
JL/JNGE
SF=OFJump if greater than or equal toJump if not less than or equal to
JGE/JNL
ZF=0 and SF=OFJump if greater thanJump if not less than or equal to
JG/JNLE
Condition for jumpsdescriptionsymbol
W.P.Fang Department of CSIE YUST, Copy Right Reserved
174
Unsigned conditional jumps
CF=1 or ZF=1Jump if equalJump if not above
JBE/JNA
CF=1Jump if belowJump if not above or equal
JB/JNAE
CF=0Jump if above or equalJump if not below
JAE/JNB
CF=0 and ZF=0Jump if aboveJump if not below or equal
JA/JNBE
Condition for jumpsdescriptionSymbol
W.P.Fang Department of CSIE YUST, Copy Right Reserved
175
Single-Flag jumps
PF=0Jump if parity oddJNP/JPO
PF=1Jump if parity evenJP/JPE
SF=0Jump if nonnegative signJNS
SF=1Jump if sign negativeJS
OF=0Jump if no overflowJNO
OF=1Jump if overflowJO
CF=0Jump if no carryJNC
CF=1Jump if carryJC
ZF=0jump if not equalJump if not zero
JNE/JNZ
ZF=1Jump if equalJump if equal to zero
JE/JZ
Condition for jumpsdescriptionsymbol
W.P.Fang Department of CSIE YUST, Copy Right Reserved
176
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
177
• 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
178
• DEC AX• JL THERE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
179
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
180
• Ex:– If AX=7FFFh– BX=8000h– CMP AX,BX– JA BELOW
• Because 7FFFh<8000h in an unsigned sense àdoes not jump to BELOW
• Ps: in signed 7FFFh>8000h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
181
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
182
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
183
• TOP:• <<if too far>>• DEC CX• JNZ TOP• MOV AX,BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
184
• TOP:– DEC CX– JNZ BOTTOM– JMP EXIT
• BOTTOM:– JMP TOP
• EXIT:– MOV AX,BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
185
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
186
IF-THEN
• IF condition is true• THEN
– Execute true-branch statements
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
187
• Ex :replace the number in AX by its absolute value
W.P.Fang Department of CSIE YUST, Copy Right Reserved
188
• IF AX<0• THEN
– Replace AX by –AX
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
189
• CMP AX,0– JNL END_IF– NEG AX
• END_IF:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
190
IF-THEN-ELSE
• IF condition is true– THEN
• Execute true-branch statements
– ELSE• Execute false-branch statements
– END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
191
• Ex: suppose AL and BL contain extended ASCII characters. Display the one the comes first in the character sequence
W.P.Fang Department of CSIE YUST, Copy Right Reserved
192
• IF AL<=BL– THEN
• Display the character in AL
– ELSE• Display the character in BL
– END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
193
– MOV AH,2– CMP AL,BL– JNBE ELSE_– MOV DL,AL– JMP DISPLAY
• ELSE_:– MOV DL,BL
• DISPLAY:– INT 21h
• END_IF• Ps: the label ELSE_ is used because ELSE is a reserved
word.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
194
Exercise• Write assembly code for each of the following decision
structures• a.
– IF AX<0– THEN
• PUT -1 IN BX– END_IF
• b.• IF AL<0
– THEN put FFh in AH• Put FFh in AH
– ELSE• Put 0 in AH
– END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
195
Exercise
• C.• Suppose DL contains the ASCII code oa a
character• (IF DL>=“A”) AND (DL<= ‘Z’)• THEN
– Display DL
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
196
• D.– If AX<BX
• THEN– IF BX<CX
» THEN» Put 0 in AX
– ELSE» Put 0 in BX
– END_IF
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
197
• E• IF(AX<BX) OR (BX<CX)
– THEN• Put 0 in BX
– ELSE• Put 1 in DX
– END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
198
HW
• Ch6. 3a,12
W.P.Fang Department of CSIE YUST, Copy Right Reserved
199
CASE
• Multiway branch structure• CASE expression
– Value1:statement 1– Value2:statement 2– :– :– Valuen:statement n
• END_CASE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
200
ex
• If AX contains a negative number, put -1 in BX;if AX contains 0, put 0 in BX;if AX contains a positive number,put 1 in BX.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
201
• CASE AX– <0:put -1 in BX– =0:put 0 in BX– >0:put 1 in BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
202
– CMP AX,0– JL NEGATIVE– JE ZERO– JG POSITIVE
• NEGATIVE:– MOV BX,-1– JMP END_CASE
• ZERO:– MOV BX,0– JMP END_CASE
• POSITIVE:– MOV BX,1
• END_CASE:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
203
ex
• If AL contains 1 or 3, display “0”; if AL contains 2 or 4, display “e”
W.P.Fang Department of CSIE YUST, Copy Right Reserved
204
• CASE AL– 1,3:display ‘o’– 2,4:display ‘e’
W.P.Fang Department of CSIE YUST, Copy Right Reserved
205
– CMP AL,1– JE ODD– CMP AL,3– JE ODD– CMP Al,2– JE EVEN– CMP AL,4– JE EVEN– JMP END_CASE
• ODD:– MOV DL,’o’– JMP DISPLAY
• EVEN:– MOV DL,’e’
• DISPLAY:– MOV AH,2– INT 21H
• END_CASE:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
206
Branches with compound conditions
• Condition_1 AND condition_2• Comdition_1 OR condition_2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
207
AND conditions
• Ex:read a character, and if it’d an uppercase letter,display it
W.P.Fang Department of CSIE YUST, Copy Right Reserved
208
• Read a character (into AL)• IF (‘A’<= character) and (character <=‘Z’)• THEN
– Display character
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
209
– MOV AH,1– INT 21H– CMP AL.’A’– JNGE END_IF– CMP AL,’Z’– JNLE END_IF– MOV DL,AL– MOV AH,2– INT 21H
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
210
OR conditions
• Read a character. IF it’s “y”or “Y”, display it; otherwise, terminate the program.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
211
• Read a character (into AL)• IF (character=‘y’) OR (character=‘Y’)
– THEN• Display it
– ELSE• Terminate the program
– END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
212
– MOV AH,1– INT 21H– CMP AL.’y’– JE THEN– CMP AL,’Y’– JE THEN– JMP ELSE
• THEN:– MOV AH,2– MOV DL,AL– INT 21H– JMP END_IF
• ELSE_:– MOV AH,4CH– INT 21H
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
213
Looping structures
• FOR LOOP• WHILE LOOP• REPEAT LOOP• WHILE versus REPEAT
W.P.Fang Department of CSIE YUST, Copy Right Reserved
214
FOR LOOP
• FOR loop_count times DO– Statements
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
215
• LOOP destination_label• The counter for the loop is the register CX which
is initialized to loop_count.Execution of the LOOP instruction causes CX to be decremented automatically
• If CX is not 0, control transfers to destination_label
• If CX=0, the next instruction after LOOP is done.• Destination_label must precede the LOOP
instruction by no more than 126 bytes
W.P.Fang Department of CSIE YUST, Copy Right Reserved
216
• TOP:– LOOP TOP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
217
ex
• Write a count-controlled loop to display a row of 80 stars
W.P.Fang Department of CSIE YUST, Copy Right Reserved
218
• FOR 80 times DO– Display ‘*’
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
219
– MOV CX,80– MOV AH,2– MOV DL,’*’
• TOP:– INT 21h– LOOP TOP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
220
• Notice:– FOR loop implement with a LOOP instruction,
is executed at least once– If CX contains 0 when the loop is entered, the
LOOP instruction cause CX to be decremented to FFFFh
– To prevent this, the instruction JCXZ(jump if CX is zero) may be used before the loop
W.P.Fang Department of CSIE YUST, Copy Right Reserved
221
• JCXZ destination_label
W.P.Fang Department of CSIE YUST, Copy Right Reserved
222
– JCXZ SKIP
• TOP:– LOOP TOP
• SKIP:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
223
WHILE LOOP
• WHILE condition DO– Statements
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
224
ex
• Write some code to count the number of characters in an input line
W.P.Fang Department of CSIE YUST, Copy Right Reserved
225
• Initialize count to 0• Read a character• WHILE character<> carriage_return DO
– Count=count+1– Read a character
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
226
– MOV DX,0– MOV AH,1– INT 21H
• WHILE_:– CMP AL,0DH– JE END_WHILE– INC DX– INT 21H– JMP WHILE_
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
227
REPEAT LOOP
• REPEAT• Statements• UNTIL condition
W.P.Fang Department of CSIE YUST, Copy Right Reserved
228
ex
• Write some code to read characters until a blank is read
W.P.Fang Department of CSIE YUST, Copy Right Reserved
229
• REPEAT– Read a character
• UNTIL character is a blank
W.P.Fang Department of CSIE YUST, Copy Right Reserved
230
– MOV AH,1
• REPEAT:– INT 21H– CMP AL.’‘– JNE REPEAT
W.P.Fang Department of CSIE YUST, Copy Right Reserved
231
WHILE Versus REPEAT
• Use of a WHILE loop or a REPEAT loop is a matter of personal preference
• The advantage of a WHILE is that the loop can be bypassed if the terminating condition is initially false, whereas the statements in a REPEAT must be done at least once.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
232
ex
• Type a line of text• THE QUICK BROWN FOX JUMPED• First capital =B last capital =X
W.P.Fang Department of CSIE YUST, Copy Right Reserved
233
• Display the opening message• Read and process a line of text• Display the results
W.P.Fang Department of CSIE YUST, Copy Right Reserved
234
TITLE PGM6_2:FIRST AND LAST CAPITALS.MODEL SMALL.STACk 100H.DATAPROMPT DB 'Type a line of text',0DH,0AH,'$'NOCAP_MSG DB 0DH,0AH,'No capitals $'CAP_MSG DB 0DH,0AH,'First capital = 'FIRST DB ']'
DB ' Last capital = 'LAST DB '@ $'.CODEMAIN PROC
MOV AX,@DATAMOV DS,AXMOV AH,9LEA DX,PROMPTINT 21HMOV AH,1INT 21H
WHILE_:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
235
CMP AL,0DHJE END_WHILECMP AL,'A'JNGE END_IFCMP AL,'Z'JNLE END_IFCMP AL,FIRSTJNL CHECK_LASTMOV FIRST,AL
CHECK_LAST:CMP AL,LASTJNG END_IFMOV LAST,AL
END_IF:INT 21HJMP WHILE_
END_WHILE:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
236
MOV AH,9CMP FIRST,']'JNE CAPSLEA DX,NOCAP_MSGJMP DISPLAY
CAPS:LEA DX,CAP_MSG
DISPLAY:INT 21HMOV AH,4CHINT 21H
MAIN ENDPEND MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
237
hw
• Ch6.3a,12
W.P.Fang Department of CSIE YUST, Copy Right Reserved
238
Logic, Shift ,and Rotate Instructions
W.P.Fang Department of CSIE YUST, Copy Right Reserved
239
Logic, Shift,and Rotate Instructions
• Logic instructions– AND– OR– XOR– NOT
W.P.Fang Department of CSIE YUST, Copy Right Reserved
240
Logic instructions
• Perform the following logic operations– 10101010 AND 11110000– 10101010 OR 11110000– 10101010 XOR 11110000– NOT 10101010
W.P.Fang Department of CSIE YUST, Copy Right Reserved
241
Truth tables(0=false ,1=true)
01111
11001
11010
00000
A XOR bA OR bA AND bba
W.P.Fang Department of CSIE YUST, Copy Right Reserved
242
instructions
• AND destination, source• OR destination, source• XOR destination, source• -------------------------------------------• Effect on flags
– SF,ZF,PF reflect the result– AF is undefined– CF,OF=0
W.P.Fang Department of CSIE YUST, Copy Right Reserved
243
• One use of AND, OR, and XOR is to selectively modify the bits in the destination.
• mask
W.P.Fang Department of CSIE YUST, Copy Right Reserved
244
b XOR 1=~b(compelemet of b)
b XOR 0=b
b OR 1 =1b OR 0 =b
b AND 0=0b AND 1 =b
W.P.Fang Department of CSIE YUST, Copy Right Reserved
245
• The AND instruction can be used to clear specific destination bits while preserving the others
• The OR instruction can be used to set specific destination bits while preserving the others.
• The XOR instruction can be used to complement specific destination bits while preserving the others.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
246
ex
• Clear the sign bit of AL while leaving the other bits unchanged
W.P.Fang Department of CSIE YUST, Copy Right Reserved
247
• AND AL,7Fh
• Ps:• 01111111b=7Fh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
248
Ex
• Set the most significant and least significant bits of AL while preserving the other bits
W.P.Fang Department of CSIE YUST, Copy Right Reserved
249
• OR AL,81h
• Ps:10000001b=81h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
250
ex
• Change the sign bit of DX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
251
• XOR DX,8000h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
252
Converting an ASCII digit to a Number
• “5”à 5• Method 1:SUB AL,20h• Method 2:AND AL,0Fh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
253
exercise
• Converting a stored decimal digit to its ASCII code
W.P.Fang Department of CSIE YUST, Copy Right Reserved
254
Converting a lowercase letter to upper case
01011010Z01111010z
::::
01000010B01100010b
01000001A01100001a
codecharactercodecharacter
W.P.Fang Department of CSIE YUST, Copy Right Reserved
255
• To convert lower to upper case we need only clear bit 5
• à AND with mask 11011111b (0DFh)• AND DL,0DFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
256
Clearing a Register
• MOV AX,0– Machine needs three bytes– Because prohibition on memory-on-memory
operations, must be used to clear a memory location
• SUB AX,AX– Machine needs two bytes
• XOR AX,AX– Machine needs two bytes
W.P.Fang Department of CSIE YUST, Copy Right Reserved
257
Testing a register for zero
• OR CX,CX• CMP CX,0• Because 1 OR 1 =1, 0 OR 0 =0• CX unchanged• But affect ZF and SF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
258
NOT instruction
• NOT destination
W.P.Fang Department of CSIE YUST, Copy Right Reserved
259
ex
• Complement the bits in AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
260
• NOT AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
261
TEST instruction
• The TEST instruction performs and an AND operation of the destination with the source but does not change the destination contents
• The purpose of the TESt instruciton is to set the status flags
W.P.Fang Department of CSIE YUST, Copy Right Reserved
262
• TEST destination,source• ------------------------------------• Effect on flags
– SF,ZF, PF reflect the result– AF is undefined– CF,OF=0
W.P.Fang Department of CSIE YUST, Copy Right Reserved
263
Examining bits
• TEST destination,mask• If destination has 0’s in all the etsted
position, the result will be 0 and so ZF=1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
264
ex
• Jump to label BELLOW if AL contains an even number
W.P.Fang Department of CSIE YUST, Copy Right Reserved
265
• Even numbers have a 0 in bit 0. thus, the mask is 00000001b=1
• TESt AL,1• JZ BELLOW
W.P.Fang Department of CSIE YUST, Copy Right Reserved
266
Shift instructions
• Opcode destination,CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
267
Left Shift instructions
• SHL destination,1• SHL destination, CL• ---------------------------------• Effect on flags
– SF,PF,ZF reflect the result– AF is undefined– CF=last bit shifted out– OF=1 if result changes sign on last shift
W.P.Fang Department of CSIE YUST, Copy Right Reserved
268
ex
• Suppose DH contain 8Ah and CL contains 3, what are the values of DH and CF after the instruction SHL DH,CL is executed?
W.P.Fang Department of CSIE YUST, Copy Right Reserved
269
• DH=10001010• à• 01010000b=50h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
270
Multiplication by Left Shift
• A left shift on a binary number multiplies it by 2.
• Ex:• 00000101b = 5d• 00001010b =10d• 00010100b = 20d
W.P.Fang Department of CSIE YUST, Copy Right Reserved
271
SAL
• Shift arithmetic left• the same machine code with SHL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
272
• Negative number can also be multiplied by powers of 2 by left shifts
• Ex:– FFFFh(-1) à FFF8h(-8)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
273
Overflow
• The overflow flags are not reliable indicators for a multiple left shift,
• A multiple shift is really a series of single shifts, and CF,OF only reflect the result of the last shift
W.P.Fang Department of CSIE YUST, Copy Right Reserved
274
ex
• BL=80h• CL=2• SHL BL,CL• à CF=OF=0• (this occur signed and unsigned overflow)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
275
ex
• Write some code to multiply th evalue of AX by 8
W.P.Fang Department of CSIE YUST, Copy Right Reserved
276
• MOV CL,3• SAL AX,CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
277
Right Shift instruction
• SHR destination,1• SHR destination, CL• --------------------------------• Effect on the flags is the same as for SHL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
278
ex
• Suppose DH contains 8AH and CL contains 2. what are the values of DH and CF after the instruction SHR DH,CL is executed?
W.P.Fang Department of CSIE YUST, Copy Right Reserved
279
• 10001010• à• 00100010b=22h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
280
SAR
• Shift arithmetic right• SAR destination,1• SAR destination, CL• The effect on flags is the same as for SHR• Different with SHR :
– The msb retains its original value
W.P.Fang Department of CSIE YUST, Copy Right Reserved
281
Division by right shift
• 00000101b=5• 00000010b=2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
282
Signed and unsigned division
• Signed : SAR• Unsigned: SHR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
283
ex
• Use right shifts to divide the unsigned number 65143 by 4. put the quotient in AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
284
• MOV AX,65143• MOV CL,2• SHR AX, CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
285
ex
• If AL contains -15, give the decimal value of Al after SAR AL,1 is performed
W.P.Fang Department of CSIE YUST, Copy Right Reserved
286
• Execution of SAR AL,1 divides the number by 2 and rounds down
• -15 /2 = -7.5• We get -8• Ps:• -15=11110001b• -8=11111000b
W.P.Fang Department of CSIE YUST, Copy Right Reserved
287
General multiplication and division
• MUL• IMUL• DIV• IDIV
W.P.Fang Department of CSIE YUST, Copy Right Reserved
288
Rotate instructions
• Rotate left• Shift bits to left. • The msb is shifted into the rightmost bit• ROL destination,1• ROL destination, CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
289
• Rotate right• ROR destination,1• ROR destination, CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
290
ex
• Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
291
– XOR AX,AX– MOV CX,16
• TOP:– ROL BX,1– JNC NEXT– INC AX
• NEXT:– LOOP TOP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
292
Rotate Carry left
• Rotate through Carry Left• The msb is shifted into CF• The previous value of CF is shifted into the
rightmost bit• RCL destination ,1• RCL destination, CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
293
Rotate carry right
• RCR destination,1• RCR destination, CL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
294
ex
• Suppose DH contains 8Ah, CF=1, and CL contains 3.what are the alues of DH and CF after the instruction RCR DH, CL is executed.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
295
10110001=B1h0After 3 right rotations
011000101After 2 right rotation
110001010After 1 right rotation
100010101Initial values
DHCF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
296
• Effect of the rotate instructions on flags– SF,PF,ZF reflect the result– AF is undefined– CF=last bit shifted out– OF=1 if result changes sign on the last
rotation
W.P.Fang Department of CSIE YUST, Copy Right Reserved
297
reversing a bit pattern
• 11011100à00111011– MOV CX,8
• REVERSE:– SHL AL,1– RCR BL,1– LOOP REVERSE– MOV AL,BL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
298
Binary and Hex I/O
• Reads in a binary number from the keyboard, followed by a carriage return
• Convert it to a bit value, and collect the bits in a register
W.P.Fang Department of CSIE YUST, Copy Right Reserved
299
• Clear BX• Input a character• WHILE character <> CR DO
– Convert character to binary value– Left shift BX– Insert value int lsb of BX– Input a character
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
300
– XOR BX,BX– MOV Ah,1– INT 21H
• WHILE_:– CMP AL,0DH– JE END_WHILE– AND AL,0FH– SHL BX,1– OR BL,AL– INT 21H– JMP WHILE_
• END_WHILE:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
301
Binary output
• FOR 16 times DO– Rotate left BX
• Put msb nito CF
– IF CF=1– THEN
• Output ‘1’
– ELSE• Output ‘0’
– END_IF
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
302
Hex input
• Clear BX• Input hex character• WHILE character<> CR DO
– Convert character to binary value– Left shift BX 4 times– Insert value into lower 4 bits of BX– Input a character
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
303
– XOR BX,BX– MOV CL,4– MOV AH,1– INT 21H
• WHILE_:– CMP AL,0DH– JE END_WHILE– CMP AL,39H– JG LETTER– AND AL,0FH– JMP SHIFT
• LETTER:– SUB AL,37H
• SHIFT:– SHL BX,CL– OR BL,AL– INT 21H– JMP WHILE_
• END_WHILE:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
304
Hex output
• For 4 times DO– MOV BH to DL– Shift DL 4 times to the right– IF DL<10
• THEN– Convert to character in ‘0’.. ‘9’
• ELSE– Convert to character in ‘A’.. ‘F’
• END_IF• Output character• Rotate BX left 4 times
– END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
305
homework
• Ch7.2,7.4,14
W.P.Fang Department of CSIE YUST, Copy Right Reserved
306
The Stack and Introduction to procedures
W.P.Fang Department of CSIE YUST, Copy Right Reserved
307
The stack
• A stack is one-dimensional data structure.• Items are added and removed from one
end of the structure.• Last-inn first-out• The most recent addition to the stack is
called the top of the stack.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
308
• Declaring a stack segment• .STACK 100H
W.P.Fang Department of CSIE YUST, Copy Right Reserved
309
• When the program is assembled and loaded in memory, SS will contain the segment number of the stack segment.
• For the preceding stack declaration, SP, the stack pointer, is initialized to 100h.
• When the stack is not empty, SP contains the offset address of the top of the stack.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
310
• PUSH source• Ex:
– PUSH AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
311
• SP is decreased by 2• A copy of the source content is moved to
the address specified by SS:SP. The source is unchanged
W.P.Fang Department of CSIE YUST, Copy Right Reserved
312
• PUSHF:– Pushes the contents of the FLAGS register
onto the stack.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
313
• Initially, SP contains the offset address of the memory location immediately following the stack segment
• The first PUSH decreases SP by 2• Making it point to the last word in the stack
segment.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
314
• POP destination• Ex:
– POP BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
315
• The content of SS:SP is moved to the destination
• SP is increased by 2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
316
• POPF:– Pop the top of the stack into the FLAGS
register.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
317
• There is no effect of PUSH,PUSHF,POP,POPF on the flags
• Note:– PUSH and POP are word operations, so a
byte instruction is illegal– Ex: (illegal) àbut this legal for 80186/80486
• PUSH DL• PUSH 2
W.P.Fang Department of CSIE YUST, Copy Right Reserved
318
A stack application
• Read a sequence of characters and display them in reverse order on the next line
W.P.Fang Department of CSIE YUST, Copy Right Reserved
319
• Display a ‘?’• Initialize count to 0• Read a character• WHILE character is not a carriage return DO
– Push character onto the stack– Increment count– Read a character
• END_WHILE• Go to a new line• FOR count times DO
– Pop a character from the stack– Display it
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
320
• TITLE PGM8_1:REVERSE INPUT• .MODEL SMALL• .STACK 100H• .CODE• MAIN PROC
– MOV AH,2– MOV DL.’?’– INT 21H– XOR CX,CX– MOV AH,1– INT 21H
W.P.Fang Department of CSIE YUST, Copy Right Reserved
321
• WHILE_:– CMP AL,0DH– JE END_WHILE– PUSH AX– INC CX– INT 21H– JMP WHILE_
• END_WHILE:– MOV AH,2– MOV DL,0DH– INT 21H– MOV DL,0AH– INT 21H– JCXZ EXIT
W.P.Fang Department of CSIE YUST, Copy Right Reserved
322
• TOP:– POP DX– INT 21H– LOOP TOP
• EXIT:• MOV AH,4CH• INT 21H
• MAIN ENDP– END MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
323
Terminology of procedures
• Procedure declaration– Name PROC type
• RET
– Name ENDP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
324
• Name is the user-defined name of the procedure.• The optional operand type is NEAR or FAR (if
type is omited, NEAR is assumed)• Ps:
– NEAR – the statement that calls the procedure is in the same segment as the procedure itself.
– FAR – the calling statement is in a different segment
W.P.Fang Department of CSIE YUST, Copy Right Reserved
325
• RET– Return– Causes control to transfer back to the calling
procedure– Every procedure should have a RET – Except main procedure
W.P.Fang Department of CSIE YUST, Copy Right Reserved
326
communication
• Up to the programmer to devise a way for procedures to communicate
• Unlike high-level language, do not have parameter lists
W.P.Fang Department of CSIE YUST, Copy Right Reserved
327
Procedure documentation
• ;(describe what the procedure does)• ; input: (where it receives information from
the calling program)• ;output: (where it delivers results to the
calling program)• ;users: (a list of procedures that it calls)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
328
CALL and RET
• CALL name• CALL address_expression
W.P.Fang Department of CSIE YUST, Copy Right Reserved
329
• Executing a CALL instruction causes the following to happen:– The return address to the calling program is
saved on the stack (CS:IP)– IP get the offset address of the first instruction
of the procedure
W.P.Fang Department of CSIE YUST, Copy Right Reserved
330
• RET pop_value• NEAR : pop IP• FAR : pop CS:IP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
331
0100
00FE
00FC
Offset Stack segmentaddress
Code segmentOffset address
MAIN PROC
CALL PROC1Next instruction
PROC1 PROCFirst instruction
RET
00100012
0200
IP
SP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
332
0100
00FE
00FC
Offset Stack segmentaddress
Code segmentOffset address
MAIN PROC
CALL PROC1Next instruction
PROC1 PROCFirst instruction
RET
00100012
0200IP
SP0012
W.P.Fang Department of CSIE YUST, Copy Right Reserved
333
0100
00FE
00FC
Offset Stack segmentaddress
Code segmentOffset address
MAIN PROC
CALL PROC1Next instruction
PROC1 PROCFirst instruction
RET
00100012
0200
IPSP0012
0300
W.P.Fang Department of CSIE YUST, Copy Right Reserved
334
0100
00FE
00FC
Offset Stack segmentaddress
Code segmentOffset address
MAIN PROC
CALL PROC1Next instruction
PROC1 PROCFirst instruction
RET
00100012
0200
IP
SP0300
W.P.Fang Department of CSIE YUST, Copy Right Reserved
335
example
• Finding the product of two positive integers A and B by addition and bit shifting
W.P.Fang Department of CSIE YUST, Copy Right Reserved
336
• Product=0• REPEAT
– IF lsb of B is 1– THEN
• Product=Product +A– END_IF– Shift left A– Shift right B
• UNTIL B=0
W.P.Fang Department of CSIE YUST, Copy Right Reserved
337
• Ex:– A=111b– B=1101b
111bx 1101b
---------------------111
000111
111---------------------
1011011b
W.P.Fang Department of CSIE YUST, Copy Right Reserved
338
• TITLE PGM8_2:REVERSE INPUT• .MODEL SMALL• .STACK 100H• .CODE• MAIN PROC• CALL MULTIPLY• MOV AH,4CH• INT 21H• MAIN ENDP• MULTIPLY PROC• PUSH AX• PUSH BX• XOR DX,DX• REPEAT:• TEST BX,1• JZ END_IF• ADD DX,AX• END_IF:• SHL AX,1• SHR BX,1• JNZ REPEAT• POP BX• POP AX• RET• MULTIPLY ENDP• END MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
339
-u0B87:0000 E80400 CALL 0007 0B87:0003 B44C MOV AH,4C 0B87:0005 CD21 INT 21 0B87:0007 50 PUSH AX 0B87:0008 53 PUSH BX 0B87:0009 33D2 XOR DX,DX 0B87:000B F7C30100 TEST BX,0001 0B87:000F 7402 JZ 0013 0B87:0011 03D0 ADD DX,AX 0B87:0013 D1E0 SHL AX,1 0B87:0015 D1EB SHR BX,1 0B87:0017 75F2 JNZ 000B 0B87:0019 5B POP BX 0B87:001A 58 POP AX 0B87:001B C3 RET0B87:001C 03D1 ADD DX,CX 0B87:001E E6D1 OUT D1,AL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
340
-rAX=0000 BX=0000 CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0000 NV UP EI PL NZ NA PO NC 0B87:0000 E80400 CALL 0007 -dss:f0 ff0B89:00F0 23 05 00 00 0B 00 87 0B-7A 05 0D 00 07 00 00 00
#.......z.......-raxAX 0000:7-rbxBX 0000:d-rAX=0007 BX=000D CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0000 NV UP EI PL NZ NA PO NC 0B87:0000 E80400 CALL 0007 -tAX=0007 BX=000D CX=001C DX=0000 SP=00FE BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0007 NV UP EI PL NZ NA PO NC 0B87:0007 50 PUSH AX -dss: f0 ff0B89:00F0 23 05 00 00 07 00 00 00-07 00 87 0B 7A 05 03 00
#...........z...
W.P.Fang Department of CSIE YUST, Copy Right Reserved
341
-gbAX=0007 BX=000D CX=001C DX=0000 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL ZR NA PE NC 0B87:000B F7C30100 TEST BX,0001 -dss: f0 ff0B89:00F0 23 05 00 00 0B 00 87 0B-7A 05 0D 00 07 00 03 00
#.......z.......-g17AX=000E BX=0006 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PE CY 0B87:0017 75F2 JNZ 000B -tAX=000E BX=0006 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PE CY 0B87:000B F7C30100 TEST BX,0001 -g17AX=001C BX=0003 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PE NC 0B87:0017 75F2 JNZ 000B
W.P.Fang Department of CSIE YUST, Copy Right Reserved
342
-tAX=001C BX=0003 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PE NC 0B87:000B F7C30100 TEST BX,0001 -g17AX=0038 BX=0001 CX=001C DX=0023 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PO CY 0B87:0017 75F2 JNZ 000B -tAX=0038 BX=0001 CX=001C DX=0023 SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PO CY 0B87:000B F7C30100 TEST BX,0001 -g17AX=0070 BX=0000 CX=001C DX=005B SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL ZR NA PE CY 0B87:0017 75F2 JNZ 000B -tAX=0070 BX=0000 CX=001C DX=005B SP=00FA BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0019 NV UP EI PL ZR NA PE CY 0B87:0019 5B POP BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
343
-tAX=0070 BX=000D CX=001C DX=005B SP=00FC BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=001A NV UP EI PL ZR NA PE CY 0B87:001A 58 POP AX -tAX=0007 BX=000D CX=001C DX=005B SP=00FE BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=001B NV UP EI PL ZR NA PE CY 0B87:001B C3 RET-dss:f0 ff0B89:00F0 70 00 70 00 07 00 00 00-1B 00 87 0B 7A 05 03 00
p.p.........z...-tAX=0007 BX=000D CX=001C DX=005B SP=0100 BP=0000 SI=0000 DI=0000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0003 NV UP EI PL ZR NA PE CY 0B87:0003 B44C MOV AH,4C -gProgram terminated normally-q
W.P.Fang Department of CSIE YUST, Copy Right Reserved
344
HW
• Ch8.9
W.P.Fang Department of CSIE YUST, Copy Right Reserved
345
Multiplication and division instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
346
MUL and IMUL
• The process of multiplication and division is different for signed and unsigned number
• There are byte and word forms
W.P.Fang Department of CSIE YUST, Copy Right Reserved
347
ex
• 10000000 x 1111111– Unsigned:
128x255=32640(0111111110000000– Signed:
-128x-1=128(0000000010000000
W.P.Fang Department of CSIE YUST, Copy Right Reserved
348
• MUL (multiply)• IMUL (integer multiply)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
349
• MUL source• IMUL source
W.P.Fang Department of CSIE YUST, Copy Right Reserved
350
Byte Form
• One umber is contained in the source• The other is assumed to be in AL• 16-bit production will be in AX• Ps:
– Source may be a byte register or memory byte, but not a constant
W.P.Fang Department of CSIE YUST, Copy Right Reserved
351
Word form
• One number is contained in the source• The other is assumed to be in AX• The most significant 16 bits of the double
word product will be in DX• The least significant 16 bits will be in AX• DX:AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
352
• For positive number ( 0 in the most significant bit) MUL and IMUL give the same result
W.P.Fang Department of CSIE YUST, Copy Right Reserved
353
Effect of MUL/IMUL on the status flags
• SF,ZF,AF,PF: undefined• CF/OF:
– After MUL,CF/OF • =1 it he upper half of the result is zero• =1 otherwise
– After IMUL, CF/OF• =0 if the upper half of the result is the sign
extension of the lower half ( the bits of the upper half are the same as the sign bit of the lower half)
• =1 otherwise
W.P.Fang Department of CSIE YUST, Copy Right Reserved
354
• If CF/OF=1 – The product is too big to fit in the lower half of
the destination (ALor AX)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
355
ex
• Suppose AX contains 1 and BX contains FFFFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
356
0FFFFFFFFFFFFFFFF
-1IMUL BX
0FFFF00000000FFFF65535MUL BX
CF/OFAXDXHex product
Decimal product
Instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
357
ex
• Suppose AX contains FFFFh and BX contains FFFFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
358
000010000000000011IMUL BX
10001FFFEFFFE0001
4294836225
MUL BX
CF/OFAXDXHex product
Decimal product
Instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
359
ex
• Suppose AX contains 0FFFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
360
100100FF00FFE00116769025IMUL AX
1E00100FF00FFE00116769025MUL AX
CF/OFAXDXHex product
Decimal product
Instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
361
ex
• Suppose AX contains 0100h and CX contains FFFFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
362
0FF00FFFFFFFFFF00
-256IMUL CX
1FF0000FF00FFFF0016776960MUL CX
CF/OFAXDXHex product
Decimal product
Instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
363
ex
• Suppose AL contains 80h and BL contains FFh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
364
180000080128IMUL BL
1807F7F80128MUL BL
CF/OFALAHHex product
Decimal product
Instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
365
application
• Ex : translate the high-level language assignment statement A=5xA-12xB into assembly code. Let A and B be word variables, and suppose there is no overflow. Use IMUL for multiplication
W.P.Fang Department of CSIE YUST, Copy Right Reserved
366
• MOV AX,5• IMUL A• MOV A,AX• MOV AX,12• IMUL B• SUB A,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
367
ex
• Write a procedure FACTORIAL that will compute N! for a positive integer N. The procedure should receive N in CX and return N! in AX. Suppose that overflow does not occur
W.P.Fang Department of CSIE YUST, Copy Right Reserved
368
• N!=1 if N=1• =Nx(N-1)x(N-2)x… x1 if N>1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
369
• Product =1• Term=N• FOR N times DO
– product=product x term– term=trem-1
• ENDFOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
370
• FACTORIAL PROC– MOV AX,1
• TOP:– MUL CX– LOOP TOP– RET
• FACTORIAL ENDP• Ps: CX=N,AX=N!
W.P.Fang Department of CSIE YUST, Copy Right Reserved
371
DIV and IDIV
• When division is performed, we obtain two results– Quotient– remainder
W.P.Fang Department of CSIE YUST, Copy Right Reserved
372
• DIV divisor• IDIV divisor
W.P.Fang Department of CSIE YUST, Copy Right Reserved
373
Byte form
• Divisor is an 8-bit register or memory byte• 16-bit dividend is assumed to be in AX• 8-bit quotient is in AL• 8-bit remainder is in AH• Ps:
– Divisor may not be a constant
W.P.Fang Department of CSIE YUST, Copy Right Reserved
374
Word form
• The divisor is a 16-bit register or memory• 32-bit dividend is assumed to be in DX:AX• 16-bit quotient is in AX• 16-bit remainder is in DX• For signed division, the remainder has the
same sign as the dividend
W.P.Fang Department of CSIE YUST, Copy Right Reserved
375
• The effect of DIV/IDIV on the flags is that all status flags are undefined
W.P.Fang Department of CSIE YUST, Copy Right Reserved
376
Divide overflow
• The quotient will be too big to fit in the specified destination
• Devisor is much smaller than dividend
W.P.Fang Department of CSIE YUST, Copy Right Reserved
377
ex
• Suppose DX contains 0000h, AX contains 0005h , and BX contains 0002h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
378
0001000212IDIV BX
0001000212DIV BX
DXAXDecimal remainder
Decimal quotient
instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
379
ex
• Suppose DX contains 0000h, AX contains 0005h, and BX contains FFFEh
W.P.Fang Department of CSIE YUST, Copy Right Reserved
380
0001FFFE1-2IDIV BX
0005000050DIV BX
DXAXDecimal remainder
Decimal quotient
instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
381
• Suppose DX contains FFFFh, AX contains FFFBh, and BX contains 0002
W.P.Fang Department of CSIE YUST, Copy Right Reserved
382
DIVIDE OVERFLOW
IDIV BX
FFFFFFFE-1-2DIV BX
DXAXDecimal remainder
Decimal quotient
instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
383
ex
• Suppose AX contains 00FBh and BL contains FFH
W.P.Fang Department of CSIE YUST, Copy Right Reserved
384
DIVIDE OVERFLOW
IDIV BL
00FB2510DIV BL
DXAXDecimal remainder
Decimal quotient
instruction
W.P.Fang Department of CSIE YUST, Copy Right Reserved
385
Sign extension of the dividend
• In word division, the dividend is in DX:AX even if the actual dividend will fit in AX.– For DIV,DX should be cleared– For IDIV,DX should be made the sign
extension of AX.the instruction CWD(convertword to doubleword) will be the extension
W.P.Fang Department of CSIE YUST, Copy Right Reserved
386
ex
• Divide -1250 by 7
W.P.Fang Department of CSIE YUST, Copy Right Reserved
387
• MOV AX,-1250• CWD• MOV BX,7• IDIV BX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
388
Byte division
• In byte division, the dividend is in AX.• If the actual dividend is a byte, then AH
should be prepared as follows– For DIV, AH should be cleared– For IDIV, AH should the sign extension of AL.
the instruction CBW (convert byte to word) will do the extension
W.P.Fang Department of CSIE YUST, Copy Right Reserved
389
ex
• Divide the signed value of the byte variable XBYTE by -7
W.P.Fang Department of CSIE YUST, Copy Right Reserved
390
• MOV AL,XBYTE• CBW• MOV BL,-7• IDIV BL
W.P.Fang Department of CSIE YUST, Copy Right Reserved
391
• There is no effect of CBW and CWD on the flags
W.P.Fang Department of CSIE YUST, Copy Right Reserved
392
Decimal input and output procedures
• Decimal output• Decimal input
W.P.Fang Department of CSIE YUST, Copy Right Reserved
393
Decimal output
• IF AX<0 – THEN
• Print a minus sign• Replase AX by its two’s complemet
• END_IF• Get the digits in AX’s decimal
representation• Convert these digits to characters and
print them
W.P.Fang Department of CSIE YUST, Copy Right Reserved
394
Get the digits in AX’s decimal representation
• Count=0• ERPEAT
– Divide quotient by 10– Push remainder on the stack– Count=count+1
• UNTIL quotient=0
W.P.Fang Department of CSIE YUST, Copy Right Reserved
395
Convert these digits to characters and print them
• FOR count times DO– Pop a digit from the stack– Convert it to a character– Output the character
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
396
OUTDEC PROCPUSH AXPUSH BXPUSH CXPUSH DXOR AX,AXJGE @END_IF1PUSH AXMOV DL,’-’MOV AH,2INT 21HPOP AXNEG AX
@END_IF1:XOR CX,CXMOV BX,10D
@REPEAT1:XOR DX,DXDIV BXPUSH DXINC CXOR AX,AXJNE @REPEAT1
PGM9_1.asm
W.P.Fang Department of CSIE YUST, Copy Right Reserved
397
MOV AH,2@PRINT_LOOP:
POP DXOR DL,30HINT 21HLOOP @PRINT_LOOPPOP DXPOP CXPOP BXPOP AXRET
OUTDEC ENDP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
398
INCLUDE Pseudo-op
• INCLUDE filespec• Ex:
– INCLUDE A:PGM9_1.asm
W.P.Fang Department of CSIE YUST, Copy Right Reserved
399
• TITLE PGM9_2: DECIMAL OUTPUT• .MODEL SMALL• .STACK 100H• .CODE• MAIN PROC
– CALL OUTDEC– MOV AH,4CH– INT 21H
• MAIN ENDP• INCLUDE A:PGM9_1.asm
– END MAIN
W.P.Fang Department of CSIE YUST, Copy Right Reserved
400
• Debug pgm9_2.exe• -rax• AX 0000• :9C71• -g• -25487• Program terminated normally• -rax• AX 9C71• :28E• -g• 654
W.P.Fang Department of CSIE YUST, Copy Right Reserved
401
Decimal input
• Convert a string of ASCII digits to the binary representation of a decimal integer
W.P.Fang Department of CSIE YUST, Copy Right Reserved
402
• Print a question mark• Total=0• Negative =false• Read a character• CASE character OF
– ‘-’: negative=true– Read character– ‘+’:read a character
• END_CASE• REPEAT
– IF character is not between ‘0’and ‘9’• THEN
– Go to beginning– ELSE
• Convert character to a binary value• Total =10xtotal +value
– END_IF– Read a character
• UNTIL character is a carriage return• IF negatiev = true
– THEN• Totla=-total
– ENDIF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
403
exercise
• A=5xA-7• B=(A-B)x(B+10)• A=6-9xA
W.P.Fang Department of CSIE YUST, Copy Right Reserved
404
A=5xA-7
• MOV AX,5• IMUL A• SUB AX,7• MOV A,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
405
B=(A-B)x(B+10)
• MOV AX,A• SUB AX,B• MOV BX,B• ADD BX,10• IMUL BX• MOV B,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
406
A=6-9xA
• MOV AX,9• IMUL A• NEG AX• ADD AX,6• MOV A,AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
407
– MOV AX,A– IMUL AX– MOV A,AX– MOV AX,B– IMUL AX– MOV B,AX– MOV AX,C– MUL AX,AX– SUB AX,A
• IF_:– CMP AX,B– JNE ELSE
• THEN:– STC– JMP EXIT
• ELSE:– CLC
• EXIT:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
408
exercise
• IF A^2+B^2=C^2• THEN
– SET CF
• ELSE– Clear CF
• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
409
• MOV AX,A• IMUL AX• MOV A,AX• MOV AX,B• IMUL AX• MOV B,AX• MOV AX,C• MUL AX,AX• SUB AX,A• IF_:
– CMP AX,B– JNE ELSE
• THEN:– STC– JMP EXIT
• ELSE:– CLC
• EXIT:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
410
Decimal Input• Print a question mark• Total=0• Negative=false• Read a character• CASE character OF
– ‘-’:negative=true• Read a character
– ‘+’:read a character• END_CASE• REPEAT
– If character is not between ‘0’and ‘9’– THEN
• Go to beginning
– ELSE• Convert character to a binary value• Total =10xtotal+value
– END_IF– Read a character
• UNTIL character is a carriage return• IF negative =true• THEN• Total=-total• END_IF
W.P.Fang Department of CSIE YUST, Copy Right Reserved
411
PGM9_3.asm
W.P.Fang Department of CSIE YUST, Copy Right Reserved
412
PGM9_4.asm
W.P.Fang Department of CSIE YUST, Copy Right Reserved
413
HW
• Ch9.7,11
W.P.Fang Department of CSIE YUST, Copy Right Reserved
414
Arrays and Addressing Modes
W.P.Fang Department of CSIE YUST, Copy Right Reserved
415
One-dimensional arrays
• An ordered list of elements, all of the same type.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
416
• MSG DB ‘abcde’• W DW 10,20,30,40,50,60
W.P.Fang Department of CSIE YUST, Copy Right Reserved
417
• Base address of the array
W.P.Fang Department of CSIE YUST, Copy Right Reserved
418
DUP (duplicate)
• Define arrays whose elements share a common initial value
• repeat_count DUP (value)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
419
ex
• GAMMA DW 100 DUP(0)• DELTA DB 212 DUP(?)• LINE DB 5,4, 3 DUP(2,3, DUP (0), 1)• LINE DB 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
420
Location of array elements
• A=(N-1)xS• S: number of bytes in an element• A:array
W.P.Fang Department of CSIE YUST, Copy Right Reserved
421
ex
• exchange the 10th and 25th elements in a word array W.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
422
• MOV AX,W+18• XCHG W+48,AX• MOV W+18,AX
• Ps:• W[10] is located at address W+9x2=18• W[25] is located at address
W+24x2=W+48
W.P.Fang Department of CSIE YUST, Copy Right Reserved
423
Address modes
• Register mode• Immediate mode• Direct mode
W.P.Fang Department of CSIE YUST, Copy Right Reserved
424
Register indirect mode
• [register]• The register is BX,SI,DI or BP• Segment is contained in DS• MOV AX,[SI] ; not same as MOV AX,SI• (suppose SI contain 0100h))• The CPU
– Examines SI and obtains the offset address 100h– Use the address DS:0100h to obtain the value 1234h– Move 1234h to AX
W.P.Fang Department of CSIE YUST, Copy Right Reserved
425
ex
Offset 3000h contains 031DhDI contains 1000h
Offset 2000h contains 20FEhSI contains 1000h
Offset 1000h contains 1BAChBX contains 1000h
Tell which of the following instruction are legal, give the source offset address, and the result or number movesa.MOV BX,[BX]b.MOV CX,[SI]c.MOV BX,[AX]d.ADD [SI],[DI]e.INC [DI]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
426
031Eh3000he
Illegal memory-memory addition
d
(must be BX,SI or DI)
Illegal source registerc
20FEh2000hb
1BACh1000ha
resultSource offset
W.P.Fang Department of CSIE YUST, Copy Right Reserved
427
ex
• Write some code to sum in AX the elements of the 10-element array W define by
• W DW 10,20,30,40,50,60,70,80,90,100
W.P.Fang Department of CSIE YUST, Copy Right Reserved
428
– XOR AX,AX– LEA SI,W– MOV CX,10
• ADDNOS:– ADD AX,[SI]– ADD SI,2– LOOP ADDNOS
W.P.Fang Department of CSIE YUST, Copy Right Reserved
429
Ex
• Write a procedure REVERSE that wullreverse an array of N words.
• The procedure is entered with SI pointing the array, and BX has the number or words N
W.P.Fang Department of CSIE YUST, Copy Right Reserved
430
• The idea is to exchange the 1st and N thwords, the 2nd and (N-1)st words, and so on. The number of exchanges will be N/2(rounded down to the nearest integer if N is odd) .
• Ps:– Nth element in a word array A has address
A+2x(N-1)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
431
REVERSE PROCPUSH AXPUSH BXPUSH CXPUSH SIPUSH DIMOV DI,SIMOV CX,BXDEC BXSHL BX,1ADD DI,BXSHR CX,1
XCHG_LOOP:MOV AX,[SI]XCHG AX,[DI]MOV [SI],AXADD SI,2SUB DI,2LOOP XCHG_LOOPPOP DIPOP SIPOP CXPOP BX
POP AXRET
REVERSE ENDP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
432
Based and Indexed addressing mode
• Displacement may be any of the following:– The offset address of a variable– A constant (positive or negative)– The offset address of a variable plus or minus
a constant
W.P.Fang Department of CSIE YUST, Copy Right Reserved
433
• Example of displacements• (if A is a variable)
– A (offset address of a variable)– -2 (constant)– A+4 (offset address of a variable plus a
constant)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
434
• [register+displacement]• [displacement+register]• [register]+displacement• Displacement+[register]• Displacement[register]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
435
• The register must be BX,BP,SI or DI• If BX,SI or DI is used, DS contains the segment
number of the operand’s address• If BP is used,SS has the segment number.• The addressing model is called based if BX
(base register) or BP(base pointer) is used• The addressing model is called indexed if
SI(source index) or DI(destination index) is used
W.P.Fang Department of CSIE YUST, Copy Right Reserved
436
ex
• MOV AX,W[BX]• MOV AX,[W+BX]• MOV AX,[BX+W]• MOV AX,W+[BX]• MOV AX,[BX]+W• MOV AX,[SI+2]• MOV AX,[2+SI]• MOV AX,[SI}+2• MOV AX,2[SI]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
437
ex
• Write some code to sum in AX the element of 10-element array W defined by
• W DW 10,20,30,40,50,60,70,80,90,100
W.P.Fang Department of CSIE YUST, Copy Right Reserved
438
– XOR AX,AX– XOR BX,BX– MOV CX,10
• ADDNOS:– ADD AX,W[BX]– ADD BX,2– LOOP ADDNOS
W.P.Fang Department of CSIE YUST, Copy Right Reserved
439
ex• Suppose ALPHA is declared as • ALPHA DW 0123h,0456h,0789h,0ABCDh• In the segment address by DS, suppose that • BX contains 2 offset 0002 contains 1084h• SI contains 4 offset 0004 contains 2BACh• DI contains 1• Tell which of the following instruction are legal, if legal, give the source
offset address and the number moveda. MOV AX,[ALPHA+BX]b. MOV BX,[BX+2]c. MOV CX,ALPHA[SI]d. MOV AX,-2[SI]e. MOV BX,[ALPHA+3+DI]f. MOV AX,[BX]2g. ADD BX,[ALPHA+AX]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
440Illegal source registerg
Illegal form of source operand
F
0789hALPHA+3+1=ALPHA+4E
1084h-2+4=2D
0789hALPHA+4C
2BACh2+2=4B
0456hALPHA +2A
Number movedSource offset
W.P.Fang Department of CSIE YUST, Copy Right Reserved
441
ex
• Replace each lowercase letter in the following string by its upper case equivalent. Use index addressing mode
• MSG DB ‘this is a message’
W.P.Fang Department of CSIE YUST, Copy Right Reserved
442
– MOV CX,17– XOR SI,SI
• TOP:– CMP MSG[SI],’‘– JE NEXT– AND MSG[SI],0DFh
• NEXT:– INC SI– LOOP TOP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
443
The PRT operator and LABEL pseudo-op
• Operands of an instruction must be of the same type
• If one operand is a constant, the assembler attempts to infer the type from the other operand
W.P.Fang Department of CSIE YUST, Copy Right Reserved
444
• MOV [BX],1 ;illegal ,can’t assemble
• Shall be• MOV BYTE PTR [BX],1• MOV WORD PTR [BX],1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
445
ex
• LEA SI,MSG• MOV BYTE PRT [SI],’T’
W.P.Fang Department of CSIE YUST, Copy Right Reserved
446
ex
• Using index mode– XOR SI,SI– MOV MSG[SI],’T’– Ps:here it is not necessary to use th PTR
operator, because MSG is a byte variable
W.P.Fang Department of CSIE YUST, Copy Right Reserved
447
Use PTR to override a type
Type PTR address_expressionEx:
DOLLARS DB 1AhCENTS DB 52hMOV AX,DOLLARS ;illegalMOV AX,WORD PRT DOLLARS;AL=dollars,AH=cents
W.P.Fang Department of CSIE YUST, Copy Right Reserved
448
The LABEL Pseudo-Op
• Declaration types MONEY as a word variable, and the components DOLLARS and CENTS as byte variables– MONEY LABEL WORD– DOLLAR DB 1Ah– CENTS DB 52h
W.P.Fang Department of CSIE YUST, Copy Right Reserved
449
• MOV AX,MONEY; AL=dollars,AH=cents
W.P.Fang Department of CSIE YUST, Copy Right Reserved
450
Segment override
• Specify an relative to one of the other segment registers
• Segment_register:[pointer_register]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
451
• MOV AX,ES:[SI]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
452
Accessing the stack
• Move the top three words on the stack into AX,BX, and CX without changing the stack
W.P.Fang Department of CSIE YUST, Copy Right Reserved
453
• MOV BP,SP• MOV AX,[BP]• MOV BX,[BP+2]• MOV,[BP+4]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
454
Selection algorithm
• i=N• FOR N-1 times DO
– Find the position k of the largest elemnt• Among A[1]..A[i]
– (*) swap A[k] and A[i]– i=i-1
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
455
• PGM10_2.asm
W.P.Fang Department of CSIE YUST, Copy Right Reserved
456
Two-dimensional arrays
• Memory is one-dimensional ,the elements of a two-dimensional array must be stored sequentially
• Two common used ways– Row-major order– Column-major order
W.P.Fang Department of CSIE YUST, Copy Right Reserved
457
• B DW 10,20,30,40• DW 50,60,70,80• DW 90,100,110,120
W.P.Fang Department of CSIE YUST, Copy Right Reserved
458
• MxN– A[i][j] è A+((i-1)xN+(j-1))xS– A[i][j] è A+((i-1)+(j-1)xM)xS
W.P.Fang Department of CSIE YUST, Copy Right Reserved
459
Based indexed addressing mode
• Offset address of the operand is the sum of– The contents of a base register (BX or BP)– The contents of an index register (SI or DI)– Optionally, a variable’s offset address– Optionally, a constant (positive or negative)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
460
• Variable[base_register][index_register]• [base_register+index_register+variable+co
nstant]• Variable[base_register+index_register+co
nstant]• Constant[base_register+index_register+va
riable]
W.P.Fang Department of CSIE YUST, Copy Right Reserved
461
ex
• Suppose A is a 5x7 word array stored in row-major order.write some code to (1) clear row 3,(2) clear column4 use based indexed mode
W.P.Fang Department of CSIE YUST, Copy Right Reserved
462
• (1)– MOV X,28– XOR SI,SI– MOV CX,7
• CLEAR:– MOV A[BX][SI],0– ADD SI,2– LOOP CLEAR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
463
• (2)– MOV SI,6– XOR BX,BX– MOV CX,5
• CLEAR:– MOV A[BX][SI],0– ADD BX,1– LOOP CLEAR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
464
The XFLAT instruction
• A no-operand instruction that can be used to convert a byte value into another value that comes from a table
• The byte to be convert must be in AL, and BX has the offset address of the conversion table
W.P.Fang Department of CSIE YUST, Copy Right Reserved
465
• The instruction– Add the contents of Al to the address n BX to
produce an address within the table– Replaces the contents of AL by the value
found at that address
W.P.Fang Department of CSIE YUST, Copy Right Reserved
466
ex
• TABLE DB 030h,031h,032h,033h,034h,035h,036h,037h,038h,039h
• DB 041h,042h,043h,044h,045h,046h• MOV AL,0Ch• LEA BX,TABLE• XLAT• àAL has ‘C’
W.P.Fang Department of CSIE YUST, Copy Right Reserved
467
HW
• Ch10.6
W.P.Fang Department of CSIE YUST, Copy Right Reserved
468
The string instructions
• Copy string• Search a string for a particular byte or
word• Store characters in a string• Compare strings of characters
alphabetically
W.P.Fang Department of CSIE YUST, Copy Right Reserved
469
Direction flag
• DF (direction flag)– determine the direction in which string
operations will proceed.– These operations are implemented by the two
index register SI and DI
W.P.Fang Department of CSIE YUST, Copy Right Reserved
470
• STRING1 DB ‘ABCDE’
• If DF=0,SI and DI proceed in the direction of increasing memory addresses from left to right across the string.
• If DF=1, SI and DI proceed in the direction of decreasing memory address: from right to left
W.P.Fang Department of CSIE YUST, Copy Right Reserved
471
CLD and STD
• CLD clear direction flag• STD set direction flag
W.P.Fang Department of CSIE YUST, Copy Right Reserved
472
Moving a string
• MOVSB • Copies the contents of the byte addressed
by DS:SI, to the byte address by ES:DI
W.P.Fang Department of CSIE YUST, Copy Right Reserved
473
ex• .DATA• STRING 1 DB ‘HELLO’• STRING2 DB 5 DUP(?)• MOV AX,@DATA• MOV DS,AX• MOV EX,AX• LEA SI,STRING1• LEA DI,STRING2• CLD
MOVSB• MOVSB
W.P.Fang Department of CSIE YUST, Copy Right Reserved
474
Move entire string
• REP MOVSB
• Ex:– CLD – LEA SI,STRING1– LEA DI,STRING2– MOV CX,5– REP MOVSB
W.P.Fang Department of CSIE YUST, Copy Right Reserved
475
Ex:
• Write instructions to copy STRING1 into STRING2 in reverse order
W.P.Fang Department of CSIE YUST, Copy Right Reserved
476
– LEA SI,STRING1+4– LEA DI,STRNIG2– STD– MOV CX,5
• MOVE:– MOVSB– ADD DI,2– LOOP MOVE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
477
Move word string
• MOVSW • Moves a word from the source string to the
destination string
W.P.Fang Department of CSIE YUST, Copy Right Reserved
478
ex
• For the following array• ARR DW 10,20,40,50,60,?• Write instructions to insert 30 between 20
and 40.( assume DS and ES have been initialized to the data segment.)
W.P.Fang Department of CSIE YUST, Copy Right Reserved
479
• STD• LEAD SI,ARR+8h• LEA DI,ARR+Ah• MOV CX,3• REP MOVSW• MOV WORD PTR [DI],30
W.P.Fang Department of CSIE YUST, Copy Right Reserved
480
Stroe string
• STOSB– Move the contents of the AL register to the
byte addressed by ES:DI.
• STOSW– Move the contents of the AX register to the
byte addressed by ES:DI.
W.P.Fang Department of CSIE YUST, Copy Right Reserved
481
ex
• MOV AX,@DATA• MOV ES,AX• LEA DI,STRING1• CLD• MOV AL,’A’• STOSB• STOSB
W.P.Fang Department of CSIE YUST, Copy Right Reserved
482
Reading and storing a character string
• Use INT 21h, function 1 and STOSB
W.P.Fang Department of CSIE YUST, Copy Right Reserved
483
• Chars_read=0• Read a char• WHILE char is not a carriage return DO
– IF char is a backspace– THEN chars_read=chars_read-1
• Chars_read=chars_read-1• Remove previous char from string
– ELSE• Store char ni string• Chars_read=chars_read+1
– END_IF– Read a char
• END_WHILE
W.P.Fang Department of CSIE YUST, Copy Right Reserved
484
• READ_STR PROC NEAR– PUSH AX– PUSH DI– CLD– XOR BX,BX– MOV AH,1– INT 21H
• WHILE1:– CMP AL,0DH– JE END_WHILE1– CMP AL,8H– JNE ELSE1– DEC DI– DEC BX– JMP READ
• ELSE1:– STOSB– INC BX
• READ:– INT 21H– JMP WHILE1
• END_WHILE1:– POP DI– POP AX– RET
• READ_STR ENDP
W.P.Fang Department of CSIE YUST, Copy Right Reserved
485
Load string
• LODSB– Moves the byte addressed by DS:SI into
AL,SI is then incremented if DF=0 or decremented if DF=1
• LODSW– Moves the byte addressed by DS:SI into
AX,SI is then incremented by 2 if DF=0 or decremented by 2 if DF=1
W.P.Fang Department of CSIE YUST, Copy Right Reserved
486
Displaying a character string
• FOR count times DO– Load a string character into AL– Move it to DL– Output character
• END_FOR
W.P.Fang Department of CSIE YUST, Copy Right Reserved
487
• Let Si =offset of string,BX=no. of chars. To display– MOV CX,BX– JCXZ P_EXIT– CLD– MOV AH,2
• TOP:– LODSB– MOV DL,AL– INT 21H– LOOP TOP– P_EXIT:
W.P.Fang Department of CSIE YUST, Copy Right Reserved
488
Scan string
• SCANSB• Examine a string for a target byte• The target byte is contained in AL.• SCANSB subtracts the string byte pointed
to by ES:DI from contents of AL and uses the result to set the flags
• The result is not stored
W.P.Fang Department of CSIE YUST, Copy Right Reserved
489
• SCANSW– If not found ZF=0
W.P.Fang Department of CSIE YUST, Copy Right Reserved
490
• REPNZ (repeat while not zero)• REPNE (the same as above)• Ex:
– REPNE SCANSB
W.P.Fang Department of CSIE YUST, Copy Right Reserved
491
Ex:counting vowels and consonants
• PGM11_4
W.P.Fang Department of CSIE YUST, Copy Right Reserved
492
Compare string
• CMPSB– Subtracts the byte with address ES:DI from
the byte with address DS:SI, and sets the flags. the result is not stored
• CMPSW
W.P.Fang Department of CSIE YUST, Copy Right Reserved
493
• .DATA• STRING1 DB ‘ACD’• STRING2 DB ‘ABC’• MOV AX,@DATA• MOV DS,AX• MOV ES,AX• CLD• LEA SI,STRING1• LEA DI,STRING2• CMPSB• CMPSB