You are on page 1of 493

Assembly Language

W.P.fang

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

1

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

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

2

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

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

3

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

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

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

schdular
¤é´Á 1 2 3 4 5 6 7 8 9 ¶i«× ³Æµù

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

7

schdular
¤é´Á 10 11 12 13 14 15 16 17 18 ¶i«× ³Æµù

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

8

Microcomputer Systems

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

9

Fang Department of CSIE YUST. W. Copy Right Reserved 10 .Microcomputer Systems • Overview – Introduction to the architeture of microcomputers in general and to the IBM PC in particular.P.

Fang Department of CSIE YUST.Microcomputer System (component) • The components o a microcomputer system – – – – System unit Keyboard display screen Disk drives W.P. Copy Right Reserved 11 .

Microcomputer System (component) • Computer circuits – Central processing unit (CPU) – I/O circuits • Microcomputer – CPU is a single-chip processor (microprocessor) • System board – Microprocessor and memorycircuirs W.P. Copy Right Reserved 12 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST. Address : 3 2 1 0 content : 01100001 01011110 00000000 01101110 W. Copy Right Reserved 13 .P.Microcomputer System (component) • Bytes – eight bits • Address – identified by a number • Content– the data stored in a memory byte.

How many memory bytes can be accessed? W. Copy Right Reserved 14 .Microcomputer System (component) • Example 1.1 Suppose a processor uses 20 bits for an address.Fang Department of CSIE YUST.P.

Microcomputer System (component) • Word – typical microcomputer.P. Copy Right Reserved 15 . two bytes form a word • Bit position Byte bit position 7 Word bit position 15 14 13 12 11 10 9 8 7 8 6 5 4 3 2 1 0 6 5 4 3 2 1 0 High byte Low byte W.Fang Department of CSIE YUST.

Microcomputer system (component) • RAM and ROM • Bus W. Copy Right Reserved 16 .P.Fang Department of CSIE YUST.

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

BP.SP W. NOT – Registers • AX.SI.P. Copy Right Reserved 18 .Microcomputer system (component) • EU (Execution Unit) – Arithmetic • +-*/ – Logic unit • AND. OR.DI.Fang Department of CSIE YUST.BX.CXDX.

IP W. Copy Right Reserved 19 .Fang Department of CSIE YUST.DS.SS.ES.P.Microcomputer system (component) • EIU ( Bus Interface Unit) – Communication between the EU and the memory or I/O circuits – Registers • CS.

P.Microcomputer system (component) • I/O ports W. Copy Right Reserved 20 .Fang Department of CSIE YUST.

P.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. Copy Right Reserved 21 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST. – Execute • Perform the operation on the data • Store the result in memory if needed. 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. W.P.

Copy Right Reserved 23 .Microcomputer system (I/O Devices) • • • • Magnetics disks Keyboard Display monitor printers W.P.Fang Department of CSIE YUST.

P.Fang Department of CSIE YUST. Copy Right Reserved .Microcomputer system (programming languages) Machine instruction 10100001 00000000 00000000 • Machine language operation Fetch the content of memory word to and put it in register AX Add 4 to AX 00000101 00000100 00000000 10100011 00000000 00000000 Store the content of AX in memory word 0 24 W.

P.Fang Department of CSIE YUST.Microcomputer system (programming languages) Assembly language instruction MOV AX.A • Assembly language operation Fetch the content of location A and put it in register AX Add 4 to AX ADD AX.AX move the content of AX into location A W.4 MOV A. Copy Right Reserved 25 .

Microcomputer system (programming languages) • Assembler • compiler • High-level language W. Copy Right Reserved 26 .P.Fang Department of CSIE YUST.

Copy Right Reserved 27 .P.Fang Department of CSIE YUST.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.

@DATA MOV DS.exit to Dos MOV AX.TITLE PGM1_1: SAMPLE PROGRAM .add the numbers MOV AX.AX .DATA A DW 2 B DW 5 SUM DW ? .4C00H INT 21H MAIN ENDP END MAIN Microcomputer system (assembly languages) ch1.MODEL SMALL .B MOV SUM. Copy Right Reserved 28 .AX .CODE MAIN PROC .Fang Department of CSIE YUST.initialize DS MOV AX.A ADD AX.asm W.P.STACK 100H .

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.Fang Department of CSIE YUST.P.

Copy Right Reserved 30 .P.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.Fang Department of CSIE YUST.

/? Display this help screen /iPATH Search PATH for include files /jCMD Jam in an assembler directive CMD (eg./r Emulated or Real floating-point instructions /h. or = value VAL /e. zd=line numbers only. standard w/overlays. mx=globals. version xxxx /w0./w1. /jIDEAL) /kh# Hash table capacity # symbols /l. Phar Lap./s Alphabetic or Source-code segment ordering /c Generate cross-reference in listing /dSYM[=VAL] Define symbol SYM = 0./mx.Fang Department of CSIE YUST./w2 Set warning level: w0=none. 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.Microcomputer system (tasm) • • • • • • • • • • • • • • • • • • • • • • • • Syntax: TASM [options] source [./oi Object code: standard./op./la Generate listing: l=normal listing. w1=w2=warnings on /w-xxx./zd. Copy Right Reserved 31 ./o. mu=none /mv# Set maximum valid length for symbols /m# Allow # multiple passes to resolve forward references /n Suppress symbol tables in listing /os./mu Case sensitivity on symbols: ml=all./w+xxx Disable (-) or enable (+) warning xxx /x Include false conditionals in listing /z Display source line with error message /zi./zn Debug info: zi=full. la=expanded listing /ml.xref] /a.object] [. zn=none W.listing] [.P.

P. exefile. Copy Right Reserved 32 .01 Copyright (c) 1987. 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. 1990 Borland International Syntax: TLINK objfiles.Microcomputer system (tlink) • • • • • • • • • • • • • • • • • • Turbo Link Version 3.Fang Department of CSIE YUST. mapfile.

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

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

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.

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

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

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

Copy Right Reserved 39 .P.Glossary • System board • Video adapter • word W.Fang Department of CSIE YUST.

Representation of numbers and characters W. Copy Right Reserved 40 .Fang Department of CSIE YUST.P.

Fang Department of CSIE YUST.Representation of numbers and characters • Please refer to bcc W.P. Copy Right Reserved 41 .

• Binary number system W.P.Fang Department of CSIE YUST. Copy Right Reserved 42 .

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

• • • • •

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

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

44

ASCII code
• American Standard Code for Information Interchange

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

45

Homework
• Chap 2
– – – – 2. 7.d 8.c The rule of ascii

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

46

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

• Conversion • Addition and subtraction • Character representation

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

47

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

Representation of numbers and characters (Glossary)

48

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

Representation of numbers and characters (Glossary)

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

49

Organization of the IBM Personal Computers

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

50

Organization of the IBM Personal Computers
• Overall structure of the IBM PC
– – – – The memory organization I/O ports DOS routines BIOS routines

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

51

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

Register • • • • • • Data register Address register Status register Segment Pointer Index register W.P.Fang Department of CSIE YUST. Copy Right Reserved 53 .

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

Fang Department of CSIE YUST. Copy Right Reserved 55 .P.Data register • • • • AX(accumulator) BX(base register) CX(count register) DX(data register) W.

Fang Department of CSIE YUST.code segment DS – data segment SS – stack segment ES – access second data segment (extra segment) W.P. Copy Right Reserved 56 .Segment register • • • • CS -.

Copy Right Reserved 57 .• Memory segment • Segment:offset address – Because 20bit can not represent in 16 bit W.Fang Department of CSIE YUST.P.

Logical address • Ex: • A4FB:4872h • A4FB0+4872h=A9822h(20-bit physical address) W.P.Fang Department of CSIE YUST. Copy Right Reserved 58 .

give the address in segment:offset form for segments 1256h and 1240h W.Fang Department of CSIE YUST. Copy Right Reserved 59 .• For the memory location whose physical address is specified by 1256Ah.P.

P. Copy Right Reserved 60 .• • • • 1256Ah=12560h+X 1256Ah=12400h+Y X=Ah.Fang Department of CSIE YUST.Y=16Ah 1256Ah=1256:000A=1240:016A W.

P.• A memory location has physical address 80FD2h.Fang Department of CSIE YUST. Copy Right Reserved 61 . In what segment does it have offset BFD2h W.

Copy Right Reserved 62 .P.Fang Department of CSIE YUST.• • • • Physical address=segmentx10h+offset Segmentx10h=physical address-offset 80FD2h-BFD2h=75000h àsegment 7500h W.

Pointer and index register • SP – stack pointer – Used in conjunction with SS for accessing stack segment.P. – Unlike SP. • BP – Base pointer – Used primarily to access data on the stack.Fang Department of CSIE YUST. Copy Right Reserved 63 . we can also use BP to access data in the other segment W.

• SI – Source Index – Point to memory locations in the data segment addressed by DS. Copy Right Reserved 64 . – By incrementing the contents of SI.P. we can easily access consecutive memory locations • DI – Destination Index – Same functions as SI – For string operation – Access memory locations addressed by ES W.Fang Department of CSIE YUST.

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. Copy Right Reserved 65 .P.Fang Department of CSIE YUST.

W. Copy Right Reserved 66 .Fang Department of CSIE YUST.Flags • Status flags – Ex:ZF(zero flag) • Controls – Ex: IF(interrupt flag) =0 à input from the keyboard are ignored by the processor.P.

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.Fang Department of CSIE YUST.P. Copy Right Reserved 67 .

Copy Right Reserved 68 .COM W.P.• Dos routine that services user commands is called COMMAND.Fang Department of CSIE YUST.

P.BIOS • Machine specific • DOS I/O operations are ultimately carried out by the BIOS routines • Addresses of the BIOS routine – interrupt vectors W.Fang Department of CSIE YUST. Copy Right Reserved 69 .

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

P.Port address 20h-21h 60h-63h 200h-20fh 2f8h-2ffh 320h-32fh 378h-37fh 3c0h-3cfh 3d0h-3dfh 3f8h-3ffh description Interrupt controller Keyboard controller Game controller Serial port (COM2) Hard disk Parallel printer port 1 EGA CGA Serial port (COM1) W.Fang Department of CSIE YUST. Copy Right Reserved 71 .

Copy Right Reserved 72 .P.Fang Department of CSIE YUST.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.

Fang Department of CSIE YUST.Introduction to IBM PC Assembly Language W. Copy Right Reserved 73 .P.

P. Copy Right Reserved 74 .Fang Department of CSIE YUST.Introduction to IBM PC Assembly Language • Syntax – Name operation operand(s) comment • Ex: – START: MOV CX.5 .initialize counter – MAIN PROC W.

• Name Field – 1 to 31 characters long – Letters.Fang Department of CSIE YUST. digits.P. Copy Right Reserved 75 .@_$% – Embeded blanks are not allowed W. special characters ?.

or two operand – Ex: – NOP – INC AX – ADD WORD1.Fang Department of CSIE YUST.P.2 W.Operation feilds • Operands field – An instruction may have zero. one . Copy Right Reserved 76 .

Fang Department of CSIE YUST.P.0 . Copy Right Reserved 77 .move 0 to CX • Permissible to make an entire line a comment W. • Ex: – MOV CX.Comment fields • A semicolon marks the beginning of this field • The assembler ignores anything typed after the semicolon.

P.Fang Department of CSIE YUST.Program data • The processor operates only on binary data • The assembler must translate all data representation into binary number W. Copy Right Reserved 78 .

NUMBER 11011 11011B 64223 -21843D 1.Fang Department of CSIE YUST. Copy Right Reserved 79 .P.234 1B4DH 1B4D FFFH 0FFFFH ¢âYPE Decimal Binary Decimal Decimal Illegal– contains a nondigit character Hex Illegal hex number – doesn’ end in t “H” Illegal hex number – doesn’ begin t with a decimal digit Hex W.

characters Pseudo-op DB DW DD DQ DT Stands for Define byte Define word Define double word Define quaword Define tenbytes W. Copy Right Reserved 80 .P.Fang Department of CSIE YUST.

P. Copy Right Reserved 81 .• Byte variable – Name DB initial_value • Ex: – ALPHA DB 4 W.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.

43H – MSG DB ‘ HELLO’ .• Array – B_ARRAY DB 10H.30H • String – LETTERS DB ‘ ABC’ – LETTERS DB 41H.Fang Department of CSIE YUST.20H.0DH.’ $’ W. Copy Right Reserved 83 .42H.P.0AH.

Copy Right Reserved 84 .Fang Department of CSIE YUST.P.• Named Constanted – EQU (Equates) – Name EQU constant • Ex: – LF EQU 0AH – PROMPT EQU ‘ TYPE YOUR NAME’ W.

variables • • • • • Byte variables Name DB initial_value Ex: ALPHA DB BYT DB ? W.Fang Department of CSIE YUST.P. Copy Right Reserved 85 .

Word variables • Name • Ex: DW initial_value – WRD DW -2 W.P.Fang Department of CSIE YUST. Copy Right Reserved 86 .

array • B_ARRAY DB 10H. Copy Right Reserved 87 .20H.30H Symbol B_ARRAY Address 200h contents 10h 20h 30h B_ARRAY+1 201h B_ARRAY 202h W.P.Fang Department of CSIE YUST.

43H MSG DB ‘ HELLO’ .0DH.0AH.42H. Copy Right Reserved 88 .P.0DH.Fang Department of CSIE YUST.0AH.4CH.45H.4CH.24H W.’ $’ MSG DB 48H.4FH.Character string • • • • LETTERS DB ‘ ABC’ LETTERS DB 41H.

Names constants • Name • Ex: – – – – – EQU constant LF EQU 0AH MOV DL.0AH MOV DL.Fang Department of CSIE YUST.P.LF Prompt EQU ‘ TYPE YOUR NAME’ MSG DB PROMPT • Note:no memory is allocated for EQU names W. Copy Right Reserved 89 .

P. Copy Right Reserved 90 .Fang Department of CSIE YUST.A few basic instructions • • • • • • • MOV XCHG ADD SUB INC DEC NEG W.

source General Destination register operand Source operand General register Segment register Memory location yes Yes Segment register Memory location constant Yes No Yes Yes No No Yes Yes No No 91 W.• MOV destination. Copy Right Reserved .Fang Department of CSIE YUST.P.

source Destination General operand register Source operand Generation Yes register Memory location Yes W.Fang Department of CSIE YUST.• XCHG destination.P. Copy Right Reserved Memory location Yes no 92 .

WORD1 05 AH 00 BH W.P. Copy Right Reserved 1A AH 00 BH 00 AL 05 BL 00 AL 1A BL 93 .• XCHG AH.Fang Department of CSIE YUST.BL • XCHG AX.

illegal • MOV AX.AX W. Copy Right Reserved 94 .Restrictions on MOV and XCHG • MOV or XCHG between memory location is not allowed • Ex: MOV WORD1.WORD2 • MOV WORD1.WORD2 .Fang Department of CSIE YUST.P.

source Destination General register operand Source operand General register Memory location Yes Yes Memory location Yes No Constant Yes yes 95 W.P.Fang Department of CSIE YUST. source • SUB destination. Copy Right Reserved .• ADD destination.

• INC destination • DEC destination • Ex: – INC WORD1 – DEC BYTE1 W.P. Copy Right Reserved 96 .Fang Department of CSIE YUST.

Copy Right Reserved 97 .P.• NEG destination • By two’ complement s • Ex: – NEG BX W.Fang Department of CSIE YUST.

W.P.Type agreement of operands • The operands of the preceding twooperand instruction must be of the same type. Copy Right Reserved 98 .Fang Department of CSIE YUST.

Copy Right Reserved 99 .Fang Department of CSIE YUST.P.A – MOV A.5 W.AX – NEG A – ADD A.A – MOV B.Translation of High-Level language to assembly labguage • B=A – MOV AX.AX • A=5-A – MOV AX.5 – SUB AX.

• A=B-2*A – – – – MOV SUB SUB MOV AX.Fang Department of CSIE YUST.B AX. Copy Right Reserved 100 .AX W.A A.A AX.P.

MODEL memory_model W.P.Fang Department of CSIE YUST. Copy Right Reserved 101 .• .

Program structure (Memory model) Model SMALL MEDIUM COMPACT LARGE Description Code in one segment Data in one segment Code in more than one segment Data in one segment Code one segment Data in more than one segment Code in more than one segment Data in more than one segment No array larger than 64k bytes Code in more than one segment Data in more one segment Arrays may be larger than 64k bytes HUGE W.Fang Department of CSIE YUST.P. Copy Right Reserved 102 .

P. 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.Fang Department of CSIE YUST.

Stack segment • .P.STACK size 100H • Ps: if size is omitted 1KB is set aside for the stack data W.Fang Department of CSIE YUST.STACK • Ex: – . Copy Right Reserved 104 .

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

CODE MAIN PROC .other procedures go here W.P.main procedure instructions MAIN ENDP .• Ex: – – – – – .Fang Department of CSIE YUST. Copy Right Reserved 106 .

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

Fang Department of CSIE YUST.P. 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’ much easier to program I/O with the s service routines provided by the manufacturer W.

INT instruction • INT interrupt_number W.Fang Department of CSIE YUST. Copy Right Reserved 109 .P.

Copy Right Reserved 110 .INT 21h Function number 1 2 9 routine Single-key input Single-character output Character string outpur W.Fang Department of CSIE YUST.P.

Copy Right Reserved 111 .Fang Department of CSIE YUST.INT 21h Function 1: • Single-key input • Input: – Ah=1 • Output: • AL=ASCII code if character key is pressed =0 if non-character key is pressed W.P.

Copy Right Reserved 112 .Fang Department of CSIE YUST.P.• MOV AH.1 • INT 21h W.

Fang Department of CSIE YUST.INT 21h Function 2: • Display a character or execute a control function: – Ah=2 – DL=ASCII code of the display character or control character • Output: • AL=ASCII code of the display character or control character W. Copy Right Reserved 113 .P.

’ ?’ • INT 21h W.• MOV Ah.P.2 • MOV DL.Fang Department of CSIE YUST. Copy Right Reserved 114 .

Fang YUST. Copy Right Reserved Function Beep (sounds a tone) Backspace Tab Line feed (new line) Carriage return 115 .ASCII code (HEX) 7 8 9 A D Symbol BEL BS HT LF CR Department of CSIE W.P.

Copy Right Reserved 116 .P.INT 21h Function 9: • Display a string: – DX=offset address of string – The string must end with a ‘ $’character. W.Fang Department of CSIE YUST.

Copy Right Reserved 117 .P.• Ex: – MSG DB ‘ Hello!$’ W.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.LEA • INT21h. function 9. source • EX: – LEA DX.MSG W.P. expects the offset address of the character string to be DX • LEA destination. Copy Right Reserved 118 .

Fang Department of CSIE YUST. Copy Right Reserved 119 . • 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.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.

P.@DATA • MOV DS.AX W.Fang Department of CSIE YUST. Copy Right Reserved 120 .• MOV AX.

Terminating a program • INT 32h function 4Ch W.Fang Department of CSIE YUST. Copy Right Reserved 121 .P.

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

@DATA MOV DS.9 INT 21h MOV AH.CODE MAIN PROC MOV AX.TITLE PGM4_2:PRINT STRING PROGRAM .Fang Department of CSIE YUST.MODEL SMALL .P.4CH INT 21h MAIN ENDP END MAIN W.AX LEA DX.STACK 100H . Copy Right Reserved 123 .DATA MSG DB ‘ HELLO!$’ .MSG MOV AH.

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

Fang Department of CSIE YUST.MSG2 MOV AH.4CH INT 21h MAIN ENDP END MAIN W.MOV CHAR.AL LEA DX. Copy Right Reserved 125 .P.9 INT 21h MOV AH.

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

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

P.• 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.Fang Department of CSIE YUST. Copy Right Reserved 128 .

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.The processor status and the FLAGS Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 OF DF IF TF SF ZF AF PF CF W.P. Copy Right Reserved 130 .

and 11 • Control flags are located in bits 8.Fang Department of CSIE YUST.• The status flags are located in bits 0.P.9. and 10 • The other bits have no significance W. Copy Right Reserved 131 .

P. the zero flag become 1 W. Copy Right Reserved 132 .Status flags • Reflect the result of an operation • Ex: – If SUB AX.AX is executed.Fang Department of CSIE YUST.

P.Fang Department of CSIE YUST. Copy Right Reserved symbol CF PF AF ZF SF OF 133 .Status Flags bit 0 2 4 6 7 11 name Carry flag Parity flag Auxiliary carry flag Zero flag Sign flag Overflow flag W.

Control Flags bit 8 9 10 name Trap flag Interrupt flag Direction flag Symbol TF IF DF W. Copy Right Reserved 134 .Fang Department of CSIE YUST.P.

P. 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.Fang Department of CSIE YUST.

P. Copy Right Reserved 136 .Fang Department of CSIE YUST.Parity flags • PF=1 if – The low byte of a result has an even number of one bits W.

Fang Department of CSIE YUST.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. Copy Right Reserved 137 .P.

Fang Department of CSIE YUST.Zero flag • ZF=1 for a zero result • ZF=0 for a nonezero result W.P. Copy Right Reserved 138 .

P. Copy Right Reserved 139 .Fang Department of CSIE YUST.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.

Fang Department of CSIE YUST.Overflow Flag • OF=1 if signed overflow occurred W.P. Copy Right Reserved 140 .

W. Copy Right Reserved 141 .P.Fang Department of CSIE YUST.Overflow • Is associated with the fact that the range of numbers that be represented in a computer is limited.

P.Overflow examples • Four possible outcome – – – – No overflow Signed overflow Unsigned overflow Both signed and unsigned overflow W. Copy Right Reserved 142 .Fang Department of CSIE YUST.

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

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

P. the processor does not interpret the result as either signed or unsigned.How the processor indicates overflow • OF=1 for signed overflow • CF=1 for unsigned overflow • In determining overflow.Fang Department of CSIE YUST. Copy Right Reserved 145 . • Programmer shall interpreting the result W.

Means that the correct answer is larger than the biggest unsigned number – On substraction.P.Fang Department of CSIE YUST. means that the correct answer is smaller than 0 W.How the processor determines that overflow occurred • Limit the discussion to addition and substraction • Unsigned overflow – On addition. Copy Right Reserved 146 .

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.Fang Department of CSIE YUST.P. Ex: A-(-B)=A+B -A-(+B)=-A+ -B Occur if the result has a different sign than expected W.

overflow is impossible • In substraction of numbers with the same sign cannot give overflow W.Fang Department of CSIE YUST.P.• In addition of number with different signs. Copy Right Reserved 148 .

Fang Department of CSIE YUST.• 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. Copy Right Reserved 149 .P.

Copy Right Reserved 150 .Fang Department of CSIE YUST.P.How Instructions affect the flags instruction Affects flags MOV/XCHG None ADD/SUB All INC/DEC All except CF NEG All (CF=1 unless result is 0 OF=1 if word operand is 8000h Or byte operand is 80h W.

example • ADD AX. Copy Right Reserved 151 .BX. where AX contains FFFFh.BX contains FFFh W.P.Fang Department of CSIE YUST.

P.BL contains 80h W.Fang Department of CSIE YUST. Copy Right Reserved 152 .BL.example • ADD AL. where AL contains 80h.

P.Fang Department of CSIE YUST.example • SUB AX.BX. Copy Right Reserved 153 . where AX contains 8000h and BX contains 0001h W.

P. where AL contain FFh W. Copy Right Reserved 154 .example • INC AL.Fang Department of CSIE YUST.

P.-5 W.Fang Department of CSIE YUST.example • MOV AX. Copy Right Reserved 155 .

Fang Department of CSIE YUST.P. where AX contains 8000h W.example • NEG AX. Copy Right Reserved 156 .

Copy Right Reserved 157 .Fang Department of CSIE YUST.The DEBUG Program • An environment in which a program may be tested. W.P.

0FFFFH NEG AX INC AX MOV AH.4000H ADD AX. Copy Right Reserved 158 .Fang Department of CSIE YUST.CODE MAIN PROC MOV AX.P.4CH INT 21H MAIN ENDP END MAIN W.TITLE PGM5_1:Check flags .AX SUB AX.STACK 100H .MODELSMALL .

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. Copy Right Reserved 159 .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.Fang Department of CSIE YUST.-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.

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. Copy Right Reserved 160 .Fang Department of CSIE YUST.-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.

P.Fang Department of CSIE YUST.4C -g Program terminated normally -q W. 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.

e W.P.e 2.Fang Department of CSIE YUST.d 4. Copy Right Reserved 162 .b 3.homework • Ch5 – – – – 1.

Copy Right Reserved 163 .Fang Department of CSIE YUST.P.Flow Control Instructions W.

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.Fang Department of CSIE YUST.P.

P.2 MOV CX. Copy Right Reserved 165 .0 PRINT_LOOP: int 21h INC DL DEC CX JNE PRINT_LOOP MOV AH.STACK 100H .MODEL SMALL .4CH INT 21h MAIN ENDP END MAIN W.256 MOV DL.Fang Department of CSIE YUST.Jump example • • • • • • • • • • • • • • • • • TITLE PGM6_1:IBM CHARACTER DISPLAY .CODE MAIN PROC MOV AH.

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

• JNZ:jump if not zero • CX:loop counter • PRINT_LOOP:label W.P.Fang Department of CSIE YUST. Copy Right Reserved 167 .

P.Conditional jump • Jxxx destination_label W.Fang Department of CSIE YUST. Copy Right Reserved 168 .

P.Conditional jumps • Range of a conditional jump – Precede no more than 126 bytes – Follow it by no more than 127 bytes W. Copy Right Reserved 169 .Fang Department of CSIE YUST.

P. 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 IP is not altered W.Fang Department of CSIE YUST. • If the condition is false. the CPU adjusts the IP to point to the destination label.

P.transfer to label • If ZF=1. next line W.• JNZ – Inspecting ZF • If ZF=0. Copy Right Reserved 171 .Fang Department of CSIE YUST.

Conditional jumps • Three categories – The signed jumps – Unsigned jumps – Single-flag jumps • Ps: jump instruction do not affect the flags W.P. Copy Right Reserved 172 .Fang Department of CSIE YUST.

P. ZF=0 and SF=OF JGE/JNL SF=OF JL/JNGE SF<>OF JLE/JNG ZF=1 or SF<>OF 173 .Fang Department of CSIE Jump ifCopy Right Reserved not great than YUST.Signed jumps symbol description Condition for jumps JG/JNLE Jump if greater than Jump if not less than or equal to Jump if greater than or equal to Jump if not less than or equal to Jump if less than Jump if not greater than or equal Jump if less than or equal W.

Copy Right Reserved CF=0 and ZF=0 JAE/JNB CF=0 JB/JNAE CF=1 JBE/JNA CF=1 or ZF=1 174 .P.Fang Department of CSIE YUST.Unsigned conditional jumps Symbol description Condition for jumps JA/JNBE Jump if above Jump if not below or equal Jump if above or equal Jump if not below Jump if below Jump if not above or equal Jump if equal Jump if not above W.

Fang Department Jump if parity odd of CSIE YUST.Single-Flag jumps symbol JE/JZ JNE/JNZ JC JNC JO JNO JS JNS JP/JPE JNP/JPO description Jump if equal Jump if equal to zero jump if not equal Jump if not zero Jump if carry Jump if no carry Jump if overflow Jump if no overflow Jump if sign negative Condition for jumps ZF=1 ZF=0 CF=1 CF=0 OF=1 OF=0 SF=1 Jump if nonnegative sign SF=0 Jump if parity even W.P. Copy Right Reserved PF=1 PF=0 175 .

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

• • • • • CMP AX. Copy Right Reserved 177 .P.BX JG BELOW Where AX=7.Fang Department of CSIE YUST.BX=0001 Result is 7FFFh-0001=7FFEh ZF=SF=OF=0 W.

Fang Department of CSIE YUST. Copy Right Reserved 178 .• DEC AX • JL THERE W.P.

Fang Department of CSIE YUST.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. Copy Right Reserved 179 .P.

• 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. Copy Right Reserved 180 .Fang Department of CSIE YUST.

CX JLE NEXT MOVE CX.AX CMP BX.P.Working with Characters • Example.Fang Department of CSIE YUST.BX NEXT: W. write some code to put the biggest one in CX • Solution: – – – – – MOV CX. Suppose AX and BX contain signed numbers. Copy Right Reserved 181 .

The JMP instruction • JMP destination • JMP can be used to get around the range restriction of a conditional jump W.P. Copy Right Reserved 182 .Fang Department of CSIE YUST.

P.Fang Department of CSIE YUST.BX W. Copy Right Reserved 183 .• • • • • TOP: <<if too far>> DEC CX JNZ TOP MOV AX.

Copy Right Reserved 184 .Fang Department of CSIE YUST.• TOP: – DEC CX – JNZ BOTTOM – JMP EXIT • BOTTOM: – JMP TOP • EXIT: – MOV AX.P.BX W.

Fang Department of CSIE YUST.P. Copy Right Reserved 185 .High-Level Language structure • • • • • • IF-THEN IF-THEN-ELSE CASE Branch with compound conditions AND conditions OR conditions W.

IF-THEN • IF condition is true • THEN – Execute true-branch statements • END_IF W. Copy Right Reserved 186 .P.Fang Department of CSIE YUST.

P.• Ex :replace the number in AX by its absolute value W.Fang Department of CSIE YUST. Copy Right Reserved 187 .

• IF AX<0 • THEN – Replace AX by –AX • END_IF W.P. Copy Right Reserved 188 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST.0 – JNL END_IF – NEG AX • END_IF: W. Copy Right Reserved 189 .• CMP AX.P.

IF-THEN-ELSE • IF condition is true – THEN • Execute true-branch statements – ELSE • Execute false-branch statements – END_IF W.Fang Department of CSIE YUST.P. Copy Right Reserved 190 .

Display the one the comes first in the character sequence W.Fang Department of CSIE YUST. Copy Right Reserved 191 .P.• Ex: suppose AL and BL contain extended ASCII characters.

• IF AL<=BL – THEN • Display the character in AL – ELSE • Display the character in BL – END_IF W.Fang Department of CSIE YUST.P. Copy Right Reserved 192 .

AL JMP DISPLAY • ELSE_: – MOV DL.– – – – – MOV AH.Fang Department of CSIE YUST. Copy Right Reserved 193 .2 CMP AL.P.BL JNBE ELSE_ MOV DL. W.BL • DISPLAY: – INT 21h • END_IF • Ps: the label ELSE_ is used because ELSE is a reserved word.

Exercise • Write assembly code for each of the following decision structures • a.Fang Department of CSIE YUST. – IF AX<0 – THEN • PUT -1 IN BX – END_IF • b. Copy Right Reserved 194 .P. • IF AL<0 – THEN put FFh in AH • Put FFh in AH – ELSE • Put 0 in AH – END_IF W.

Copy Right Reserved 195 .Fang Department of CSIE YUST.P.Exercise • C. • Suppose DL contains the ASCII code oa a character • (IF DL>=“A”) AND (DL<= ‘ ) Z’ • THEN – Display DL • END_IF W.

– If AX<BX • THEN – IF BX<CX » THEN » Put 0 in AX – ELSE » Put 0 in BX – END_IF • END_IF W.• D.Fang Department of CSIE YUST.P. Copy Right Reserved 196 .

Copy Right Reserved 197 .P.•E • IF(AX<BX) OR (BX<CX) – THEN • Put 0 in BX – ELSE • Put 1 in DX – END_IF W.Fang Department of CSIE YUST.

3a.P.HW • Ch6.12 W.Fang Department of CSIE YUST. Copy Right Reserved 198 .

Fang Department of CSIE YUST. Copy Right Reserved 199 .P.CASE • Multiway branch structure • CASE expression – – – – – Value1:statement 1 Value2:statement 2 : : Valuen:statement n • END_CASE W.

if AX contains a positive number.if AX contains 0.Fang Department of CSIE YUST. W.ex • If AX contains a negative number.P. put 0 in BX. put -1 in BX. Copy Right Reserved 200 .put 1 in BX.

P. Copy Right Reserved 201 .Fang Department of CSIE YUST.• CASE AX – <0:put -1 in BX – =0:put 0 in BX – >0:put 1 in BX W.

Copy Right Reserved 202 .1 • END_CASE: W.0 JL NEGATIVE JE ZERO JG POSITIVE • NEGATIVE: – MOV BX.0 – JMP END_CASE • POSITIVE: – MOV BX.Fang Department of CSIE YUST.P.– – – – CMP AX.-1 – JMP END_CASE • ZERO: – MOV BX.

Fang Department of CSIE YUST. if AL contains 2 or 4. display “e” W. display “0”.P. Copy Right Reserved 203 .ex • If AL contains 1 or 3.

P.3:display ‘ o’ – 2.• CASE AL – 1.Fang Department of CSIE YUST.4:display ‘ e’ W. Copy Right Reserved 204 .

’ e’ MOV AH.4 JE EVEN JMP END_CASE MOV DL.– – – – – – – – – CMP AL.1 JE ODD CMP AL. Copy Right Reserved 205 .’ o’ JMP DISPLAY MOV DL.P.2 JE EVEN CMP AL.Fang Department of CSIE YUST.3 JE ODD CMP Al.2 INT 21H • • • • ODD: – – – – – EVEN: DISPLAY: END_CASE: W.

Copy Right Reserved 206 .Branches with compound conditions • Condition_1 AND condition_2 • Comdition_1 OR condition_2 W.Fang Department of CSIE YUST.P.

Copy Right Reserved 207 .display it W.AND conditions • Ex:read a character. and if it’ an d uppercase letter.Fang Department of CSIE YUST.P.

Copy Right Reserved 208 .P.Fang Department of CSIE YUST.• Read a character (into AL) • IF (‘ A’<= character) and (character <=‘ ) Z’ • THEN – Display character • END_IF W.

’ A’ JNGE END_IF CMP AL. Copy Right Reserved 209 .1 INT 21H CMP AL.’ Z’ JNLE END_IF MOV DL.2 INT 21H • END_IF W.Fang Department of CSIE YUST.AL MOV AH.P.– – – – – – – – – MOV AH.

OR conditions • Read a character. W. otherwise. terminate the program.Fang Department of CSIE YUST. Copy Right Reserved 210 . display s it. IF it’ “y” or “Y”.P.

Copy Right Reserved 211 .• Read a character (into AL) • IF (character=‘ ) OR (character=‘ ) y’ Y’ – THEN • Display it – ELSE • Terminate the program – END_IF W.Fang Department of CSIE YUST.P.

AL INT 21H JMP END_IF • THEN: • ELSE_: – MOV AH.’ Y’ JE THEN JMP ELSE MOV AH.P.2 MOV DL.’ y’ JE THEN CMP AL.1 INT 21H CMP AL.Fang Department of CSIE YUST.4CH – INT 21H • END_IF W.– – – – – – – – – – – MOV AH. Copy Right Reserved 212 .

Looping structures • • • • FOR LOOP WHILE LOOP REPEAT LOOP WHILE versus REPEAT W.Fang Department of CSIE YUST. Copy Right Reserved 213 .P.

P.Fang Department of CSIE YUST. Copy Right Reserved 214 .FOR LOOP • FOR loop_count times DO – Statements • END_FOR W.

the next instruction after LOOP is done.Fang Department of CSIE YUST.P.Execution of the LOOP instruction causes CX to be decremented automatically • If CX is not 0. • Destination_label must precede the LOOP instruction by no more than 126 bytes W.• LOOP destination_label • The counter for the loop is the register CX which is initialized to loop_count. control transfers to destination_label • If CX=0. Copy Right Reserved 215 .

Fang Department of CSIE YUST. Copy Right Reserved 216 .P.• TOP: – LOOP TOP W.

Copy Right Reserved 217 .ex • Write a count-controlled loop to display a row of 80 stars W.Fang Department of CSIE YUST.P.

P.• FOR 80 times DO – Display ‘ *’ • END_FOR W. Copy Right Reserved 218 .Fang Department of CSIE YUST.

80 – MOV AH.2 – MOV DL.’ *’ • TOP: – INT 21h – LOOP TOP W. Copy Right Reserved 219 .P.– MOV CX.Fang Department of CSIE YUST.

the instruction JCXZ(jump if CX is zero) may be used before the loop W. Copy Right Reserved 220 .• Notice: – FOR loop implement with a LOOP instruction.P. the LOOP instruction cause CX to be decremented to FFFFh – To prevent this.Fang Department of CSIE YUST. is executed at least once – If CX contains 0 when the loop is entered.

• JCXZ destination_label W. Copy Right Reserved 221 .Fang Department of CSIE YUST.P.

P. Copy Right Reserved 222 .Fang Department of CSIE YUST.– JCXZ SKIP • TOP: – LOOP TOP • SKIP: W.

WHILE LOOP • WHILE condition DO – Statements • END_WHILE W.Fang Department of CSIE YUST.P. Copy Right Reserved 223 .

ex • Write some code to count the number of characters in an input line W.P.Fang Department of CSIE YUST. Copy Right Reserved 224 .

• Initialize count to 0 • Read a character • WHILE character<> carriage_return DO – Count=count+1 – Read a character • END_WHILE W.Fang Department of CSIE YUST.P. Copy Right Reserved 225 .

– MOV DX.0DH JE END_WHILE INC DX INT 21H JMP WHILE_ • END_WHILE W. Copy Right Reserved 226 .1 – INT 21H • WHILE_: – – – – – CMP AL.Fang Department of CSIE YUST.P.0 – MOV AH.

REPEAT LOOP • REPEAT • Statements • UNTIL condition W.Fang Department of CSIE YUST.P. Copy Right Reserved 227 .

ex • Write some code to read characters until a blank is read W. Copy Right Reserved 228 .P.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P. Copy Right Reserved 229 .• REPEAT – Read a character • UNTIL character is a blank W.

1 • REPEAT: – INT 21H – CMP AL.– MOV AH. Copy Right Reserved 230 .’‘ – JNE REPEAT W.Fang Department of CSIE YUST.P.

P.Fang Department of CSIE YUST.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. Copy Right Reserved 231 . W.

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.

P.• Display the opening message • Read and process a line of text • Display the results W.Fang Department of CSIE YUST. Copy Right Reserved 233 .

Fang Department of CSIE YUST.PROMPT INT 21H MOV AH.STACk 100H .CODE MAIN PROC MOV AX.0DH.9 LEA DX.'No capitals $' CAP_MSG DB 0DH.'First capital = ' FIRST DB ']' DB ' Last capital = ' LAST DB '@ $' .0AH.TITLE PGM6_2:FIRST AND LAST CAPITALS .DATA PROMPT DB 'Type a line of text'. Copy Right Reserved 234 .@DATA MOV DS.0AH.0AH.'$' NOCAP_MSG DB 0DH.MODEL SMALL .P.AX MOV AH.1 INT 21H WHILE_: W.

'A' JNGE END_IF CMP AL.0DH JE END_WHILE CMP AL.'Z' JNLE END_IF CMP AL. Copy Right Reserved 235 .AL END_IF: INT 21H JMP WHILE_ END_WHILE: W.FIRST JNL CHECK_LAST MOV FIRST.LAST JNG END_IF MOV LAST.CMP AL.AL CHECK_LAST: CMP AL.Fang Department of CSIE YUST.P.

NOCAP_MSG JMP DISPLAY CAPS: LEA DX.CAP_MSG DISPLAY: INT 21H MOV AH.MOV AH. Copy Right Reserved 236 .Fang Department of CSIE YUST.P.']' JNE CAPS LEA DX.9 CMP FIRST.4CH INT 21H MAIN ENDP END MAIN W.

Fang Department of CSIE YUST.12 W.hw • Ch6.3a. Copy Right Reserved 237 .P.

P. Shift .and Rotate Instructions W.Fang Department of CSIE YUST. Copy Right Reserved 238 .Logic.

Logic.and Rotate Instructions • Logic instructions – – – – AND OR XOR NOT W.Fang Department of CSIE YUST. Copy Right Reserved 239 . Shift.P.

P.Logic instructions • Perform the following logic operations – – – – 10101010 AND 11110000 10101010 OR 11110000 10101010 XOR 11110000 NOT 10101010 W. Copy Right Reserved 240 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST.1=true) a 0 0 1 1 b 0 1 0 1 A AND b 0 0 0 1 A OR b 0 1 1 1 A XOR b 0 1 1 0 241 W.Truth tables (0=false . Copy Right Reserved .P.

P. source ------------------------------------------Effect on flags – SF.PF reflect the result – AF is undefined – CF. source XOR destination.OF=0 W.Fang Department of CSIE YUST.ZF.instructions • • • • • AND destination. source OR destination. Copy Right Reserved 242 .

P.Fang Department of CSIE YUST. • mask W. Copy Right Reserved 243 . OR. and XOR is to selectively modify the bits in the destination.• One use of AND.

Copy Right Reserved 244 .b AND 1 =b b AND 0=0 b OR 0 =b b OR 1 =1 b XOR 0=b b XOR 1=~b (compelemet of b) W.P.Fang Department of CSIE YUST.

• The XOR instruction can be used to complement specific destination bits while preserving the others.P.• 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.Fang Department of CSIE YUST. Copy Right Reserved 245 . W.

Fang Department of CSIE YUST. Copy Right Reserved 246 .ex • Clear the sign bit of AL while leaving the other bits unchanged W.P.

P. Copy Right Reserved 247 .7Fh • Ps: • 01111111b=7Fh W.• AND AL.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P.Ex • Set the most significant and least significant bits of AL while preserving the other bits W. Copy Right Reserved 248 .

Copy Right Reserved 249 .Fang Department of CSIE YUST.P.• OR AL.81h • Ps:10000001b=81h W.

P.ex • Change the sign bit of DX W.Fang Department of CSIE YUST. Copy Right Reserved 250 .

• XOR DX.Fang Department of CSIE YUST.P. Copy Right Reserved 251 .8000h W.

Copy Right Reserved 252 .Fang Department of CSIE YUST.20h • Method 2:AND AL.Converting an ASCII digit to a Number • “5”à 5 • Method 1:SUB AL.P.0Fh W.

Copy Right Reserved 253 .exercise • Converting a stored decimal digit to its ASCII code W.Fang Department of CSIE YUST.P.

Copy Right Reserved .P.Fang Department of CSIE YUST.Converting a lowercase letter to upper case character a b : z code 01100001 01100010 : 01111010 character A B : Z code 01000001 01000010 : 01011010 254 W.

0DFh W.P.• To convert lower to upper case we need only clear bit 5 • à AND with mask 11011111b (0DFh) • AND DL.Fang Department of CSIE YUST. Copy Right Reserved 255 .

AX – Machine needs two bytes W. must be used to clear a memory location • SUB AX.0 – Machine needs three bytes – Because prohibition on memory-on-memory operations.Clearing a Register • MOV AX.P.AX – Machine needs two bytes • XOR AX. Copy Right Reserved 256 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P. Copy Right Reserved 257 . 0 OR 0 =0 CX unchanged But affect ZF and SF W.Testing a register for zero • • • • • OR CX.0 Because 1 OR 1 =1.CX CMP CX.

P. Copy Right Reserved 258 .Fang Department of CSIE YUST.NOT instruction • NOT destination W.

ex • Complement the bits in AX W.Fang Department of CSIE YUST.P. Copy Right Reserved 259 .

Copy Right Reserved 260 .P.Fang Department of CSIE YUST.• NOT AX W.

Fang Department of CSIE YUST.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. Copy Right Reserved 261 .

Copy Right Reserved 262 .Fang Department of CSIE YUST.P.OF=0 W.• TEST destination. PF reflect the result – AF is undefined – CF.ZF.source • -----------------------------------• Effect on flags – SF.

the result will be 0 and so ZF=1 W.mask • If destination has 0’ in all the etsted s position. Copy Right Reserved 263 .Examining bits • TEST destination.P.Fang Department of CSIE YUST.

ex • Jump to label BELLOW if AL contains an even number W.P.Fang Department of CSIE YUST. Copy Right Reserved 264 .

the mask is 00000001b=1 • TESt AL.Fang Department of CSIE YUST. thus.• Even numbers have a 0 in bit 0.1 • JZ BELLOW W. Copy Right Reserved 265 .P.

CL W.Shift instructions • Opcode destination.Fang Department of CSIE YUST.P. Copy Right Reserved 266 .

ZF reflect the result AF is undefined CF=last bit shifted out OF=1 if result changes sign on last shift W.Left Shift instructions • • • • SHL destination.PF.1 SHL destination. Copy Right Reserved 267 .P. CL --------------------------------Effect on flags – – – – SF.Fang Department of CSIE YUST.

CL is executed? W.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.P.

P.• DH=10001010 •à • 01010000b=50h W. Copy Right Reserved 269 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P.Multiplication by Left Shift • A left shift on a binary number multiplies it by 2. • Ex: • 00000101b = 5d • 00001010b =10d • 00010100b = 20d W. Copy Right Reserved 270 .

Copy Right Reserved 271 .SAL • Shift arithmetic left • the same machine code with SHL W.Fang Department of CSIE YUST.P.

• Negative number can also be multiplied by powers of 2 by left shifts • Ex: – FFFFh(-1) à FFF8h(-8) W.P. Copy Right Reserved 272 .Fang Department of CSIE YUST.

Fang Department of CSIE YUST. and CF.P. • A multiple shift is really a series of single shifts.OF only reflect the result of the last shift W.Overflow • The overflow flags are not reliable indicators for a multiple left shift. Copy Right Reserved 273 .

P.Fang Department of CSIE YUST.CL à CF=OF=0 (this occur signed and unsigned overflow) W.ex • • • • • BL=80h CL=2 SHL BL. Copy Right Reserved 274 .

ex • Write some code to multiply th evalue of AX by 8 W.P.Fang Department of CSIE YUST. Copy Right Reserved 275 .

Copy Right Reserved 276 .P.Fang Department of CSIE YUST.3 • SAL AX.• MOV CL.CL W.

1 SHR destination.Right Shift instruction • • • • SHR destination. Copy Right Reserved 277 . CL -------------------------------Effect on the flags is the same as for SHL W.P.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P.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. Copy Right Reserved 278 .

Fang Department of CSIE YUST.P.• 10001010 •à • 00100010b=22h W. Copy Right Reserved 279 .

SAR • • • • • Shift arithmetic right SAR destination.1 SAR destination.P. Copy Right Reserved 280 . CL The effect on flags is the same as for SHR Different with SHR : – The msb retains its original value W.Fang Department of CSIE YUST.

Fang Department of CSIE YUST. Copy Right Reserved 281 .P.Division by right shift • 00000101b=5 • 00000010b=2 W.

Fang Department of CSIE YUST.Signed and unsigned division • Signed : SAR • Unsigned: SHR W.P. Copy Right Reserved 282 .

ex • Use right shifts to divide the unsigned number 65143 by 4. Copy Right Reserved 283 . put the quotient in AX W.P.Fang Department of CSIE YUST.

65143 • MOV CL.• MOV AX. Copy Right Reserved 284 .P.2 • SHR AX. CL W.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.Fang Department of CSIE YUST.5 • We get -8 • Ps: • -15=11110001b • -8=11111000b W.P.

Fang Department of CSIE YUST.P. Copy Right Reserved 287 .General multiplication and division • • • • MUL IMUL DIV IDIV W.

Copy Right Reserved 288 .Rotate instructions • • • • • Rotate left Shift bits to left.1 ROL destination.P.Fang Department of CSIE YUST. The msb is shifted into the rightmost bit ROL destination. CL W.

CL W.• Rotate right • ROR destination. Copy Right Reserved 289 .Fang Department of CSIE YUST.P.1 • ROR destination.

Put the answer in AX W.ex • Use ROL to count the number of 1 bits in BX.Fang Department of CSIE YUST. without changing BX. Copy Right Reserved 290 .P.

– XOR AX. Copy Right Reserved 291 .Fang Department of CSIE YUST.P.1 – JNC NEXT – INC AX • NEXT: – LOOP TOP W.AX – MOV CX.16 • TOP: – ROL BX.

CL W.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.P. Copy Right Reserved 292 .Fang Department of CSIE YUST.

P. CL W.1 • RCR destination.Fang Department of CSIE YUST. Copy Right Reserved 293 .Rotate carry right • RCR destination.

CL is executed. W. CF=1.what are the alues of DH and CF after the instruction RCR DH.ex • Suppose DH contains 8Ah. and CL contains 3.P.Fang Department of CSIE YUST. Copy Right Reserved 294 .

P.Fang Department of CSIE YUST.CF Initial values After 1 right rotation After 2 right rotation After 3 right rotations 1 0 1 0 W. Copy Right Reserved DH 10001010 11000101 01100010 10110001=B1h 295 .

PF. Copy Right Reserved 296 .ZF reflect the result AF is undefined CF=last bit shifted out OF=1 if result changes sign on the last rotation W.Fang Department of CSIE YUST.P.• Effect of the rotate instructions on flags – – – – SF.

BL W.reversing a bit pattern • 11011100à00111011 – MOV CX.P.8 • REVERSE: – – – – SHL AL.Fang Department of CSIE YUST.1 LOOP REVERSE MOV AL. Copy Right Reserved 297 .1 RCR BL.

Copy Right Reserved 298 . and collect the bits in a register W.Binary and Hex I/O • Reads in a binary number from the keyboard. followed by a carriage return • Convert it to a bit value.P.Fang Department of CSIE YUST.

P.• 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.Fang Department of CSIE YUST. Copy Right Reserved 299 .

AL INT 21H JMP WHILE_ W.BX – MOV Ah.1 OR BL. Copy Right Reserved 300 • END_WHILE: .P.– XOR BX.1 – INT 21H • WHILE_: – – – – – – – CMP AL.0DH JE END_WHILE AND AL.0FH SHL BX.Fang Department of CSIE YUST.

P.Fang Department of CSIE YUST.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. Copy Right Reserved 301 .

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.Fang Department of CSIE YUST.P.

0FH JMP SHIFT SUB AL.– – – – XOR BX.Fang Department of CSIE YUST.AL INT 21H JMP WHILE_ • WHILE_: – – – – – – • • LETTER: – – – – – SHIFT: • END_WHILE: W.CL OR BL.1 INT 21H CMP AL.0DH JE END_WHILE CMP AL.BX MOV CL.39H JG LETTER AND AL. Copy Right Reserved 303 .4 MOV AH.P.37H SHL BX.

.. ‘ F’ • END_IF • Output character • Rotate BX left 4 times – END_FOR 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’.

homework • Ch7.4.P. Copy Right Reserved 305 .7.14 W.Fang Department of CSIE YUST.2.

P. Copy Right Reserved 306 .The Stack and Introduction to procedures W.Fang Department of CSIE YUST.

P.The stack • A stack is one-dimensional data structure. • Last-inn first-out • The most recent addition to the stack is called the top of the stack. • Items are added and removed from one end of the structure. Copy Right Reserved 307 . W.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.STACK 100H W.P. Copy Right Reserved 308 .• Declaring a stack segment • .

P. is initialized to 100h. the stack pointer. SP contains the offset address of the top of the stack. • For the preceding stack declaration.• When the program is assembled and loaded in memory. SP. W. SS will contain the segment number of the stack segment. Copy Right Reserved 309 . • When the stack is not empty.Fang Department of CSIE YUST.

• PUSH source • Ex:
– PUSH AX

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

310

• 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

311

• PUSHF:
– Pushes the contents of the FLAGS register onto the stack.

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

312

• 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

313

• POP destination • Ex:
– POP BX

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

314

• 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

315

• POPF:
– Pop the top of the stack into the FLAGS register.

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

316

• 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

317

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

318

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.

• • • • • TITLE PGM8_1:REVERSE INPUT .STACK 100H .CX MOV AH.Fang Department of CSIE YUST.’ ?’ INT 21H XOR CX. Copy Right Reserved 320 .1 INT 21H W.P.MODEL SMALL .CODE MAIN PROC – – – – – – MOV AH.2 MOV DL.

0AH INT 21H JCXZ EXIT W. Copy Right Reserved 321 • END_WHILE: .0DH JE END_WHILE PUSH AX INC CX INT 21H JMP WHILE_ MOV AH.Fang Department of CSIE YUST.P.• WHILE_: – – – – – – – – – – – – CMP AL.0DH INT 21H MOV DL.2 MOV DL.

Fang Department of CSIE YUST.P. Copy Right Reserved 322 .• TOP: – POP DX – INT 21H – LOOP TOP • EXIT: • MOV AH.4CH • INT 21H • MAIN ENDP – END MAIN W.

Fang Department of CSIE YUST.P. Copy Right Reserved 323 .Terminology of procedures • Procedure declaration – Name PROC type • RET – Name ENDP W.

Copy Right Reserved 324 . • The optional operand type is NEAR or FAR (if type is omited.• Name is the user-defined name of the procedure. – FAR – the calling statement is in a different segment W.P. NEAR is assumed) • Ps: – NEAR – the statement that calls the procedure is in the same segment as the procedure itself.Fang Department of CSIE YUST.

Copy Right Reserved 325 .Fang Department of CSIE YUST.P.• RET – Return – Causes control to transfer back to the calling procedure – Every procedure should have a RET – Except main procedure W.

Fang Department of CSIE YUST.P. do not have parameter lists W. Copy Right Reserved 326 .communication • Up to the programmer to devise a way for procedures to communicate • Unlike high-level language.

input: (where it receives information from the calling program) • .(describe what the procedure does) • .Fang Department of CSIE YUST.P.output: (where it delivers results to the calling program) • .users: (a list of procedures that it calls) W.Procedure documentation • . Copy Right Reserved 327 .

Fang Department of CSIE YUST.CALL and RET • CALL name • CALL address_expression W.P. Copy Right Reserved 328 .

• 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 329 .

P.• RET pop_value • NEAR : pop IP • FAR : pop CS:IP W. Copy Right Reserved 330 .Fang Department of CSIE YUST.

Offset address Code segment MAIN PROC 0010 0012 CALL PROC1 Next instruction P Offset Stack segment address 0200 PROC1 PROC First instruction 0100 00FE RET 00FC SP W.P.Fang Department of CSIE YUST. Copy Right Reserved 331 .

Fang Department of CSIE YUST.P. Copy Right Reserved 332 .Offset address Code segment MAIN PROC 0010 0012 CALL PROC1 Next instruction Offset Stack segment address P 0200 PROC1 PROC First instruction 0100 00FE 0012 SP RET 00FC W.

Fang Department of CSIE YUST. Copy Right Reserved 333 .Offset address Code segment MAIN PROC 0010 0012 CALL PROC1 Next instruction Offset Stack segment address 0200 PROC1 PROC First instruction 0100 00FE 0012 SP P 0300 RET 00FC W.P.

Copy Right Reserved 334 .Offset address Code segment MAIN PROC 0010 0012 CALL PROC1 Next instruction P Offset Stack segment address 0200 PROC1 PROC First instruction 0100 00FE 0300 RET 00FC SP W.Fang Department of CSIE YUST.P.

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.

Copy Right Reserved 336 .P.• 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.Fang Department of CSIE YUST.

Copy Right Reserved 337 .• Ex: – A=111b – B=1101b 111b x 1101b --------------------111 000 111 111 --------------------1011011b W.Fang Department of CSIE YUST.P.

DX REPEAT: TEST BX.Fang Department of CSIE YUST.CODE MAIN PROC CALL MULTIPLY MOV AH.1 SHR BX.1 JNZ REPEAT POP BX POP AX RET MULTIPLY ENDP END MAIN W. Copy Right Reserved 338 .4CH INT 21H MAIN ENDP MULTIPLY PROC PUSH AX PUSH BX XOR DX.P.• • • • • • • • • • • • • • • • • • • • • • • • • • TITLE PGM8_2:REVERSE INPUT .MODEL SMALL .1 JZ END_IF ADD DX.STACK 100H .AX END_IF: SHL AX.

P.4C 21 AX BX DX.DX BX.Fang Department of CSIE YUST.-u0B87:0000 E80400 CALL 0B87:0003 B44C MOV 0B87:0005 CD21 INT 0B87:0007 50 PUSH 0B87:0008 53 PUSH 0B87:0009 33D2 XOR 0B87:000B F7C30100 TEST 0B87:000F 7402 JZ 0013 0B87:0011 03D0 ADD 0B87:0013 D1E0 SHL 0B87:0015 D1EB SHR 0B87:0017 75F2 JNZ 0B87:0019 5B POP BX 0B87:001A 58 POP AX 0B87:001B C3 RET 0B87:001C 03D1 ADD 0B87:001E E6D1 OUT 0007 AH.CX D1. Copy Right Reserved 339 .1 000B DX.AL W.0001 DX.AX AX.1 BX.

.....Fang Department of CSIE YUST... -raxAX 0000:7 -rbxBX 0000:d -rAX=0007 BX=000D CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=00 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0000 NV UP EI PL NZ NA PO NC 0B87:0000 E80400 CALL 0007 -t AX=0007 BX=000D CX=001C DX=0000 SP=00FE BP=0000 SI=0000 DI=000 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...P.z...... W..... Copy Right Reserved 340 ....-rAX=0000 BX=0000 CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=00 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 #..

.0001 -dss: f0 ff0B89:00F0 23 05 00 00 0B 00 87 0B-7A 05 0D 00 07 00 03 00 #...Fang Department of CSIE YUST.P.0001 -g17 AX=001C BX=0003 CX=001C DX=0007 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 0B87:0017 75F2 JNZ 000B W..z.. -g17 AX=000E BX=0006 CX=001C DX=0007 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 0B87:0017 75F2 JNZ 000B -t AX=000E BX=0006 CX=001C DX=0007 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B 0B87:000B F7C30100 TEST BX... Copy Right Reserved BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PE CY BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PE CY BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PE NC 341 ...-gb AX=0007 BX=000D CX=001C DX=0000 SP=00FA BP=0000 SI=0000 DI=000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL ZR NA PE NC 0B87:000B F7C30100 TEST BX....

P.0001 -g17 AX=0038 BX=0001 CX=001C DX=0023 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 0B87:0017 75F2 JNZ 000B -t AX=0038 BX=0001 CX=001C DX=0023 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B 0B87:000B F7C30100 TEST BX.Fang Department of CSIE YUST. Copy Right Reserved BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PE NC BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PO CY BP=0000 SI=0000 DI=000 NV UP EI PL NZ NA PO CY BP=0000 SI=0000 DI=000 NV UP EI PL ZR NA PE CY BP=0000 SI=0000 DI=000 NV UP EI PL ZR NA PE CY 342 .-t AX=001C BX=0003 CX=001C DX=0007 SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B 0B87:000B F7C30100 TEST BX.0001 -g17 AX=0070 BX=0000 CX=001C DX=005B SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 0B87:0017 75F2 JNZ 000B -t AX=0070 BX=0000 CX=001C DX=005B SP=00FA DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0019 0B87:0019 5B POP BX W.

.Fang Department of CSIE YUST.....z.P.-t AX=0070 BX=000D CX=001C DX=005B SP=00FC BP=0000 SI=0000 DI=00 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=001A NV UP EI PL ZR NA PE CY 0B87:001A 58 POP AX -t AX=0007 BX=000D CX=001C DX=005B SP=00FE BP=0000 SI=0000 DI=00 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.... -t AX=0007 BX=000D CX=001C DX=005B SP=0100 BP=0000 SI=0000 DI=000 DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0003 NV UP EI PL ZR NA PE CY 0B87:0003 B44C MOV AH..p. Copy Right Reserved 343 ..4C -g Program terminated normally -q W.

9 W.HW • Ch8. Copy Right Reserved 344 .Fang Department of CSIE YUST.P.

Fang Department of CSIE YUST.Multiplication and division instruction W.P. Copy Right Reserved 345 .

Copy Right Reserved 346 .P.Fang Department of CSIE YUST.MUL and IMUL • The process of multiplication and division is different for signed and unsigned number • There are byte and word forms W.

Fang Department of CSIE YUST.ex • 10000000 x 1111111 – Unsigned: 128x255=32640(0111111110000000 – Signed: -128x-1=128(0000000010000000 W.P. Copy Right Reserved 347 .

Fang Department of CSIE YUST.• MUL (multiply) • IMUL (integer multiply) W.P. Copy Right Reserved 348 .

Copy Right Reserved 349 .• MUL source • IMUL source W.P.Fang Department of CSIE YUST.

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.Fang Department of CSIE YUST. but not a constant W.P. Copy Right Reserved 350 .

P.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.Fang Department of CSIE YUST. Copy Right Reserved 351 .

• 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 352 .

Effect of MUL/IMUL on the status flags • SF.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.Fang Department of CSIE YUST.ZF.P. Copy Right Reserved 353 .AF.

Fang Department of CSIE YUST.• If CF/OF=1 – The product is too big to fit in the lower half of the destination (ALor AX) W. Copy Right Reserved 354 .P.

Fang Department of CSIE YUST.ex • Suppose AX contains 1 and BX contains FFFFh W.P. Copy Right Reserved 355 .

P.Fang Department of CSIE YUST. Copy Right Reserved 356 .Instruction Decimal product Hex product DX AX CF/OF MUL BX 65535 0000FFFF 0000 FFFF 0 IMUL BX -1 FFFFFFF F FFFF FFFF 0 W.

Copy Right Reserved 357 .Fang Department of CSIE YUST.ex • Suppose AX contains FFFFh and BX contains FFFFh W.P.

Copy Right Reserved 358 .Fang Department of CSIE YUST.Instruction Decimal product Hex product DX AX CF/OF MUL BX 42948362 FFFE000 25 1 FFFE 0001 1 IMUL BX 1 00000001 0000 0001 0 W.P.

P.Fang Department of CSIE YUST. Copy Right Reserved 359 .ex • Suppose AX contains 0FFFh W.

Instruction Decimal product Hex product DX AX CF/OF MUL AX 16769025 00FFE001 00FF E001 1 IMUL AX 16769025 00FFE001 00FF 001 1 W.P.Fang Department of CSIE YUST. Copy Right Reserved 360 .

P.Fang Department of CSIE YUST. Copy Right Reserved 361 .ex • Suppose AX contains 0100h and CX contains FFFFh W.

Copy Right Reserved 362 .Fang Department of CSIE YUST.Instruction Decimal product Hex product DX AX CF/OF MUL CX 16776960 00FFFF00 00FF FF00 1 IMUL CX -256 FFFFFF0 0 FFFF FF00 0 W.P.

P.ex • Suppose AL contains 80h and BL contains FFh W.Fang Department of CSIE YUST. Copy Right Reserved 363 .

Fang Department of CSIE YUST.Instruction Decimal product Hex product AH AL CF/OF MUL BL 128 7F80 7F 80 1 IMUL BL 128 0080 00 80 1 W.P. Copy Right Reserved 364 .

and suppose there is no overflow.application • Ex : translate the high-level language assignment statement A=5xA-12xB into assembly code. Use IMUL for multiplication W. Let A and B be word variables. Copy Right Reserved 365 .P.Fang Department of CSIE YUST.

5 IMUL A MOV A.• • • • • • MOV AX.12 IMUL B SUB A. Copy Right Reserved 366 .P.AX MOV AX.AX W.Fang Department of CSIE YUST.

P.ex • Write a procedure FACTORIAL that will compute N! for a positive integer N. Suppose that overflow does not occur W. The procedure should receive N in CX and return N! in AX.Fang Department of CSIE YUST. Copy Right Reserved 367 .

• 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 368 .

Fang Department of CSIE YUST. Copy Right Reserved 369 .P.• Product =1 • Term=N • FOR N times DO – product=product x term – term=trem-1 • ENDFOR W.

• FACTORIAL PROC – MOV AX.1 • TOP: – MUL CX – LOOP TOP – RET • FACTORIAL ENDP • Ps: CX=N. Copy Right Reserved 370 .AX=N! W.Fang Department of CSIE YUST.P.

Copy Right Reserved 371 . we obtain two results – Quotient – remainder W.P.Fang Department of CSIE YUST.DIV and IDIV • When division is performed.

• DIV divisor • IDIV divisor W.P.Fang Department of CSIE YUST. Copy Right Reserved 372 .

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.

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.

Copy Right Reserved 375 .• The effect of DIV/IDIV on the flags is that all status flags are undefined W.Fang Department of CSIE YUST.P.

Divide overflow • The quotient will be too big to fit in the specified destination • Devisor is much smaller than dividend W.Fang Department of CSIE YUST. Copy Right Reserved 376 .P.

Copy Right Reserved 377 .P.Fang Department of CSIE YUST.ex • Suppose DX contains 0000h. AX contains 0005h . and BX contains 0002h W.

P.instruction Decimal quotient Decimal remainder AX DX DIV BX 2 1 0002 0001 IDIV BX 2 1 0002 0001 W. Copy Right Reserved 378 .Fang Department of CSIE YUST.

AX contains 0005h.P. and BX contains FFFEh W.Fang Department of CSIE YUST.ex • Suppose DX contains 0000h. Copy Right Reserved 379 .

instruction

Decimal quotient

Decimal remainder

AX

DX

DIV BX

0

5

0000

0005

IDIV BX

-2

1

FFFE

0001

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

380

• Suppose DX contains FFFFh, AX contains FFFBh, and BX contains 0002

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

381

instruction

Decimal quotient

Decimal remainder

AX

DX

DIV BX

-2

-1

FFFE

FFFF

IDIV BX

DIVIDE OVERFLOW

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

382

ex
• Suppose AX contains 00FBh and BL contains FFH

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

383

instruction

Decimal quotient

Decimal remainder

AX

DX

DIV BL

0

251

FB

00

IDIV BL

DIVIDE OVERFLOW

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

384

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(convert word to doubleword) will be the extension

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

385

ex
• Divide -1250 by 7

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

386

• • • •

MOV AX,-1250 CWD MOV BX,7 IDIV BX

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

387

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

388

ex • Divide the signed value of the byte variable XBYTE by -7 W.P.Fang Department of CSIE YUST. Copy Right Reserved 389 .

Fang Department of CSIE YUST.-7 IDIV BL W.XBYTE CBW MOV BL.• • • • MOV AL.P. Copy Right Reserved 390 .

Copy Right Reserved 391 .P.• There is no effect of CBW and CWD on the flags W.Fang Department of CSIE YUST.

Copy Right Reserved 392 .Fang Department of CSIE YUST.P.Decimal input and output procedures • Decimal output • Decimal input W.

Decimal output • IF AX<0 – THEN • Print a minus sign • Replase AX by its two’ complemet s • END_IF • Get the digits in AX’ decimal s representation • Convert these digits to characters and print them W. Copy Right Reserved 393 .P.Fang Department of CSIE YUST.

Get the digits in AX’ decimal s representation • Count=0 • ERPEAT – Divide quotient by 10 – Push remainder on the stack – Count=count+1 • UNTIL quotient=0 W.P. Copy Right Reserved 394 .Fang Department of CSIE YUST.

Copy Right Reserved 395 .P.Fang Department of CSIE YUST.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.

Copy Right Reserved 396 .CX MOV BX.AX W.OUTDEC PROC PUSH AX PGM9_1.asm PUSH BX PUSH CX PUSH DX OR AX.AX JGE @END_IF1 PUSH AX MOV DL.Fang Department of CSIE YUST.10D @REPEAT1: XOR DX.DX DIV BX PUSH DX INC CX OR AX.P.2 INT 21H POP AX NEG AX @END_IF1: XOR CX.’ -’ MOV AH.

30H INT 21H LOOP @PRINT_LOOP POP DX POP CX POP BX POP AX RET OUTDEC ENDP W.Fang Department of CSIE YUST. Copy Right Reserved 397 .2 @PRINT_LOOP: POP DX OR DL.P.MOV AH.

Fang Department of CSIE YUST.INCLUDE Pseudo-op • INCLUDE filespec • Ex: – INCLUDE A:PGM9_1. Copy Right Reserved 398 .asm W.P.

P.MODEL SMALL .STACK 100H .4CH – INT 21H • MAIN ENDP • INCLUDE A:PGM9_1.Fang Department of CSIE YUST.asm – END MAIN W.CODE MAIN PROC – CALL OUTDEC – MOV AH. Copy Right Reserved 399 .• • • • • TITLE PGM9_2: DECIMAL OUTPUT .

Copy Right Reserved 400 .Fang Department of CSIE YUST.exe -rax AX 0000 :9C71 -g -25487 Program terminated normally -rax AX 9C71 :28E -g 654 W.• • • • • • • • • • • • Debug pgm9_2.P.

Copy Right Reserved 401 .P.Decimal input • Convert a string of ASCII digits to the binary representation of a decimal integer W.Fang Department of CSIE YUST.

Copy Right Reserved 402 .Fang Department of CSIE YUST.P.• • • • • 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.exercise • A=5xA-7 • B=(A-B)x(B+10) • A=6-9xA W. Copy Right Reserved 403 .Fang Department of CSIE YUST.

7 MOV A. Copy Right Reserved 404 .5 IMUL A SUB AX.AX W.P.A=5xA-7 • • • • MOV AX.Fang Department of CSIE YUST.

B ADD BX. Copy Right Reserved 405 .P.B MOV BX.10 IMUL BX MOV B.Fang Department of CSIE YUST.A SUB AX.B=(A-B)x(B+10) • • • • • • MOV AX.AX W.

AX W.Fang Department of CSIE YUST.A=6-9xA • • • • • MOV AX.6 MOV A. Copy Right Reserved 406 .P.9 IMUL A NEG AX ADD AX.

Fang Department of CSIE YUST. Copy Right Reserved 407 .AX SUB AX.B JNE ELSE STC JMP EXIT CLC • • • • IF_: – – – – – THEN: ELSE: EXIT: W.A IMUL AX MOV A.AX MOV AX.AX MOV AX.B IMUL AX MOV B.P.C MUL AX.A CMP AX.– – – – – – – – – MOV AX.

exercise • IF A^2+B^2=C^2 • THEN – SET CF • ELSE – Clear CF • END_IF W.Fang Department of CSIE YUST. Copy Right Reserved 408 .P.

B IMUL AX MOV B.AX SUB AX.B JNE ELSE STC JMP EXIT CLC THEN: ELSE: EXIT: W.C MUL AX.AX MOV AX.• • • • • • • • • • • • • MOV AX.AX MOV AX. Copy Right Reserved 409 .A IMUL AX MOV A.Fang Department of CSIE YUST.A IF_: – – – – – CMP AX.P.

P. 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 Convert character to a binary value Total =10xtotal+value ELSE • • END_IF Read a character • • • • • UNTIL character is a carriage return IF negative =true THEN Total=-total END_IF W.Fang Department of CSIE YUST.

asm W.Fang Department of CSIE YUST.PGM9_3. Copy Right Reserved 411 .P.

P. Copy Right Reserved 412 .PGM9_4.asm W.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P.7.HW • Ch9.11 W. Copy Right Reserved 413 .

Fang Department of CSIE YUST. Copy Right Reserved 414 .P.Arrays and Addressing Modes W.

Copy Right Reserved 415 .One-dimensional arrays • An ordered list of elements. W. all of the same type.P.Fang Department of CSIE YUST.

40.30. Copy Right Reserved 416 .P.• MSG DB ‘ abcde’ • W DW 10.Fang Department of CSIE YUST.60 W.50.20.

P.Fang Department of CSIE YUST.• Base address of the array W. Copy Right Reserved 417 .

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.

ex • • • • GAMMA DW 100 DUP(0) DELTA DB 212 DUP(?) LINE DB 5. 3 DUP(2.0.2.1.Fang Department of CSIE YUST.2.0.0.0. Copy Right Reserved 419 .4.4.0. 1) LINE DB 5.1.P.2.1 W.3.0.0.0. DUP (0).0.

P.Location of array elements • A=(N-1)xS • S: number of bytes in an element • A:array W. Copy Right Reserved 420 .Fang Department of CSIE YUST.

ex • exchange the 10th and 25th elements in a word array W.P. Copy Right Reserved 421 .Fang Department of CSIE YUST. W.

P.AX • Ps: • W[10] is located at address W+9x2=18 • W[25] is located at address W+24x2=W+48 W.Fang Department of CSIE YUST.• MOV AX.AX • MOV W+18.W+18 • XCHG W+48. Copy Right Reserved 422 .

Fang Department of CSIE YUST. Copy Right Reserved 423 .Address modes • Register mode • Immediate mode • Direct mode W.P.

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.Fang Department of CSIE YUST.P. Copy Right Reserved 424 . not same as MOV AX.DI or BP Segment is contained in DS MOV AX.SI.[SI] .Register indirect mode • • • • • • [register] The register is BX.

[AX] d.MOV BX.[SI] c.ADD [SI].Fang Department of CSIE YUST.P.[DI] e. and the result or number moves a.[BX] b. give the source offset address.ex BX contains 1000h SI contains 1000h DI contains 1000h Offset 1000h contains 1BACh Offset 2000h contains 20FEh Offset 3000h contains 031Dh Tell which of the following instruction are legal. Copy Right Reserved 425 .MOV CX.INC [DI] W.MOV BX.

Source offset a b c d e 1000h 2000h Illegal source register Illegal memory-memory addition 3000h W.P.SI or DI) 031Eh 426 . Copy Right Reserved result 1BACh 20FEh (must be BX.Fang Department of CSIE YUST.

70. Copy Right Reserved 427 .20.100 W.40.30.80.90.ex • Write some code to sum in AX the elements of the 10-element array W define by • W DW 10.60.Fang Department of CSIE YUST.50.P.

AX – LEA SI.[SI] – ADD SI.10 • ADDNOS: – ADD AX.– XOR AX.Fang Department of CSIE YUST.W – MOV CX.P.2 – LOOP ADDNOS W. Copy Right Reserved 428 .

• The procedure is entered with SI pointing the array.P. Copy Right Reserved 429 .Fang Department of CSIE YUST. and BX has the number or words N W.Ex • Write a procedure REVERSE that wull reverse an array of N words.

the 2nd and (N-1)st words. Copy Right Reserved 430 . 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.Fang Department of CSIE YUST.• The idea is to exchange the 1st and N th words.P. and so on.

1 ADD DI.[SI] XCHG AX.AX ADD SI.1 XCHG_LOOP: MOV AX.BX SHR CX. Copy Right Reserved 431 .2 LOOP XCHG_LOOP POP DI POP SI POP CX W.SI MOV CX.[DI] MOV [SI].Fang Department of CSIE POP BX YUST.P.2 SUB DI.BX DEC BX SHL BX.REVERSE PROC PUSH AX POP AX PUSH BX RET PUSH CX REVERSE ENDP PUSH SI PUSH DI MOV DI.

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. Copy Right Reserved 432 .P.Fang Department of CSIE YUST.

• 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.Fang Department of CSIE YUST.P. Copy Right Reserved 433 .

Copy Right Reserved 434 .Fang Department of CSIE YUST.P.• • • • • [register+displacement] [displacement+register] [register]+displacement Displacement+[register] Displacement[register] W.

• The register must be BX.SI or DI is used.BP.P. • 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.Fang Department of CSIE YUST.SS has the segment number. Copy Right Reserved 435 . DS contains the segment number of the operand’ address s • If BP is used.SI or DI • If BX.

P.ex • • • • • • • • • MOV AX.Fang Department of CSIE YUST. Copy Right Reserved 436 .W+[BX] MOV AX.2[SI] W.[BX]+W MOV AX.[2+SI] MOV AX.[SI+2] MOV AX.[BX+W] MOV AX.[W+BX] MOV AX.W[BX] MOV AX.[SI}+2 MOV AX.

30.50.P.Fang Department of CSIE YUST.70.80.ex • Write some code to sum in AX the element of 10-element array W defined by • W DW 10.90.60.100 W. Copy Right Reserved 437 .20.40.

W[BX] – ADD BX.– XOR AX.2 – LOOP ADDNOS W.Fang Department of CSIE YUST.AX – XOR BX.BX – MOV CX.P. Copy Right Reserved 438 .10 • ADDNOS: – ADD AX.

e.[ALPHA+BX] MOV BX. c.0ABCDh In the segment address by DS.P. f. b.ALPHA[SI] MOV AX. if legal. Copy Right Reserved 439 .ex • • • • • • • a.0456h.[BX]2 ADD BX. g.[ALPHA+3+DI] MOV AX. give the source offset address and the number moved MOV AX.[ALPHA+AX] W.0789h.-2[SI] MOV BX.Fang Department of CSIE YUST.[BX+2] MOV CX. Suppose ALPHA is declared as ALPHA DW 0123h. d. 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.

Fang Department YUST.P. Copy Right Reserved Number moved 0456h 2BACh 0789h 1084h 0789h 440 .Source offset A B C D E F g ALPHA +2 2+2=4 ALPHA+4 -2+4=2 ALPHA+3+1=ALPHA+4 Illegal form of source operand Illegal source register of CSIE W.

Copy Right Reserved 441 .P.ex • Replace each lowercase letter in the following string by its upper case equivalent.Fang Department of CSIE YUST. Use index addressing mode • MSG DB ‘ this is a message’ W.

– MOV CX.P.SI • TOP: – CMP MSG[SI].’‘ – JE NEXT – AND MSG[SI]. Copy Right Reserved 442 .17 – XOR SI.0DFh • NEXT: – INC SI – LOOP TOP W.Fang Department of CSIE YUST.

P. the assembler attempts to infer the type from the other operand W. 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.Fang Department of CSIE YUST.

Fang Department of CSIE YUST.P.1 .can’ assemble t • Shall be • MOV BYTE PTR [BX].illegal .1 W.• MOV [BX]. Copy Right Reserved 444 .1 • MOV WORD PTR [BX].

Fang Department of CSIE YUST.’ T’ W. Copy Right Reserved 445 .MSG • MOV BYTE PRT [SI].ex • LEA SI.P.

’ T’ – Ps:here it is not necessary to use th PTR operator. because MSG is a byte variable W.SI – MOV MSG[SI].Fang Department of CSIE YUST.P. Copy Right Reserved 446 .ex • Using index mode – XOR SI.

AH=cents W.DOLLARS .Fang Department of CSIE YUST.AL=dollars.illegal MOV AX.WORD PRT DOLLARS.P. Copy Right Reserved 447 .Use PTR to override a type Type PTR address_expression Ex: DOLLARS DB 1Ah CENTS DB 52h MOV AX.

Fang Department of CSIE YUST. and the components DOLLARS and CENTS as byte variables – MONEY LABEL WORD – DOLLAR DB 1Ah – CENTS DB 52h W. Copy Right Reserved 448 .P.The LABEL Pseudo-Op • Declaration types MONEY as a word variable.

MONEY.P.• MOV AX. Copy Right Reserved 449 .AH=cents W. AL=dollars.Fang Department of CSIE YUST.

Segment override • Specify an relative to one of the other segment registers • Segment_register:[pointer_register] W. Copy Right Reserved 450 .P.Fang Department of CSIE YUST.

• MOV AX.ES:[SI] W. Copy Right Reserved 451 .Fang Department of CSIE YUST.P.

Accessing the stack • Move the top three words on the stack into AX. Copy Right Reserved 452 . and CX without changing the stack W.BX.Fang Department of CSIE YUST.P.

Copy Right Reserved 453 .P.• • • • MOV BP.[BP+4] W.Fang Department of CSIE YUST.[BP+2] MOV.[BP] MOV BX.SP MOV AX.

.Fang Department of CSIE YUST.P.A[i] – (*) swap A[k] and A[i] – i=i-1 • END_FOR W. Copy Right Reserved 454 .Selection algorithm • i=N • FOR N-1 times DO – Find the position k of the largest elemnt • Among A[1].

P.Fang Department of CSIE YUST.asm W. Copy Right Reserved 455 .• PGM10_2.

Fang Department of CSIE YUST.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. Copy Right Reserved 456 .P.

70.40 • DW 50.20.60. Copy Right Reserved 457 .• B DW 10.110.P.30.Fang Department of CSIE YUST.80 • DW 90.100.120 W.

Copy Right Reserved 458 .P.• MxN – A[i][j] è A+((i-1)xN+(j-1))xS – A[i][j] è A+((i-1)+(j-1)xM)xS W.Fang Department of CSIE YUST.

Copy Right Reserved 459 . a variable’ offset address s Optionally.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.P. a constant (positive or negative) W.Fang Department of CSIE YUST.

• 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.Fang Department of CSIE YUST. Copy Right Reserved 460 .P.

ex • Suppose A is a 5x7 word array stored in row-major order.(2) clear column4 use based indexed mode W. Copy Right Reserved 461 .P.Fang Department of CSIE YUST.write some code to (1) clear row 3.

• (1) – MOV X. Copy Right Reserved 462 .0 – ADD SI.28 – XOR SI.Fang Department of CSIE YUST.SI – MOV CX.7 • CLEAR: – MOV A[BX][SI].2 – LOOP CLEAR W.P.

BX – MOV CX.6 – XOR BX.1 – LOOP CLEAR W.0 – ADD BX.• (2) – MOV SI.P.5 • CLEAR: – MOV A[BX][SI].Fang Department of CSIE YUST. Copy Right Reserved 463 .

P. and BX has the offset address of the conversion table W. Copy Right Reserved 464 .Fang Department of CSIE YUST.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.

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.

0Ch • LEA BX.TABLE • XLAT • àAL has ‘ C’ W.039h • DB 041h.03 7h.043h.046h • MOV AL.045h.042h. Copy Right Reserved 466 .044h.P.033h.038h.ex • TABLE DB 030h.036h.034h.032h.Fang Department of CSIE YUST.035h.031h.

HW • Ch10.6 W.Fang Department of CSIE YUST. Copy Right Reserved 467 .P.

Fang Department of CSIE YUST.P. 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.

Fang Department of CSIE YUST. – These operations are implemented by the two index register SI and DI W.Direction flag • DF (direction flag) – determine the direction in which string operations will proceed.P. Copy Right Reserved 469 .

Fang Department of CSIE YUST.• STRING1 DB ‘ ABCDE’ • If DF=0.SI and DI proceed in the direction of increasing memory addresses from left to right across the string. SI and DI proceed in the direction of decreasing memory address: from right to left W. Copy Right Reserved 470 .P. • If DF=1.

CLD and STD • CLD • STD clear direction flag set direction flag W.P.Fang Department of CSIE YUST. Copy Right Reserved 471 .

to the byte address by ES:DI W.Moving a string • MOVSB • Copies the contents of the byte addressed by DS:SI.Fang Department of CSIE YUST.P. Copy Right Reserved 472 .

@DATA MOV DS.AX LEA SI.AX MOV EX.STRING1 LEA DI.P.ex • • • • • • • • • .STRING2 CLD MOVSB • MOVSB W. Copy Right Reserved 473 .Fang Department of CSIE YUST.DATA STRING 1 DB ‘ HELLO’ STRING2 DB 5 DUP(?) MOV AX.

STRING2 MOV CX.STRING1 LEA DI.P.Move entire string • REP MOVSB • Ex: – – – – – CLD LEA SI.Fang Department of CSIE YUST. Copy Right Reserved 474 .5 REP MOVSB W.

P. Copy Right Reserved 475 .Fang Department of CSIE YUST.Ex: • Write instructions to copy STRING1 into STRING2 in reverse order W.

– – – –

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 476

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

477

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

478

• • • • • •

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

479

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

480

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 481

Reading and storing a character string
• Use INT 21h, function 1 and STOSB

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

482

• 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 483

READ_STR PROC NEAR
– – – – – – PUSH AX PUSH DI CLD XOR BX,BX MOV AH,1 INT 21H CMP AL,0DH JE END_WHILE1 CMP AL,8H JNE ELSE1 DEC DI DEC BX JMP READ STOSB INC BX INT 21H JMP WHILE1 POP DI POP AX RET

WHILE1:
– – – – – – –

• • •

ELSE1:
– – – – – – –

READ: END_WHILE1:

READ_STR ENDP

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

484

Load string • LODSB – Moves the byte addressed by DS:SI into AL.SI is then incremented by 2 if DF=0 or decremented by 2 if DF=1 W.Fang Department of CSIE YUST.SI is then incremented if DF=0 or decremented if DF=1 • LODSW – Moves the byte addressed by DS:SI into AX. Copy Right Reserved 485 .P.

Displaying a character string • FOR count times DO – Load a string character into AL – Move it to DL – Output character • END_FOR W.Fang Department of CSIE YUST. Copy Right Reserved 486 .P.

Fang Department of CSIE YUST.BX=no.BX JCXZ P_EXIT CLD MOV AH. of chars.P.• Let Si =offset of string. To display – – – – – – – – – MOV CX.AL INT 21H LOOP TOP P_EXIT: W.2 LODSB MOV DL. Copy Right Reserved 487 • TOP: .

Scan string • • • • SCANSB Examine a string for a target byte The target byte is contained in AL.P. Copy Right Reserved 488 .Fang Department of CSIE YUST. 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.

• SCANSW – If not found ZF=0 W. Copy Right Reserved 489 .P.Fang Department of CSIE YUST.

• REPNZ (repeat while not zero) • REPNE (the same as above) • Ex: – REPNE SCANSB W.P.Fang Department of CSIE YUST. Copy Right Reserved 490 .

Fang Department of CSIE YUST. Copy Right Reserved 491 .Ex:counting vowels and consonants • PGM11_4 W.P.

Copy Right Reserved 492 .Fang Department of CSIE YUST.Compare string • CMPSB – Subtracts the byte with address ES:DI from the byte with address DS:SI.P. the result is not stored • CMPSW W. and sets the flags.

AX MOV ES.STRING2 CMPSB CMPSB W.@DATA MOV DS.DATA STRING1 DB ‘ ACD’ STRING2 DB ‘ ABC’ MOV AX.STRING1 LEA DI. Copy Right Reserved 493 .P.AX CLD LEA SI.• • • • • • • • • • • .Fang Department of CSIE YUST.