You are on page 1of 493

Assembly Language

W.P.fang

W.P.Fang Department of CSIE 1


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

W.P.Fang Department of CSIE 2


YUST, Copy Right Reserved
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 3


YUST, Copy Right Reserved
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 4
YUST, Copy Right Reserved
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 5


YUST, Copy Right Reserved
Outline
• Macros
• Memory Management
• BIOS and DOS Interrupt
• Color Graphics
• Recursion
• Advanced Arithmetic
• Disk and File Operations
• Intel’s Advanced Microprocessors6
W.P.Fang Department of CSIE 6
YUST, Copy Right Reserved
schdular
日期 進度 備註
1
2
3
4
5
6
7
8
W.P.Fang Department of CSIE 7
9 YUST, Copy Right Reserved
schdular
日期 進度 備註
10
11
12
13
14
15
16
17
W.P.Fang Department of CSIE 8
18 YUST, Copy Right Reserved
Microcomputer Systems

W.P.Fang Department of CSIE 9


YUST, Copy Right Reserved
Microcomputer Systems
• Overview
– Introduction to the architeture of
microcomputers in general and to the IBM PC
in particular.

W.P.Fang Department of CSIE 10


YUST, Copy Right Reserved
Microcomputer System
(component)
• The components o a microcomputer
system
– System unit
– Keyboard
– display screen
– Disk drives

W.P.Fang Department of CSIE 11


YUST, Copy Right Reserved
Microcomputer System
(component)
• Computer circuits
– Central processing unit (CPU)
– I/O circuits
• Microcomputer
– CPU is a single-chip processor
(microprocessor)
• System board
– Microprocessor and memorycircuirs

W.P.Fang Department of CSIE 12


YUST, Copy Right Reserved
Microcomputer System
(component)
• Bytes – eight bits
• Address – identified by a number
• Content– the data stored in a memory byte.

Address content
: :
3 01100001
2 01011110
1 00000000
0 01101110

W.P.Fang Department of CSIE 13


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

W.P.Fang Department of CSIE 14


YUST, Copy Right Reserved
Microcomputer System
(component)
• Word – typical microcomputer, two bytes
form a word
• Bit position
Byte bit position
7 6 5 4 3 2 1 0

Word bit position

15 14 13 12 11 10 9 88 7 6 5 4 3 2 1 0

High byte Low byte

W.P.Fang Department of CSIE 15


YUST, Copy Right Reserved
Microcomputer system
(component)
• RAM and ROM
• Bus

W.P.Fang Department of CSIE 16


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

W.P.Fang Department of CSIE 17


YUST, Copy Right Reserved
Microcomputer system
(component)
• EU (Execution Unit)
– Arithmetic
• +-*/
– Logic unit
• AND, OR, NOT
– Registers
• AX,BX,CXDX,SI,DI,BP,SP

W.P.Fang Department of CSIE 18


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

W.P.Fang Department of CSIE 19


YUST, Copy Right Reserved
Microcomputer system
(component)
• I/O ports

W.P.Fang Department of CSIE 20


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

W.P.Fang Department of CSIE 21


YUST, Copy Right Reserved
Microcomputer system
(instruction execution)
• Fetch-execute cycle
– Fetch
• Fetch an instruction from memory
• Decde he inctruction to determine the operation
• Fetch data from memory if necessary.
– Execute
• Perform the operation on the data
• Store the result in memory if needed.

W.P.Fang Department of CSIE 22


YUST, Copy Right Reserved
Microcomputer system
(I/O Devices)
• Magnetics disks
• Keyboard
• Display monitor
• printers

W.P.Fang Department of CSIE 23


YUST, Copy Right Reserved
Microcomputer system
(programming languages)
• Machine language
Machine instruction operation

10100001 00000000 00000000 Fetch the content of memory word


to and put it in register AX

00000101 00000100 00000000 Add 4 to AX

10100011 00000000 00000000 Store the content of AX in memory


word 0

W.P.Fang Department of CSIE 24


YUST, Copy Right Reserved
Microcomputer system
(programming languages)
• Assembly language
Assembly language operation
instruction
MOV AX,A Fetch the content of location A and
put it in register AX

ADD AX,4 Add 4 to AX

MOV A,AX move the content of AX into


location A

W.P.Fang Department of CSIE 25


YUST, Copy Right Reserved
Microcomputer system
(programming languages)
• Assembler
• compiler
• High-level language

W.P.Fang Department of CSIE 26


YUST, Copy Right Reserved
Microcomputer system
(programming languages)
• Advantage of high-level languages
– Closer to natural language
– Reduce Coding time
– Executed on any machine
• Advantage of assembly language
– Efficiency
– Some operations can be done easily in
assembly language but may be impossible at
a higher level

W.P.Fang Department of CSIE 27


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

W.P.Fang Department of CSIE 28


YUST, Copy Right Reserved
Microcomputer system
(Debug)
• assemble A [address]
• compare C range address
• dump D [range]
• enter E address [list]
• fill F range list
• go G [=address] [addresses]
• hex H value1 value2
• input I port
• load L [address] [drive] [firstsector] [number]
• move M range address
• name N [pathname] [arglist]
• output O port byte

W.P.Fang Department of CSIE 29


YUST, Copy Right Reserved
Microcomputer system
(Debug)
• proceed P [=address] [number]
• quit Q
• register R [register]
• search S range list
• trace T [=address] [value]
• unassemble U [range]
• write W [address] [drive] [firstsector] [number]
• allocate expanded memory XA [#pages]
• deallocate expanded memory XD [handle]
• map expanded memory pages XM [Lpage] [Ppage] [handle]
• display expanded memory status XS

W.P.Fang Department of CSIE 30


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

W.P.Fang Department of CSIE 31


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

W.P.Fang Department of CSIE 32


YUST, Copy Right Reserved
Glossary
• Add-in board or card
• Address
• Address bus
• Arithmetic and logic unit, ALU
• Assembler
• Assembly language
• Binary digit
• Bit
W.P.Fang Department of CSIE 33
YUST, Copy Right Reserved
Glossary
• Bus
• Bus interface unit, BIU
• Byte
• Central processing unit, CPU
• Clock period
• Clock pulse
• Clock rate
• Clock speed
W.P.Fang Department of CSIE 34
YUST, Copy Right Reserved
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 35
YUST, Copy Right Reserved
Glossary
• Firmware
• Fixed disk
• Floppy diskhardcopy
• Hard disk
• I/O devices
• I/O ports
• Intruction pointer,IP
• Instruction set
• Kilobyte, KB

W.P.Fang Department of CSIE 36


YUST, Copy Right Reserved
Glossary
• Machine language
• Mega
• Megabyte, MB
• Megahertz, MHz
• Memory byte (circuit)
• Memory location
• Memory word
• microprocessor
W.P.Fang Department of CSIE 37
YUST, Copy Right Reserved
Glossary
• Motherboard
• Opcode
• Operand
• Peripheral (device)
• Random access memory
• RAM
• Read-only memory (ROM)
• register
W.P.Fang Department of CSIE 38
YUST, Copy Right Reserved
Glossary
• System board
• Video adapter
• word

W.P.Fang Department of CSIE 39


YUST, Copy Right Reserved
Representation of numbers
and characters

W.P.Fang Department of CSIE 40


YUST, Copy Right Reserved
Representation of numbers and
characters
• Please refer to bcc

W.P.Fang Department of CSIE 41


YUST, Copy Right Reserved
• Binary number system

W.P.Fang Department of CSIE 42


YUST, Copy Right Reserved
• Ex:
• 8A2Dh=?
• 11101=?
• 2BD4h=?
• Convert 95 to binary
• Convert 2B3Ch to binary
• Convert 1110101010 to hex

W.P.Fang Department of CSIE 43


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

W.P.Fang Department of CSIE 44


YUST, Copy Right Reserved
ASCII code
• American Standard Code for Information
Interchange

W.P.Fang Department of CSIE 45


YUST, Copy Right Reserved
Homework
• Chap 2
– 2.
– 7.d
– 8.c
– The rule of ascii

W.P.Fang Department of CSIE 46


YUST, Copy Right Reserved
Representation of numbers and
characters
• Number system
– Binary
– Hexadecimal
• Conversion
• Addition and subtraction
• Character representation

W.P.Fang Department of CSIE 47


YUST, Copy Right Reserved
Representation of numbers and
characters
(Glossary)
• ASCII (American Standard Code for
Information Interchange) code
• Binary number system
• Hexadecimal number system
• Least significant bit lsb
• most significant bit msb
• One’s complement of a binarynmber
• Scan code

W.P.Fang Department of CSIE 48


YUST, Copy Right Reserved
Representation of numbers and
characters
(Glossary)
• Signed integer
• Two’s complement of a binary number
• Unsigned integer

W.P.Fang Department of CSIE 49


YUST, Copy Right Reserved
Organization of the IBM
Personal Computers

W.P.Fang Department of CSIE 50


YUST, Copy Right Reserved
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 51


YUST, Copy Right Reserved
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 52


YUST, Copy Right Reserved
Register
• Data register
• Address register
• Status register
• Segment
• Pointer
• Index register

W.P.Fang Department of CSIE 53


YUST, Copy Right Reserved
• Ax
• Bx
• Cx
• Dx
• Cs
• Ds
• Ss
• Es
• Si
• Di
• Sp
• Bp
• Ip
• flag

W.P.Fang Department of CSIE 54


YUST, Copy Right Reserved
Data register
• AX(accumulator)
• BX(base register)
• CX(count register)
• DX(data register)

W.P.Fang Department of CSIE 55


YUST, Copy Right Reserved
Segment register
• CS -- code segment
• DS – data segment
• SS – stack segment
• ES – access second data segment (extra
segment)

W.P.Fang Department of CSIE 56


YUST, Copy Right Reserved
• Memory segment
• Segment:offset address
– Because 20bit can not represent in 16 bit

W.P.Fang Department of CSIE 57


YUST, Copy Right Reserved
Logical address
• Ex:
• A4FB:4872h
• A4FB0+4872h=A9822h(20-bit physical
address)

W.P.Fang Department of CSIE 58


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

W.P.Fang Department of CSIE 59


YUST, Copy Right Reserved
• 1256Ah=12560h+X
• 1256Ah=12400h+Y
• X=Ah,Y=16Ah
• 1256Ah=1256:000A=1240:016A

W.P.Fang Department of CSIE 60


YUST, Copy Right Reserved
• A memory location has physical address
80FD2h. In what segment does it have
offset BFD2h

W.P.Fang Department of CSIE 61


YUST, Copy Right Reserved
• Physical address=segmentx10h+offset
• Segmentx10h=physical address-offset
• 80FD2h-BFD2h=75000h
• àsegment 7500h

W.P.Fang Department of CSIE 62


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

W.P.Fang Department of CSIE 63


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

W.P.Fang Department of CSIE 64


YUST, Copy Right Reserved
Instruction Pointer
• IP
– Use register CS ad IP to access instruction
– CS à segment number of next instruction
– IP à offset
– Can not be directly manipulated by an
instruction

W.P.Fang Department of CSIE 65


YUST, Copy Right Reserved
Flags
• Status flags
– Ex:ZF(zero flag)
• Controls
– Ex: IF(interrupt flag) =0 à input from the
keyboard are ignored by the processor.

W.P.Fang Department of CSIE 66


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

W.P.Fang Department of CSIE 67


YUST, Copy Right Reserved
• Dos routine that services user commands
is called COMMAND.COM

W.P.Fang Department of CSIE 68


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

W.P.Fang Department of CSIE 69


YUST, Copy Right Reserved
Memory
Bios F0000h
Reserved E0000h
Reserved D0000h
Reserved C0000h
Video B0000h
Video A0000h
Application program
area
Dos
Bios and dos data 00400h
Intreeupt vectors W.P.Fang Department of CSIE
00000h
YUST, Copy Right Reserved
70
Port address description
20h-21h Interrupt controller
60h-63h Keyboard controller
200h-20fh Game controller
2f8h-2ffh Serial port (COM2)
320h-32fh Hard disk
378h-37fh Parallel printer port 1
3c0h-3cfh EGA
3d0h-3dfh CGA
3f8h-3ffh Serial port (COM1)
W.P.Fang Department of CSIE 71
YUST, Copy Right Reserved
Start-up operation
• Power up
• CS register is set to FFFFh
• IP is set to 0000h
• à FFFF0h (in ROM)
• First check for system and memory errors
• Initialize the interrupt vectors and BIOS
data
• Boot program

W.P.Fang Department of CSIE 72


YUST, Copy Right Reserved
Introduction to IBM PC
Assembly Language

W.P.Fang Department of CSIE 73


YUST, Copy Right Reserved
Introduction to IBM PC Assembly
Language
• Syntax
– Name operation operand(s) comment
• Ex:
– START: MOV CX,5 ;initialize counter
– MAIN PROC

W.P.Fang Department of CSIE 74


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

W.P.Fang Department of CSIE 75


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

W.P.Fang Department of CSIE 76


YUST, Copy Right Reserved
Comment fields
• A semicolon marks the beginning of this
field
• The assembler ignores anything typed
after the semicolon.
• Ex:
– MOV CX,0 ;move 0 to CX
• Permissible to make an entire line a
comment
W.P.Fang Department of CSIE 77
YUST, Copy Right Reserved
Program data
• The processor operates only on binary
data
• The assembler must translate all data
representation into binary number

W.P.Fang Department of CSIE 78


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

DB Define byte

DW Define word

DD Define double word

DQ Define quaword

DT Define tenbytes

W.P.Fang Department of CSIE 80


YUST, Copy Right Reserved
• Byte variable
– Name DB initial_value
• Ex:
– ALPHA DB 4

W.P.Fang Department of CSIE 81


YUST, Copy Right Reserved
• Word variable
– Name DW initial_value
• Ex:
– WRD DW -2

W.P.Fang Department of CSIE 82


YUST, Copy Right Reserved
• Array
– B_ARRAY DB 10H,20H,30H
• String
– LETTERS DB ‘ABC’
– LETTERS DB 41H,42H,43H
– MSG DB ‘HELLO’,0AH,0DH,’$’

W.P.Fang Department of CSIE 83


YUST, Copy Right Reserved
• Named Constanted
– EQU (Equates)
– Name EQU constant
• Ex:
– LF EQU 0AH
– PROMPT EQU ‘TYPE YOUR NAME’

W.P.Fang Department of CSIE 84


YUST, Copy Right Reserved
variables
• Byte variables
• Name DB initial_value
• Ex:
• ALPHA DB
• BYT DB ?

W.P.Fang Department of CSIE 85


YUST, Copy Right Reserved
Word variables
• Name DW initial_value
• Ex:
– WRD DW -2

W.P.Fang Department of CSIE 86


YUST, Copy Right Reserved
array
• B_ARRAY DB
10H,20H,30H

Symbol Address contents


B_ARRAY 200h 10h
B_ARRAY+1 201h 20h
B_ARRAY 202h 30h

W.P.Fang Department of CSIE 87


YUST, Copy Right Reserved
Character string
• LETTERS DB ‘ABC’
• LETTERS DB 41H,42H,43H
• MSG DB ‘HELLO’,0AH,0DH,’$’
• MSG DB
48H,45H,4CH,4CH,4FH,0AH,0DH,24H

W.P.Fang Department of CSIE 88


YUST, Copy Right Reserved
Names constants
• Name EQU constant
• Ex:
– LF EQU 0AH
– MOV DL,0AH
– MOV DL,LF
– Prompt EQU ‘TYPE YOUR NAME’
– MSG DB PROMPT
• Note:no memory is allocated for EQU
names
W.P.Fang Department of CSIE 89
YUST, Copy Right Reserved
A few basic instructions
• MOV
• XCHG
• ADD
• SUB
• INC
• DEC
• NEG

W.P.Fang Department of CSIE 90


YUST, Copy Right Reserved
• MOV
destination,source
General Segment Memory constant
Destination register register location
operand
Source
operand
General yes Yes Yes No
register
Segment Yes No Yes No
register

Memory Yes Yes No No


location W.P.Fang Department of CSIE 91
YUST, Copy Right Reserved
Constant Yes No Yes no
• XCHG destination,
source

Destination General Memory


operand register location
Source
operand
Generation Yes Yes
register
Memory Yes no
location W.P.Fang Department of CSIE
YUST, Copy Right Reserved
92
• XCHG AH,BL
• XCHG AX,WORD1

1A 00 05 00

AH AL AH AL

00 05 00 1A

BH BL BH BL

W.P.Fang Department of CSIE 93


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

W.P.Fang Department of CSIE 94


YUST, Copy Right Reserved
• ADD destination, source
• SUB destination, source

Destination General register Memory location


operand
Source operand
General register Yes Yes

Memory location Yes No

Constant Yes yes


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

W.P.Fang Department of CSIE 96


YUST, Copy Right Reserved
• NEG destination
• By two’s complement
• Ex:
– NEG BX

W.P.Fang Department of CSIE 97


YUST, Copy Right Reserved
Type agreement of operands
• The operands of the preceding two-
operand instruction must be of the same
type.

W.P.Fang Department of CSIE 98


YUST, Copy Right Reserved
Translation of High-Level language
to assembly labguage
• B=A
– MOV AX,A
– MOV B,AX
• A=5-A
– MOV AX,5
– SUB AX,A
– MOV A,AX

– NEG A
– ADD A,5

W.P.Fang Department of CSIE 99


YUST, Copy Right Reserved
• A=B-2*A
– MOV AX,B
– SUB AX,A
– SUB AX,A
– MOV A,AX

W.P.Fang Department of CSIE 100


YUST, Copy Right Reserved
• .MODEL memory_model

W.P.Fang Department of CSIE 101


YUST, Copy Right Reserved
Program structure
(Memory model)
Model Description
SMALL Code in one segment
Data in one segment
MEDIUM Code in more than one segment
Data in one segment
COMPACT Code one segment
Data in more than one segment
LARGE Code in more than one segment
Data in more than one segment
No array larger than 64k bytes
HUGE Code in more than one segment
Data in more one segment
Arrays may be larger than 64k
bytes
W.P.Fang Department of CSIE 102
YUST, Copy Right Reserved
Data segment
• Contains all the variable definitions
• .DATA
• WORD1 DW2
• WORD2 DW 5
• MSG DB ‘THIS IS A MESSAGE’
• MASK EQU 10010010B

W.P.Fang Department of CSIE 103


YUST, Copy Right Reserved
Stack segment
• .STACK size
• Ex:
– .STACK 100H
• Ps: if size is omitted 1KB is set aside for
the stack data

W.P.Fang Department of CSIE 104


YUST, Copy Right Reserved
Code segment
• .CODE name
• There is no need for a name in a SMALL
program

• Name PROC
• ;body of the procedure
• Name ENDP

W.P.Fang Department of CSIE 105


YUST, Copy Right Reserved
• Ex:
– .CODE
– MAIN PROC
– ;main procedure instructions
– MAIN ENDP
– ;other procedures go here

W.P.Fang Department of CSIE 106


YUST, Copy Right Reserved
Putting it together
• .MODEL SMALL
• .STACK 100H
• .DATA
• ;data definitions go here
• .CODE
• MAIN PROC
• ;instructions go here
• MAIN ENDP
• ;other procedures go here
• END MAIN

W.P.Fang Department of CSIE 107


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

W.P.Fang Department of CSIE 108


YUST, Copy Right Reserved
INT instruction
• INT interrupt_number

W.P.Fang Department of CSIE 109


YUST, Copy Right Reserved
INT 21h
Function number routine

1 Single-key input

2 Single-character output

9 Character string outpur

W.P.Fang Department of CSIE 110


YUST, Copy Right Reserved
INT 21h
Function 1:
• Single-key input
• Input:
– Ah=1
• Output:
• AL=ASCII code if character key is pressed
=0 if non-character key is pressed

W.P.Fang Department of CSIE 111


YUST, Copy Right Reserved
• MOV AH,1
• INT 21h

W.P.Fang Department of CSIE 112


YUST, Copy Right Reserved
INT 21h
Function 2:
• Display a character or execute a control
function:
– Ah=2
– DL=ASCII code of the display character or
control character
• Output:
• AL=ASCII code of the display character or
control character

W.P.Fang Department of CSIE 113


YUST, Copy Right Reserved
• MOV Ah,2
• MOV DL,’?’
• INT 21h

W.P.Fang Department of CSIE 114


YUST, Copy Right Reserved
ASCII code Symbol Function
(HEX)
7 BEL Beep (sounds a
tone)
8 BS Backspace

9 HT Tab

A LF Line feed (new


line)
D CR
W.P.Fang Department of CSIE
Carriage return115
YUST, Copy Right Reserved (start of current
INT 21h
Function 9:
• Display a string:
– DX=offset address of string
– The string must end with a ‘$’character.

W.P.Fang Department of CSIE 116


YUST, Copy Right Reserved
• Ex:
– MSG DB ‘Hello!$’

W.P.Fang Department of CSIE 117


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

W.P.Fang Department of CSIE 118


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

W.P.Fang Department of CSIE 120


YUST, Copy Right Reserved
Terminating a program
• INT 32h function 4Ch

W.P.Fang Department of CSIE 121


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

W.P.Fang Department of CSIE 122


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

W.P.Fang Department of CSIE 123


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

W.P.Fang Department of CSIE 124


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

W.P.Fang Department of CSIE 125


YUST, Copy Right Reserved
Exercise
• Using only MOV,ADD,SUB,INC,DEC and
NEG translate below into assembly (A,B,C
are word)
• A=B-A
• A=-(A+1)
• C=A+B
• B=3*B+7
• A=B-A-1
W.P.Fang Department of CSIE 126
YUST, Copy Right Reserved
homework
• Ch4
–4
–8
– 12
• If programming exercise please print out
code and result (hard copy)
• If more than one page , please bind in left
upper
• Use A4 paper
W.P.Fang Department of CSIE 127
YUST, Copy Right Reserved
• Write a program to
– Display a “?”
– Read two decimal digits whose sum is less
than 10
– Display them and their sum on the next line,
with an appropriate message
• ?27
• THE SUM OF 2 AND 7 is 9

W.P.Fang Department of CSIE 128


YUST, Copy Right Reserved
• 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 129


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


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

W.P.Fang Department of CSIE 131


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

W.P.Fang Department of CSIE 132


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

8 Trap flag TF

9 Interrupt flag IF

10 Direction flag DF

W.P.Fang Department of CSIE 134


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

W.P.Fang Department of CSIE 135


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

W.P.Fang Department of CSIE 136


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

W.P.Fang Department of CSIE 137


YUST, Copy Right Reserved
Zero flag
• ZF=1 for a zero result
• ZF=0 for a nonezero result

W.P.Fang Department of CSIE 138


YUST, Copy Right Reserved
Sign Flag
• SF=1 if the msb of a result is 1
• Means the result is negative if giving a
signed interpretation
• SF=0 if the msb=0

W.P.Fang Department of CSIE 139


YUST, Copy Right Reserved
Overflow Flag
• OF=1 if signed overflow occurred

W.P.Fang Department of CSIE 140


YUST, Copy Right Reserved
Overflow
• Is associated with the fact that the range
of numbers that be represented in a
computer is limited.

W.P.Fang Department of CSIE 141


YUST, Copy Right Reserved
Overflow examples
• Four possible outcome
– No overflow
– Signed overflow
– Unsigned overflow
– Both signed and unsigned overflow

W.P.Fang Department of CSIE 142


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

W.P.Fang Department of CSIE 144


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

W.P.Fang Department of CSIE 145


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

W.P.Fang Department of CSIE 146


YUST, Copy Right Reserved
• Signed overflow
– On addition
• Signed overflow occurs when the sum has a different sign
– On substraction
• With different signed like adding number of the same sign.
• Ex:
• A-(-B)=A+B
• -A-(+B)=-A+ -B
• Occur if the result has a different sign than expected

W.P.Fang Department of CSIE 147


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

W.P.Fang Department of CSIE 148


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

W.P.Fang Department of CSIE 149


YUST, Copy Right Reserved
How Instructions affect the flags
instruction Affects flags

MOV/XCHG None

ADD/SUB All

INC/DEC All except CF

NEG All (CF=1 unless result is 0


OF=1 if word operand is 8000h
Or byte operand is 80h
W.P.Fang Department of CSIE 150
YUST, Copy Right Reserved
example
• ADD AX,BX, where AX contains
FFFFh,BX contains FFFh

W.P.Fang Department of CSIE 151


YUST, Copy Right Reserved
example
• ADD AL,BL, where AL contains 80h,BL
contains 80h

W.P.Fang Department of CSIE 152


YUST, Copy Right Reserved
example
• SUB AX,BX, where AX contains 8000h
and BX contains 0001h

W.P.Fang Department of CSIE 153


YUST, Copy Right Reserved
example
• INC AL, where AL contain FFh

W.P.Fang Department of CSIE 154


YUST, Copy Right Reserved
example
• MOV AX,-5

W.P.Fang Department of CSIE 155


YUST, Copy Right Reserved
example
• NEG AX, where AX contains 8000h

W.P.Fang Department of CSIE 156


YUST, Copy Right Reserved
The DEBUG Program
• An environment in which a program may
be tested.

W.P.Fang Department of CSIE 157


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

W.P.Fang Department of CSIE 158


YUST, Copy Right Reserved
-r

AX=0000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC
0B8E:0000 B80040 MOV AX,4000
-r

AX=0000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC
0B8E:0000 B80040 MOV AX,4000
-t

AX=4000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0003 NV UP EI PL NZ NA PO NC
0B8E:0003 03C0 ADD AX,AX

W.P.Fang Department of CSIE 159


YUST, Copy Right Reserved
-t

AX=8000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0005 OV UP EI NG NZ NA PE NC
0B8E:0005 2DFFFF SUB AX,FFFF
-t

AX=8001 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0008 NV UP EI NG NZ AC PO CY
0B8E:0008 F7D8 NEG AX
-t

AX=7FFF BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000A NV UP EI PL NZ AC PE CY
0B8E:000A 40 INC AX

W.P.Fang Department of CSIE 160


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

AX=8000 BX=0000 CX=000F DX=0000 SP=0100 BP=0000 SI=0000 DI=0000


DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000B OV UP EI NG NZ AC PE CY
0B8E:000B B44C MOV AH,4C
-g

Program terminated normally


-q

W.P.Fang Department of CSIE 161


YUST, Copy Right Reserved
homework
• Ch5
– 1.e
– 2.b
– 3.d
– 4.e

W.P.Fang Department of CSIE 162


YUST, Copy Right Reserved
Flow Control Instructions

W.P.Fang Department of CSIE 163


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

W.P.Fang Department of CSIE 164


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

W.P.Fang Department of CSIE 165


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

W.P.Fang Department of CSIE 167


YUST, Copy Right Reserved
Conditional jump
• Jxxx destination_label

W.P.Fang Department of CSIE 168


YUST, Copy Right Reserved
Conditional jumps
• Range of a conditional jump
– Precede no more than 126 bytes
– Follow it by no more than 127 bytes

W.P.Fang Department of CSIE 169


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

W.P.Fang Department of CSIE 170


YUST, Copy Right Reserved
• JNZ
– Inspecting ZF
• If ZF=0,transfer to label
• If ZF=1, next line

W.P.Fang Department of CSIE 171


YUST, Copy Right Reserved
Conditional jumps
• Three categories
– The signed jumps
– Unsigned jumps
– Single-flag jumps
• Ps: jump instruction do not affect the flags

W.P.Fang Department of CSIE 172


YUST, Copy Right Reserved
Signed jumps
symbol description Condition for jumps

JG/JNLE Jump if greater than ZF=0 and SF=OF


Jump if not less than or
equal to
JGE/JNL Jump if greater than or SF=OF
equal to
Jump if not less than or
equal to
JL/JNGE Jump if less than SF<>OF
Jump if not greater than
or equal
JLE/JNG Jump if less than or ZF=1 or SF<>OF
equal
W.P.Fang Department of CSIE 173
Jump if not great than
YUST, Copy Right Reserved
Unsigned conditional jumps
Symbol description Condition for jumps

JA/JNBE Jump if above CF=0 and ZF=0


Jump if not below or
equal
JAE/JNB Jump if above or equal CF=0
Jump if not below

JB/JNAE Jump if below CF=1


Jump if not above or
equal
JBE/JNA Jump if equal CF=1 or ZF=1
Jump if not above

W.P.Fang Department of CSIE 174


YUST, Copy Right Reserved
Single-Flag jumps
symbol description Condition for jumps
JE/JZ Jump if equal ZF=1
Jump if equal to zero
JNE/JNZ jump if not equal ZF=0
Jump if not zero
JC Jump if carry CF=1
JNC Jump if no carry CF=0
JO Jump if overflow OF=1
JNO Jump if no overflow OF=0
JS Jump if sign negative SF=1
JNS Jump if nonnegative sign SF=0
JP/JPE Jump if parity even PF=1
JNP/JPO W.P.Fang
Jump Department
if parity odd of CSIE PF=0 175
YUST, Copy Right Reserved
The CMP instruction
• CMP destination , source
• Compares destination and source by
computing (destination)-(source)
• the result is not stored, but flags are
affected
• May not both memory locations
• Destination may not be a constant

W.P.Fang Department of CSIE 176


YUST, Copy Right Reserved
• CMP AX,BX
• JG BELOW
• Where AX=7,BX=0001
• Result is 7FFFh-0001=7FFEh
• ZF=SF=OF=0

W.P.Fang Department of CSIE 177


YUST, Copy Right Reserved
• DEC AX
• JL THERE

W.P.Fang Department of CSIE 178


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

W.P.Fang Department of CSIE 179


YUST, Copy Right Reserved
• Ex:
– If AX=7FFFh
– BX=8000h
– CMP AX,BX
– JA BELOW
• Because 7FFFh<8000h in an unsigned
sense àdoes not jump to BELOW
• Ps: in signed 7FFFh>8000h
W.P.Fang Department of CSIE 180
YUST, Copy Right Reserved
Working with Characters
• Example. Suppose AX and BX contain
signed numbers, write some code to put
the biggest one in CX
• Solution:
– MOV CX,AX
– CMP BX,CX
– JLE NEXT
– MOVE CX,BX
– NEXT:
W.P.Fang Department of CSIE 181
YUST, Copy Right Reserved
The JMP instruction
• JMP destination
• JMP can be used to get around the range
restriction of a conditional jump

W.P.Fang Department of CSIE 182


YUST, Copy Right Reserved
• TOP:
• <<if too far>>
• DEC CX
• JNZ TOP
• MOV AX,BX

W.P.Fang Department of CSIE 183


YUST, Copy Right Reserved
• TOP:
– DEC CX
– JNZ BOTTOM
– JMP EXIT
• BOTTOM:
– JMP TOP
• EXIT:
– MOV AX,BX

W.P.Fang Department of CSIE 184


YUST, Copy Right Reserved
High-Level Language structure
• IF-THEN
• IF-THEN-ELSE
• CASE
• Branch with compound conditions
• AND conditions
• OR conditions

W.P.Fang Department of CSIE 185


YUST, Copy Right Reserved
IF-THEN
• IF condition is true
• THEN
– Execute true-branch statements
• END_IF

W.P.Fang Department of CSIE 186


YUST, Copy Right Reserved
• Ex :replace the number in AX by its
absolute value

W.P.Fang Department of CSIE 187


YUST, Copy Right Reserved
• IF AX<0
• THEN
– Replace AX by –AX
• END_IF

W.P.Fang Department of CSIE 188


YUST, Copy Right Reserved
• CMP AX,0
– JNL END_IF
– NEG AX
• END_IF:

W.P.Fang Department of CSIE 189


YUST, Copy Right Reserved
IF-THEN-ELSE
• IF condition is true
– THEN
• Execute true-branch statements
– ELSE
• Execute false-branch statements
– END_IF

W.P.Fang Department of CSIE 190


YUST, Copy Right Reserved
• Ex: suppose AL and BL contain extended
ASCII characters. Display the one the
comes first in the character sequence

W.P.Fang Department of CSIE 191


YUST, Copy Right Reserved
• IF AL<=BL
– THEN
• Display the character in AL
– ELSE
• Display the character in BL
– END_IF

W.P.Fang Department of CSIE 192


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

W.P.Fang Department of CSIE 193


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

W.P.Fang Department of CSIE 194


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

W.P.Fang Department of CSIE 195


YUST, Copy Right Reserved
• D.
– If AX<BX
• THEN
– IF BX<CX
» THEN
» Put 0 in AX
– ELSE
» Put 0 in BX
– END_IF
• END_IF

W.P.Fang Department of CSIE 196


YUST, Copy Right Reserved
•E
• IF(AX<BX) OR (BX<CX)
– THEN
• Put 0 in BX
– ELSE
• Put 1 in DX
– END_IF

W.P.Fang Department of CSIE 197


YUST, Copy Right Reserved
HW
• Ch6. 3a,12

W.P.Fang Department of CSIE 198


YUST, Copy Right Reserved
CASE
• Multiway branch structure
• CASE expression
– Value1:statement 1
– Value2:statement 2
– :
– :
– Valuen:statement n
• END_CASE
W.P.Fang Department of CSIE 199
YUST, Copy Right Reserved
ex
• If AX contains a negative number, put -1 in
BX;if AX contains 0, put 0 in BX;if AX
contains a positive number,put 1 in BX.

W.P.Fang Department of CSIE 200


YUST, Copy Right Reserved
• CASE AX
– <0:put -1 in BX
– =0:put 0 in BX
– >0:put 1 in BX

W.P.Fang Department of CSIE 201


YUST, Copy Right Reserved
– CMP AX,0
– JL NEGATIVE
– JE ZERO
– JG POSITIVE
• NEGATIVE:
– MOV BX,-1
– JMP END_CASE
• ZERO:
– MOV BX,0
– JMP END_CASE
• POSITIVE:
– MOV BX,1
• END_CASE:

W.P.Fang Department of CSIE 202


YUST, Copy Right Reserved
ex
• If AL contains 1 or 3, display “0”; if AL
contains 2 or 4, display “e”

W.P.Fang Department of CSIE 203


YUST, Copy Right Reserved
• CASE AL
– 1,3:display ‘o’
– 2,4:display ‘e’

W.P.Fang Department of CSIE 204


YUST, Copy Right Reserved
– CMP AL,1
– JE ODD
– CMP AL,3
– JE ODD
– CMP Al,2
– JE EVEN
– CMP AL,4
– JE EVEN
– JMP END_CASE
• ODD:
– MOV DL,’o’
– JMP DISPLAY
• EVEN:
– MOV DL,’e’
• DISPLAY:
– MOV AH,2
– INT 21H
• END_CASE:

W.P.Fang Department of CSIE 205


YUST, Copy Right Reserved
Branches with compound
conditions
• Condition_1 AND condition_2
• Comdition_1 OR condition_2

W.P.Fang Department of CSIE 206


YUST, Copy Right Reserved
AND conditions
• Ex:read a character, and if it’d an
uppercase letter,display it

W.P.Fang Department of CSIE 207


YUST, Copy Right Reserved
• Read a character (into AL)
• IF (‘A’<= character) and (character <=‘Z’)
• THEN
– Display character
• END_IF

W.P.Fang Department of CSIE 208


YUST, Copy Right Reserved
– MOV AH,1
– INT 21H
– CMP AL.’A’
– JNGE END_IF
– CMP AL,’Z’
– JNLE END_IF
– MOV DL,AL
– MOV AH,2
– INT 21H
• END_IF
W.P.Fang Department of CSIE 209
YUST, Copy Right Reserved
OR conditions
• Read a character. IF it’s “y” or “Y”, display
it; otherwise, terminate the program.

W.P.Fang Department of CSIE 210


YUST, Copy Right Reserved
• Read a character (into AL)
• IF (character=‘y’) OR (character=‘Y’)
– THEN
• Display it
– ELSE
• Terminate the program
– END_IF

W.P.Fang Department of CSIE 211


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

W.P.Fang Department of CSIE 213


YUST, Copy Right Reserved
FOR LOOP
• FOR loop_count times DO
– Statements
• END_FOR

W.P.Fang Department of CSIE 214


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

W.P.Fang Department of CSIE 215


YUST, Copy Right Reserved
• TOP:
– LOOP TOP

W.P.Fang Department of CSIE 216


YUST, Copy Right Reserved
ex
• Write a count-controlled loop to display a
row of 80 stars

W.P.Fang Department of CSIE 217


YUST, Copy Right Reserved
• FOR 80 times DO
– Display ‘*’
• END_FOR

W.P.Fang Department of CSIE 218


YUST, Copy Right Reserved
– MOV CX,80
– MOV AH,2
– MOV DL,’*’
• TOP:
– INT 21h
– LOOP TOP

W.P.Fang Department of CSIE 219


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

W.P.Fang Department of CSIE 220


YUST, Copy Right Reserved
• JCXZ destination_label

W.P.Fang Department of CSIE 221


YUST, Copy Right Reserved
– JCXZ SKIP
• TOP:
– LOOP TOP
• SKIP:

W.P.Fang Department of CSIE 222


YUST, Copy Right Reserved
WHILE LOOP
• WHILE condition DO
– Statements
• END_WHILE

W.P.Fang Department of CSIE 223


YUST, Copy Right Reserved
ex
• Write some code to count the number of
characters in an input line

W.P.Fang Department of CSIE 224


YUST, Copy Right Reserved
• Initialize count to 0
• Read a character
• WHILE character<> carriage_return DO
– Count=count+1
– Read a character
• END_WHILE

W.P.Fang Department of CSIE 225


YUST, Copy Right Reserved
– MOV DX,0
– MOV AH,1
– INT 21H
• WHILE_:
– CMP AL,0DH
– JE END_WHILE
– INC DX
– INT 21H
– JMP WHILE_
• END_WHILE

W.P.Fang Department of CSIE 226


YUST, Copy Right Reserved
REPEAT LOOP
• REPEAT
• Statements
• UNTIL condition

W.P.Fang Department of CSIE 227


YUST, Copy Right Reserved
ex
• Write some code to read characters until a
blank is read

W.P.Fang Department of CSIE 228


YUST, Copy Right Reserved
• REPEAT
– Read a character
• UNTIL character is a blank

W.P.Fang Department of CSIE 229


YUST, Copy Right Reserved
– MOV AH,1
• REPEAT:
– INT 21H
– CMP AL.’‘
– JNE REPEAT

W.P.Fang Department of CSIE 230


YUST, Copy Right Reserved
WHILE Versus REPEAT
• Use of a WHILE loop or a REPEAT loop is
a matter of personal preference
• The advantage of a WHILE is that the loop
can be bypassed if the terminating
condition is initially false, whereas the
statements in a REPEAT must be done at
least once.

W.P.Fang Department of CSIE 231


YUST, Copy Right Reserved
ex
• Type a line of text
• THE QUICK BROWN FOX JUMPED
• First capital =B last capital =X

W.P.Fang Department of CSIE 232


YUST, Copy Right Reserved
• Display the opening message
• Read and process a line of text
• Display the results

W.P.Fang Department of CSIE 233


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

W.P.Fang Department of CSIE 234


YUST, Copy Right Reserved
CMP AL,0DH
JE END_WHILE
CMP AL,'A'
JNGE END_IF
CMP AL,'Z'
JNLE END_IF
CMP AL,FIRST
JNL CHECK_LAST
MOV FIRST,AL
CHECK_LAST:
CMP AL,LAST
JNG END_IF
MOV LAST,AL
END_IF:
INT 21H
JMP WHILE_
END_WHILE:

W.P.Fang Department of CSIE 235


YUST, Copy Right Reserved
MOV AH,9
CMP FIRST,']'
JNE CAPS
LEA DX,NOCAP_MSG
JMP DISPLAY
CAPS:
LEA DX,CAP_MSG
DISPLAY:
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

W.P.Fang Department of CSIE 236


YUST, Copy Right Reserved
hw
• Ch6.3a,12

W.P.Fang Department of CSIE 237


YUST, Copy Right Reserved
Logic, Shift ,and Rotate
Instructions

W.P.Fang Department of CSIE 238


YUST, Copy Right Reserved
Logic, Shift,and Rotate Instructions
• Logic instructions
– AND
– OR
– XOR
– NOT

W.P.Fang Department of CSIE 239


YUST, Copy Right Reserved
Logic instructions
• Perform the following logic operations
– 10101010 AND 11110000
– 10101010 OR 11110000
– 10101010 XOR 11110000
– NOT 10101010

W.P.Fang Department of CSIE 240


YUST, Copy Right Reserved
Truth tables
(0=false ,1=true)
a b A AND b A OR b A XOR b

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

W.P.Fang Department of CSIE 241


YUST, Copy Right Reserved
instructions
• AND destination, source
• OR destination, source
• XOR destination, source
• -------------------------------------------
• Effect on flags
– SF,ZF,PF reflect the result
– AF is undefined
– CF,OF=0
W.P.Fang Department of CSIE 242
YUST, Copy Right Reserved
• One use of AND, OR, and XOR is to
selectively modify the bits in the
destination.
• mask

W.P.Fang Department of CSIE 243


YUST, Copy Right Reserved
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 244


YUST, Copy Right Reserved
• The AND instruction can be used to clear
specific destination bits while preserving
the others
• The OR instruction can be used to set
specific destination bits while preserving
the others.
• The XOR instruction can be used to
complement specific destination bits while
preserving the others.
W.P.Fang Department of CSIE 245
YUST, Copy Right Reserved
ex
• Clear the sign bit of AL while leaving the
other bits unchanged

W.P.Fang Department of CSIE 246


YUST, Copy Right Reserved
• AND AL,7Fh

• Ps:
• 01111111b=7Fh

W.P.Fang Department of CSIE 247


YUST, Copy Right Reserved
Ex
• Set the most significant and least
significant bits of AL while preserving the
other bits

W.P.Fang Department of CSIE 248


YUST, Copy Right Reserved
• OR AL,81h

• Ps:10000001b=81h

W.P.Fang Department of CSIE 249


YUST, Copy Right Reserved
ex
• Change the sign bit of DX

W.P.Fang Department of CSIE 250


YUST, Copy Right Reserved
• XOR DX,8000h

W.P.Fang Department of CSIE 251


YUST, Copy Right Reserved
Converting an ASCII digit to a
Number
• “5”à 5
• Method 1:SUB AL,20h
• Method 2:AND AL,0Fh

W.P.Fang Department of CSIE 252


YUST, Copy Right Reserved
exercise
• Converting a stored decimal digit to its
ASCII code

W.P.Fang Department of CSIE 253


YUST, Copy Right Reserved
Converting a lowercase letter to
upper case
character code character code

a 01100001 A 01000001

b 01100010 B 01000010

: : : :

z 01111010 Z 01011010

W.P.Fang Department of CSIE 254


YUST, Copy Right Reserved
• To convert lower to upper case we need
only clear bit 5
• à AND with mask 11011111b (0DFh)
• AND DL,0DFh

W.P.Fang Department of CSIE 255


YUST, Copy Right Reserved
Clearing a Register
• MOV AX,0
– Machine needs three bytes
– Because prohibition on memory-on-memory
operations, must be used to clear a memory
location
• SUB AX,AX
– Machine needs two bytes
• XOR AX,AX
– Machine needs two bytes

W.P.Fang Department of CSIE 256


YUST, Copy Right Reserved
Testing a register for zero
• OR CX,CX
• CMP CX,0
• Because 1 OR 1 =1, 0 OR 0 =0
• CX unchanged
• But affect ZF and SF

W.P.Fang Department of CSIE 257


YUST, Copy Right Reserved
NOT instruction
• NOT destination

W.P.Fang Department of CSIE 258


YUST, Copy Right Reserved
ex
• Complement the bits in AX

W.P.Fang Department of CSIE 259


YUST, Copy Right Reserved
• NOT AX

W.P.Fang Department of CSIE 260


YUST, Copy Right Reserved
TEST instruction
• The TEST instruction performs and an
AND operation of the destination with the
source but does not change the
destination contents
• The purpose of the TESt instruciton is to
set the status flags

W.P.Fang Department of CSIE 261


YUST, Copy Right Reserved
• TEST destination,source
• ------------------------------------
• Effect on flags
– SF,ZF, PF reflect the result
– AF is undefined
– CF,OF=0

W.P.Fang Department of CSIE 262


YUST, Copy Right Reserved
Examining bits
• TEST destination,mask
• If destination has 0’s in all the etsted
position, the result will be 0 and so ZF=1

W.P.Fang Department of CSIE 263


YUST, Copy Right Reserved
ex
• Jump to label BELLOW if AL contains an
even number

W.P.Fang Department of CSIE 264


YUST, Copy Right Reserved
• Even numbers have a 0 in bit 0. thus, the
mask is 00000001b=1
• TESt AL,1
• JZ BELLOW

W.P.Fang Department of CSIE 265


YUST, Copy Right Reserved
Shift instructions
• Opcode destination,CL

W.P.Fang Department of CSIE 266


YUST, Copy Right Reserved
Left Shift instructions
• SHL destination,1
• SHL destination, CL
• ---------------------------------
• Effect on flags
– SF,PF,ZF reflect the result
– AF is undefined
– CF=last bit shifted out
– OF=1 if result changes sign on last shift
W.P.Fang Department of CSIE 267
YUST, Copy Right Reserved
ex
• Suppose DH contain 8Ah and CL contains
3, what are the values of DH and CF after
the instruction SHL DH,CL is executed?

W.P.Fang Department of CSIE 268


YUST, Copy Right Reserved
• DH=10001010
•à
• 01010000b=50h

W.P.Fang Department of CSIE 269


YUST, Copy Right Reserved
Multiplication by Left Shift
• A left shift on a binary number multiplies it
by 2.
• Ex:
• 00000101b = 5d
• 00001010b =10d
• 00010100b = 20d

W.P.Fang Department of CSIE 270


YUST, Copy Right Reserved
SAL
• Shift arithmetic left
• the same machine code with SHL

W.P.Fang Department of CSIE 271


YUST, Copy Right Reserved
• Negative number can also be multiplied by
powers of 2 by left shifts
• Ex:
– FFFFh(-1) à FFF8h(-8)

W.P.Fang Department of CSIE 272


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

W.P.Fang Department of CSIE 273


YUST, Copy Right Reserved
ex
• BL=80h
• CL=2
• SHL BL,CL
• à CF=OF=0
• (this occur signed and unsigned overflow)

W.P.Fang Department of CSIE 274


YUST, Copy Right Reserved
ex
• Write some code to multiply th evalue of
AX by 8

W.P.Fang Department of CSIE 275


YUST, Copy Right Reserved
• MOV CL,3
• SAL AX,CL

W.P.Fang Department of CSIE 276


YUST, Copy Right Reserved
Right Shift instruction
• SHR destination,1
• SHR destination, CL
• --------------------------------
• Effect on the flags is the same as for SHL

W.P.Fang Department of CSIE 277


YUST, Copy Right Reserved
ex
• Suppose DH contains 8AH and CL
contains 2. what are the values of DH and
CF after the instruction SHR DH,CL is
executed?

W.P.Fang Department of CSIE 278


YUST, Copy Right Reserved
• 10001010
•à
• 00100010b=22h

W.P.Fang Department of CSIE 279


YUST, Copy Right Reserved
SAR
• Shift arithmetic right
• SAR destination,1
• SAR destination, CL
• The effect on flags is the same as for SHR
• Different with SHR :
– The msb retains its original value

W.P.Fang Department of CSIE 280


YUST, Copy Right Reserved
Division by right shift
• 00000101b=5
• 00000010b=2

W.P.Fang Department of CSIE 281


YUST, Copy Right Reserved
Signed and unsigned division
• Signed : SAR
• Unsigned: SHR

W.P.Fang Department of CSIE 282


YUST, Copy Right Reserved
ex
• Use right shifts to divide the unsigned
number 65143 by 4. put the quotient in AX

W.P.Fang Department of CSIE 283


YUST, Copy Right Reserved
• MOV AX,65143
• MOV CL,2
• SHR AX, CL

W.P.Fang Department of CSIE 284


YUST, Copy Right Reserved
ex
• If AL contains -15, give the decimal value
of Al after SAR AL,1 is performed

W.P.Fang Department of CSIE 285


YUST, Copy Right Reserved
• Execution of SAR AL,1 divides the number
by 2 and rounds down
• -15 /2 = -7.5
• We get -8
• Ps:
• -15=11110001b
• -8=11111000b

W.P.Fang Department of CSIE 286


YUST, Copy Right Reserved
General multiplication and division
• MUL
• IMUL
• DIV
• IDIV

W.P.Fang Department of CSIE 287


YUST, Copy Right Reserved
Rotate instructions
• Rotate left
• Shift bits to left.
• The msb is shifted into the rightmost bit
• ROL destination,1
• ROL destination, CL

W.P.Fang Department of CSIE 288


YUST, Copy Right Reserved
• Rotate right
• ROR destination,1
• ROR destination, CL

W.P.Fang Department of CSIE 289


YUST, Copy Right Reserved
ex
• Use ROL to count the number of 1 bits in
BX, without changing BX. Put the answer
in AX

W.P.Fang Department of CSIE 290


YUST, Copy Right Reserved
– XOR AX,AX
– MOV CX,16
• TOP:
– ROL BX,1
– JNC NEXT
– INC AX
• NEXT:
– LOOP TOP

W.P.Fang Department of CSIE 291


YUST, Copy Right Reserved
Rotate Carry left
• Rotate through Carry Left
• The msb is shifted into CF
• The previous value of CF is shifted into the
rightmost bit
• RCL destination ,1
• RCL destination, CL

W.P.Fang Department of CSIE 292


YUST, Copy Right Reserved
Rotate carry right
• RCR destination,1
• RCR destination, CL

W.P.Fang Department of CSIE 293


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

W.P.Fang Department of CSIE 294


YUST, Copy Right Reserved
CF DH

Initial values 1 10001010

After 1 right 0 11000101


rotation
After 2 right 1 01100010
rotation
After 3 right 0 10110001=B1h
rotations
W.P.Fang Department of CSIE 295
YUST, Copy Right Reserved
• Effect of the rotate instructions on flags
– SF,PF,ZF reflect the result
– AF is undefined
– CF=last bit shifted out
– OF=1 if result changes sign on the last
rotation

W.P.Fang Department of CSIE 296


YUST, Copy Right Reserved
reversing a bit pattern
• 11011100à00111011
– MOV CX,8
• REVERSE:
– SHL AL,1
– RCR BL,1
– LOOP REVERSE
– MOV AL,BL

W.P.Fang Department of CSIE 297


YUST, Copy Right Reserved
Binary and Hex I/O
• Reads in a binary number from the
keyboard, followed by a carriage return
• Convert it to a bit value, and collect the
bits in a register

W.P.Fang Department of CSIE 298


YUST, Copy Right Reserved
• Clear BX
• Input a character
• WHILE character <> CR DO
– Convert character to binary value
– Left shift BX
– Insert value int lsb of BX
– Input a character
• END_WHILE
W.P.Fang Department of CSIE 299
YUST, Copy Right Reserved
– XOR BX,BX
– MOV Ah,1
– INT 21H
• WHILE_:
– CMP AL,0DH
– JE END_WHILE
– AND AL,0FH
– SHL BX,1
– OR BL,AL
– INT 21H
– JMP WHILE_
• END_WHILE:
W.P.Fang Department of CSIE 300
YUST, Copy Right Reserved
Binary output
• FOR 16 times DO
– Rotate left BX
• Put msb nito CF
– IF CF=1
– THEN
• Output ‘1’
– ELSE
• Output ‘0’
– END_IF
• END_FOR
W.P.Fang Department of CSIE 301
YUST, Copy Right Reserved
Hex input
• Clear BX
• Input hex character
• WHILE character<> CR DO
– Convert character to binary value
– Left shift BX 4 times
– Insert value into lower 4 bits of BX
– Input a character
• END_WHILE
W.P.Fang Department of CSIE 302
YUST, Copy Right Reserved
– XOR BX,BX
– MOV CL,4
– MOV AH,1
– INT 21H
• WHILE_:
– CMP AL,0DH
– JE END_WHILE
– CMP AL,39H
– JG LETTER
– AND AL,0FH
– JMP SHIFT
• LETTER:
– SUB AL,37H
• SHIFT:
– SHL BX,CL
– OR BL,AL
– INT 21H
– JMP WHILE_
• END_WHILE:

W.P.Fang Department of CSIE 303


YUST, Copy Right Reserved
Hex output
• For 4 times DO
– MOV BH to DL
– Shift DL 4 times to the right
– IF DL<10
• THEN
– Convert to character in ‘0’.. ‘9’
• ELSE
– Convert to character in ‘A’.. ‘F’
• END_IF
• Output character
• Rotate BX left 4 times
– END_FOR

W.P.Fang Department of CSIE 304


YUST, Copy Right Reserved
homework
• Ch7.2,7.4,14

W.P.Fang Department of CSIE 305


YUST, Copy Right Reserved
The Stack and Introduction to
procedures

W.P.Fang Department of CSIE 306


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

W.P.Fang Department of CSIE 307


YUST, Copy Right Reserved
• Declaring a stack segment
• .STACK 100H

W.P.Fang Department of CSIE 308


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

W.P.Fang Department of CSIE 309


YUST, Copy Right Reserved
• PUSH source
• Ex:
– PUSH AX

W.P.Fang Department of CSIE 310


YUST, Copy Right Reserved
• 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 311


YUST, Copy Right Reserved
• PUSHF:
– Pushes the contents of the FLAGS register
onto the stack.

W.P.Fang Department of CSIE 312


YUST, Copy Right Reserved
• 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 313


YUST, Copy Right Reserved
• POP destination
• Ex:
– POP BX

W.P.Fang Department of CSIE 314


YUST, Copy Right Reserved
• The content of SS:SP is moved to the
destination
• SP is increased by 2

W.P.Fang Department of CSIE 315


YUST, Copy Right Reserved
• POPF:
– Pop the top of the stack into the FLAGS
register.

W.P.Fang Department of CSIE 316


YUST, Copy Right Reserved
• 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 317


YUST, Copy Right Reserved
A stack application
• Read a sequence of characters and
display them in reverse order on the next
line

W.P.Fang Department of CSIE 318


YUST, Copy Right Reserved
• Display a ‘?’
• Initialize count to 0
• Read a character
• WHILE character is not a carriage return DO
– Push character onto the stack
– Increment count
– Read a character
• END_WHILE
• Go to a new line
• FOR count times DO
– Pop a character from the stack
– Display it
• END_FOR
W.P.Fang Department of CSIE 319
YUST, Copy Right Reserved
• TITLE PGM8_1:REVERSE INPUT
• .MODEL SMALL
• .STACK 100H
• .CODE
• MAIN PROC
– MOV AH,2
– MOV DL.’?’
– INT 21H
– XOR CX,CX
– MOV AH,1
– INT 21H

W.P.Fang Department of CSIE 320


YUST, Copy Right Reserved
• WHILE_:
– CMP AL,0DH
– JE END_WHILE
– PUSH AX
– INC CX
– INT 21H
– JMP WHILE_
• END_WHILE:
– MOV AH,2
– MOV DL,0DH
– INT 21H
– MOV DL,0AH
– INT 21H
– JCXZ EXIT

W.P.Fang Department of CSIE 321


YUST, Copy Right Reserved
• TOP:
– POP DX
– INT 21H
– LOOP TOP
• EXIT:
• MOV AH,4CH
• INT 21H
• MAIN ENDP
– END MAIN

W.P.Fang Department of CSIE 322


YUST, Copy Right Reserved
Terminology of procedures
• Procedure declaration
– Name PROC type
• RET
– Name ENDP

W.P.Fang Department of CSIE 323


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

W.P.Fang Department of CSIE 324


YUST, Copy Right Reserved
• RET
– Return
– Causes control to transfer back to the calling
procedure
– Every procedure should have a RET
– Except main procedure

W.P.Fang Department of CSIE 325


YUST, Copy Right Reserved
communication
• Up to the programmer to devise a way for
procedures to communicate
• Unlike high-level language, do not have
parameter lists

W.P.Fang Department of CSIE 326


YUST, Copy Right Reserved
Procedure documentation
• ;(describe what the procedure does)
• ; input: (where it receives information from
the calling program)
• ;output: (where it delivers results to the
calling program)
• ;users: (a list of procedures that it calls)

W.P.Fang Department of CSIE 327


YUST, Copy Right Reserved
CALL and RET
• CALL name
• CALL address_expression

W.P.Fang Department of CSIE 328


YUST, Copy Right Reserved
• 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 329


YUST, Copy Right Reserved
• RET pop_value
• NEAR : pop IP
• FAR : pop CS:IP

W.P.Fang Department of CSIE 330


YUST, Copy Right Reserved
Offset address
Code segment

MAIN PROC

IP 0010 CALL PROC1


0012 Next instruction Offset Stack segment
address

PROC1 PROC
0200 First instruction
0100

00FE
RET
00FC SP

W.P.Fang Department of CSIE 331


YUST, Copy Right Reserved
Offset address
Code segment

MAIN PROC

0010 CALL PROC1


0012 Next instruction Offset Stack segment
address

IP PROC1 PROC
0200 First instruction
0100

00FE 0012 SP
RET
00FC

W.P.Fang Department of CSIE 332


YUST, Copy Right Reserved
Offset address
Code segment

MAIN PROC

0010 CALL PROC1


0012 Next instruction Offset Stack segment
address

PROC1 PROC
0200 First instruction
0100

00FE 0012 SP
IP
0300 RET
00FC

W.P.Fang Department of CSIE 333


YUST, Copy Right Reserved
Offset address
Code segment

MAIN PROC
IP 0010 CALL PROC1
0012 Next instruction Offset Stack segment
address

PROC1 PROC
0200 First instruction
0100

00FE
0300 RET
00FC SP

W.P.Fang Department of CSIE 334


YUST, Copy Right Reserved
example
• Finding the product of two positive
integers A and B by addition and bit
shifting

W.P.Fang Department of CSIE 335


YUST, Copy Right Reserved
• Product=0
• REPEAT
– IF lsb of B is 1
– THEN
• Product=Product +A
– END_IF
– Shift left A
– Shift right B
• UNTIL B=0
W.P.Fang Department of CSIE 336
YUST, Copy Right Reserved
• Ex:
– A=111b
– B=1101b
111b
x 1101b
---------------------
111
000
111
111
---------------------
1011011b
W.P.Fang Department of CSIE 337
YUST, Copy Right Reserved
• TITLE PGM8_2:REVERSE INPUT
• .MODEL SMALL
• .STACK 100H
• .CODE
• MAIN PROC
• CALL MULTIPLY
• MOV AH,4CH
• INT 21H
• MAIN ENDP
• MULTIPLY PROC
• PUSH AX
• PUSH BX
• XOR DX,DX
• REPEAT:
• TEST BX,1
• JZ END_IF
• ADD DX,AX
• END_IF:
• SHL AX,1
• SHR BX,1
• JNZ REPEAT
• POP BX
• POP AX
• RET
• MULTIPLY ENDP
• END MAIN

W.P.Fang Department of CSIE 338


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

W.P.Fang Department of CSIE 339


YUST, Copy Right Reserved
-rAX=0000 BX=0000 CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0000 NV UP EI PL NZ NA PO NC
0B87:0000 E80400 CALL 0007
-dss:f0 ff0B89:00F0 23 05 00 00 0B 00 87 0B-7A 05 0D 00 07 00 00 00

#.......z.......
-raxAX 0000:7
-rbxBX 0000:d
-rAX=0007 BX=000D CX=001C DX=0000 SP=0100 BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0000 NV UP EI PL NZ NA PO NC
0B87:0000 E80400 CALL 0007
-t
AX=0007 BX=000D CX=001C DX=0000 SP=00FE BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0007 NV UP EI PL NZ NA PO NC
0B87:0007 50 PUSH AX
-dss: f0 ff0B89:00F0 23 05 00 00 07 00 00 00-07 00 87 0B 7A 05 03 00

#...........z...

W.P.Fang Department of CSIE 340


YUST, Copy Right Reserved
-gb
AX=0007 BX=000D CX=001C DX=0000 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL ZR NA PE NC
0B87:000B F7C30100 TEST BX,0001
-dss: f0 ff0B89:00F0 23 05 00 00 0B 00 87 0B-7A 05 0D 00 07 00 03 00

#.......z.......
-g17
AX=000E BX=0006 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PE CY
0B87:0017 75F2 JNZ 000B
-t
AX=000E BX=0006 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PE CY
0B87:000B F7C30100 TEST BX,0001
-g17
AX=001C BX=0003 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PE NC
0B87:0017 75F2 JNZ 000B

W.P.Fang Department of CSIE 341


YUST, Copy Right Reserved
-t
AX=001C BX=0003 CX=001C DX=0007 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PE NC
0B87:000B F7C30100 TEST BX,0001
-g17
AX=0038 BX=0001 CX=001C DX=0023 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL NZ NA PO CY
0B87:0017 75F2 JNZ 000B
-t
AX=0038 BX=0001 CX=001C DX=0023 SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=000B NV UP EI PL NZ NA PO CY
0B87:000B F7C30100 TEST BX,0001
-g17
AX=0070 BX=0000 CX=001C DX=005B SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0017 NV UP EI PL ZR NA PE CY
0B87:0017 75F2 JNZ 000B
-t
AX=0070 BX=0000 CX=001C DX=005B SP=00FA BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0019 NV UP EI PL ZR NA PE CY
0B87:0019 5B POP BX
W.P.Fang Department of CSIE 342
YUST, Copy Right Reserved
-t
AX=0070 BX=000D CX=001C DX=005B SP=00FC BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=001A NV UP EI PL ZR NA PE CY
0B87:001A 58 POP AX
-t
AX=0007 BX=000D CX=001C DX=005B SP=00FE BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=001B NV UP EI PL ZR NA PE CY
0B87:001B C3 RET
-dss:f0 ff0B89:00F0 70 00 70 00 07 00 00 00-1B 00 87 0B 7A 05 03 00

p.p.........z...
-t
AX=0007 BX=000D CX=001C DX=005B SP=0100 BP=0000 SI=0000 DI=0000
DS=0B77 ES=0B77 SS=0B89 CS=0B87 IP=0003 NV UP EI PL ZR NA PE CY
0B87:0003 B44C MOV AH,4C
-g
Program terminated normally
-q

W.P.Fang Department of CSIE 343


YUST, Copy Right Reserved
HW
• Ch8.9

W.P.Fang Department of CSIE 344


YUST, Copy Right Reserved
Multiplication and division
instruction

W.P.Fang Department of CSIE 345


YUST, Copy Right Reserved
MUL and IMUL
• The process of multiplication and division
is different for signed and unsigned
number
• There are byte and word forms

W.P.Fang Department of CSIE 346


YUST, Copy Right Reserved
ex
• 10000000 x 1111111
– Unsigned:
128x255=32640(0111111110000000
– Signed:
-128x-1=128(0000000010000000

W.P.Fang Department of CSIE 347


YUST, Copy Right Reserved
• MUL (multiply)
• IMUL (integer multiply)

W.P.Fang Department of CSIE 348


YUST, Copy Right Reserved
• MUL source
• IMUL source

W.P.Fang Department of CSIE 349


YUST, Copy Right Reserved
Byte Form
• One umber is contained in the source
• The other is assumed to be in AL
• 16-bit production will be in AX
• Ps:
– Source may be a byte register or memory
byte, but not a constant

W.P.Fang Department of CSIE 350


YUST, Copy Right Reserved
Word form
• One number is contained in the source
• The other is assumed to be in AX
• The most significant 16 bits of the double
word product will be in DX
• The least significant 16 bits will be in AX
• DX:AX

W.P.Fang Department of CSIE 351


YUST, Copy Right Reserved
• For positive number ( 0 in the most
significant bit) MUL and IMUL give the
same result

W.P.Fang Department of CSIE 352


YUST, Copy Right Reserved
Effect of MUL/IMUL on the status
flags
• SF,ZF,AF,PF: undefined
• CF/OF:
– After MUL,CF/OF
• =1 it he upper half of the result is zero
• =1 otherwise
– After IMUL, CF/OF
• =0 if the upper half of the result is the sign
extension of the lower half ( the bits of the upper
half are the same as the sign bit of the lower half)
• =1 otherwise

W.P.Fang Department of CSIE 353


YUST, Copy Right Reserved
• If CF/OF=1
– The product is too big to fit in the lower half of
the destination (ALor AX)

W.P.Fang Department of CSIE 354


YUST, Copy Right Reserved
ex
• Suppose AX contains 1 and BX contains
FFFFh

W.P.Fang Department of CSIE 355


YUST, Copy Right Reserved
Instruction Decimal Hex DX AX CF/OF
product product

MUL BX 65535 0000FFFF 0000 FFFF 0

IMUL BX -1 FFFFFFF FFFF FFFF 0


F

W.P.Fang Department of CSIE 356


YUST, Copy Right Reserved
ex
• Suppose AX contains FFFFh and BX
contains FFFFh

W.P.Fang Department of CSIE 357


YUST, Copy Right Reserved
Instruction Decimal Hex DX AX CF/OF
product product

MUL BX 42948362 FFFE000 FFFE 0001 1


25 1

IMUL BX 1 00000001 0000 0001 0

W.P.Fang Department of CSIE 358


YUST, Copy Right Reserved
ex
• Suppose AX contains 0FFFh

W.P.Fang Department of CSIE 359


YUST, Copy Right Reserved
Instruction Decimal Hex DX AX CF/OF
product product

MUL AX 16769025 00FFE001 00FF E001 1

IMUL AX 16769025 00FFE001 00FF 001 1

W.P.Fang Department of CSIE 360


YUST, Copy Right Reserved
ex
• Suppose AX contains 0100h and CX
contains FFFFh

W.P.Fang Department of CSIE 361


YUST, Copy Right Reserved
Instruction Decimal Hex DX AX CF/OF
product product

MUL CX 16776960 00FFFF00 00FF FF00 1

IMUL CX -256 FFFFFF0 FFFF FF00 0


0

W.P.Fang Department of CSIE 362


YUST, Copy Right Reserved
ex
• Suppose AL contains 80h and BL contains
FFh

W.P.Fang Department of CSIE 363


YUST, Copy Right Reserved
Instruction Decimal Hex AH AL CF/OF
product product

MUL BL 128 7F80 7F 80 1

IMUL BL 128 0080 00 80 1

W.P.Fang Department of CSIE 364


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

W.P.Fang Department of CSIE 365


YUST, Copy Right Reserved
• MOV AX,5
• IMUL A
• MOV A,AX
• MOV AX,12
• IMUL B
• SUB A,AX

W.P.Fang Department of CSIE 366


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

W.P.Fang Department of CSIE 367


YUST, Copy Right Reserved
• N!=1 if N=1
• =Nx(N-1)x(N-2)x… x1 if N>1

W.P.Fang Department of CSIE 368


YUST, Copy Right Reserved
• Product =1
• Term=N
• FOR N times DO
– product=product x term
– term=trem-1
• ENDFOR

W.P.Fang Department of CSIE 369


YUST, Copy Right Reserved
• FACTORIAL PROC
– MOV AX,1
• TOP:
– MUL CX
– LOOP TOP
– RET
• FACTORIAL ENDP
• Ps: CX=N,AX=N!
W.P.Fang Department of CSIE 370
YUST, Copy Right Reserved
DIV and IDIV
• When division is performed, we obtain two
results
– Quotient
– remainder

W.P.Fang Department of CSIE 371


YUST, Copy Right Reserved
• DIV divisor
• IDIV divisor

W.P.Fang Department of CSIE 372


YUST, Copy Right Reserved
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 373


YUST, Copy Right Reserved
Word form
• The divisor is a 16-bit register or memory
• 32-bit dividend is assumed to be in DX:AX
• 16-bit quotient is in AX
• 16-bit remainder is in DX
• For signed division, the remainder has the
same sign as the dividend

W.P.Fang Department of CSIE 374


YUST, Copy Right Reserved
• The effect of DIV/IDIV on the flags is that
all status flags are undefined

W.P.Fang Department of CSIE 375


YUST, Copy Right Reserved
Divide overflow
• The quotient will be too big to fit in the
specified destination
• Devisor is much smaller than dividend

W.P.Fang Department of CSIE 376


YUST, Copy Right Reserved
ex
• Suppose DX contains 0000h, AX contains
0005h , and BX contains 0002h

W.P.Fang Department of CSIE 377


YUST, Copy Right Reserved
instruction Decimal Decimal AX DX
quotient remainder

DIV BX 2 1 0002 0001

IDIV BX 2 1 0002 0001

W.P.Fang Department of CSIE 378


YUST, Copy Right Reserved
ex
• Suppose DX contains 0000h, AX contains
0005h, and BX contains FFFEh

W.P.Fang Department of CSIE 379


YUST, Copy Right Reserved
instruction Decimal Decimal AX DX
quotient remainder

DIV BX 0 5 0000 0005

IDIV BX -2 1 FFFE 0001

W.P.Fang Department of CSIE 380


YUST, Copy Right Reserved
• Suppose DX contains FFFFh, AX contains
FFFBh, and BX contains 0002

W.P.Fang Department of CSIE 381


YUST, Copy Right Reserved
instruction Decimal Decimal AX DX
quotient remainder

DIV BX -2 -1 FFFE FFFF

IDIV BX DIVIDE
OVERFLOW

W.P.Fang Department of CSIE 382


YUST, Copy Right Reserved
ex
• Suppose AX contains 00FBh and BL
contains FFH

W.P.Fang Department of CSIE 383


YUST, Copy Right Reserved
instruction Decimal Decimal AX DX
quotient remainder

DIV BL 0 251 FB 00

IDIV BL DIVIDE
OVERFLOW

W.P.Fang Department of CSIE 384


YUST, Copy Right Reserved
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 385


YUST, Copy Right Reserved
ex
• Divide -1250 by 7

W.P.Fang Department of CSIE 386


YUST, Copy Right Reserved
• MOV AX,-1250
• CWD
• MOV BX,7
• IDIV BX

W.P.Fang Department of CSIE 387


YUST, Copy Right Reserved
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 388


YUST, Copy Right Reserved
ex
• Divide the signed value of the byte
variable XBYTE by -7

W.P.Fang Department of CSIE 389


YUST, Copy Right Reserved
• MOV AL,XBYTE
• CBW
• MOV BL,-7
• IDIV BL

W.P.Fang Department of CSIE 390


YUST, Copy Right Reserved
• There is no effect of CBW and CWD on
the flags

W.P.Fang Department of CSIE 391


YUST, Copy Right Reserved
Decimal input and output
procedures
• Decimal output
• Decimal input

W.P.Fang Department of CSIE 392


YUST, Copy Right Reserved
Decimal output
• IF AX<0
– THEN
• Print a minus sign
• Replase AX by its two’s complemet
• END_IF
• Get the digits in AX’s decimal
representation
• Convert these digits to characters and
print them
W.P.Fang Department of CSIE 393
YUST, Copy Right Reserved
Get the digits in AX’s decimal
representation
• Count=0
• ERPEAT
– Divide quotient by 10
– Push remainder on the stack
– Count=count+1
• UNTIL quotient=0

W.P.Fang Department of CSIE 394


YUST, Copy Right Reserved
Convert these digits to characters
and print them
• FOR count times DO
– Pop a digit from the stack
– Convert it to a character
– Output the character
• END_FOR

W.P.Fang Department of CSIE 395


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

W.P.Fang Department of CSIE 397


YUST, Copy Right Reserved
INCLUDE Pseudo-op
• INCLUDE filespec
• Ex:
– INCLUDE A:PGM9_1.asm

W.P.Fang Department of CSIE 398


YUST, Copy Right Reserved
• TITLE PGM9_2: DECIMAL OUTPUT
• .MODEL SMALL
• .STACK 100H
• .CODE
• MAIN PROC
– CALL OUTDEC
– MOV AH,4CH
– INT 21H
• MAIN ENDP
• INCLUDE A:PGM9_1.asm
– END MAIN
W.P.Fang Department of CSIE 399
YUST, Copy Right Reserved
• Debug pgm9_2.exe
• -rax
• AX 0000
• :9C71
• -g
• -25487
• Program terminated normally
• -rax
• AX 9C71
• :28E
• -g
• 654
W.P.Fang Department of CSIE 400
YUST, Copy Right Reserved
Decimal input
• Convert a string of ASCII digits to the
binary representation of a decimal integer

W.P.Fang Department of CSIE 401


YUST, Copy Right Reserved
• Print a question mark
• Total=0
• Negative =false
• Read a character
• CASE character OF
– ‘-’: negative=true
– Read character
– ‘+’:read a character
• END_CASE
• REPEAT
– IF character is not between ‘0’and ‘9’
• THEN
– Go to beginning
– ELSE
• Convert character to a binary value
• Total =10xtotal +value
– END_IF
– Read a character
• UNTIL character is a carriage return
• IF negatiev = true
– THEN
• Totla= -total
– ENDIF

W.P.Fang Department of CSIE 402


YUST, Copy Right Reserved
exercise
• A=5xA-7
• B=(A-B)x(B+10)
• A=6-9xA

W.P.Fang Department of CSIE 403


YUST, Copy Right Reserved
A=5xA-7
• MOV AX,5
• IMUL A
• SUB AX,7
• MOV A,AX

W.P.Fang Department of CSIE 404


YUST, Copy Right Reserved
B=(A-B)x(B+10)
• MOV AX,A
• SUB AX,B
• MOV BX,B
• ADD BX,10
• IMUL BX
• MOV B,AX

W.P.Fang Department of CSIE 405


YUST, Copy Right Reserved
A=6-9xA
• MOV AX,9
• IMUL A
• NEG AX
• ADD AX,6
• MOV A,AX

W.P.Fang Department of CSIE 406


YUST, Copy Right Reserved
– MOV AX,A
– IMUL AX
– MOV A,AX
– MOV AX,B
– IMUL AX
– MOV B,AX
– MOV AX,C
– MUL AX,AX
– SUB AX,A
• IF_:
– CMP AX,B
– JNE ELSE
• THEN:
– STC
– JMP EXIT
• ELSE:
– CLC
• EXIT:

W.P.Fang Department of CSIE 407


YUST, Copy Right Reserved
exercise
• IF A^2+B^2=C^2
• THEN
– SET CF
• ELSE
– Clear CF
• END_IF

W.P.Fang Department of CSIE 408


YUST, Copy Right Reserved
• MOV AX,A
• IMUL AX
• MOV A,AX
• MOV AX,B
• IMUL AX
• MOV B,AX
• MOV AX,C
• MUL AX,AX
• SUB AX,A
• IF_:
– CMP AX,B
– JNE ELSE
• THEN:
– STC
– JMP EXIT
• ELSE:
– CLC
• EXIT:

W.P.Fang Department of CSIE 409


YUST, Copy Right Reserved
Decimal Input
• Print a question mark
• Total=0
• Negative=false
• Read a character
• CASE character OF
– ‘-’:negative=true
• Read a character
– ‘+’:read a character
• END_CASE
• REPEAT
– If character is not between ‘0’and ‘9’
– THEN
• Go to beginning
– ELSE
• Convert character to a binary value
• Total =10xtotal+value
– END_IF
– Read a character
• UNTIL character is a carriage return
• IF negative =true
• THEN
• Total=-total
• END_IF

W.P.Fang Department of CSIE 410


YUST, Copy Right Reserved
PGM9_3.asm

W.P.Fang Department of CSIE 411


YUST, Copy Right Reserved
PGM9_4.asm

W.P.Fang Department of CSIE 412


YUST, Copy Right Reserved
HW
• Ch9.7,11

W.P.Fang Department of CSIE 413


YUST, Copy Right Reserved
Arrays and Addressing Modes

W.P.Fang Department of CSIE 414


YUST, Copy Right Reserved
One-dimensional arrays
• An ordered list of elements, all of the same
type.

W.P.Fang Department of CSIE 415


YUST, Copy Right Reserved
• MSG DB ‘abcde’
• W DW 10,20,30,40,50,60

W.P.Fang Department of CSIE 416


YUST, Copy Right Reserved
• Base address of the array

W.P.Fang Department of CSIE 417


YUST, Copy Right Reserved
DUP (duplicate)
• Define arrays whose elements share a
common initial value
• repeat_count DUP (value)

W.P.Fang Department of CSIE 418


YUST, Copy Right Reserved
ex
• GAMMA DW 100 DUP(0)
• DELTA DB 212 DUP(?)
• LINE DB 5,4, 3 DUP(2,3, DUP (0), 1)
• LINE DB 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1

W.P.Fang Department of CSIE 419


YUST, Copy Right Reserved
Location of array elements
• A=(N-1)xS
• S: number of bytes in an element
• A:array

W.P.Fang Department of CSIE 420


YUST, Copy Right Reserved
ex
• exchange the 10th and 25th elements in a
word array W.

W.P.Fang Department of CSIE 421


YUST, Copy Right Reserved
• MOV AX,W+18
• XCHG W+48,AX
• MOV W+18,AX

• Ps:
• W[10] is located at address W+9x2=18
• W[25] is located at address
W+24x2=W+48

W.P.Fang Department of CSIE 422


YUST, Copy Right Reserved
Address modes
• Register mode
• Immediate mode
• Direct mode

W.P.Fang Department of CSIE 423


YUST, Copy Right Reserved
Register indirect mode
• [register]
• The register is BX,SI,DI or BP
• Segment is contained in DS
• MOV AX,[SI] ; not same as MOV AX,SI
• (suppose SI contain 0100h))
• The CPU
– Examines SI and obtains the offset address 100h
– Use the address DS:0100h to obtain the value 1234h
– Move 1234h to AX
W.P.Fang Department of CSIE 424
YUST, Copy Right Reserved
ex
BX contains 1000h Offset 1000h contains 1BACh

SI contains 1000h Offset 2000h contains 20FEh

DI contains 1000h Offset 3000h contains 031Dh

Tell which of the following instruction are legal, give the source offset
address, and the result or number moves
a.MOV BX,[BX]
b.MOV CX,[SI]
c.MOV BX,[AX]
d.ADD [SI],[DI]
e.INC [DI]

W.P.Fang Department of CSIE 425


YUST, Copy Right Reserved
Source offset result

a 1000h 1BACh

b 2000h 20FEh

c Illegal source register (must be BX,SI


or DI)
d Illegal memory-memory
addition
e 3000h 031Eh
W.P.Fang Department of CSIE 426
YUST, Copy Right Reserved
ex
• Write some code to sum in AX the
elements of the 10-element array W define
by
• W DW 10,20,30,40,50,60,70,80,90,100

W.P.Fang Department of CSIE 427


YUST, Copy Right Reserved
– XOR AX,AX
– LEA SI,W
– MOV CX,10
• ADDNOS:
– ADD AX,[SI]
– ADD SI,2
– LOOP ADDNOS

W.P.Fang Department of CSIE 428


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

W.P.Fang Department of CSIE 429


YUST, Copy Right Reserved
• The idea is to exchange the 1st and N th
words, the 2nd and (N-1)st words, and so
on. The number of exchanges will be
N/2(rounded down to the nearest integer if
N is odd) .
• Ps:
– Nth element in a word array A has address
A+2x(N-1)

W.P.Fang Department of CSIE 430


YUST, Copy Right Reserved
REVERSE PROC
PUSH AX POP AX
PUSH BX RET
PUSH CX REVERSE ENDP
PUSH SI
PUSH DI
MOV DI,SI
MOV CX,BX
DEC BX
SHL BX,1
ADD DI,BX
SHR CX,1
XCHG_LOOP:
MOV AX,[SI]
XCHG AX,[DI]
MOV [SI],AX
ADD SI,2
SUB DI,2
LOOP XCHG_LOOP
POP DI
POP SI
POP CX
W.P.Fang Department of CSIE 431
POP BX YUST, Copy Right Reserved
Based and Indexed addressing
mode
• Displacement may be any of the following:
– The offset address of a variable
– A constant (positive or negative)
– The offset address of a variable plus or minus
a constant

W.P.Fang Department of CSIE 432


YUST, Copy Right Reserved
• Example of displacements
• (if A is a variable)
– A (offset address of a variable)
– -2 (constant)
– A+4 (offset address of a variable plus a
constant)

W.P.Fang Department of CSIE 433


YUST, Copy Right Reserved
• [register+displacement]
• [displacement+register]
• [register]+displacement
• Displacement+[register]
• Displacement[register]

W.P.Fang Department of CSIE 434


YUST, Copy Right Reserved
• The register must be BX,BP,SI or DI
• If BX,SI or DI is used, DS contains the segment
number of the operand’s address
• If BP is used,SS has the segment number.
• The addressing model is called based if BX
(base register) or BP(base pointer) is used
• The addressing model is called indexed if
SI(source index) or DI(destination index) is used

W.P.Fang Department of CSIE 435


YUST, Copy Right Reserved
ex
• MOV AX,W[BX]
• MOV AX,[W+BX]
• MOV AX,[BX+W]
• MOV AX,W+[BX]
• MOV AX,[BX]+W
• MOV AX,[SI+2]
• MOV AX,[2+SI]
• MOV AX,[SI}+2
• MOV AX,2[SI]

W.P.Fang Department of CSIE 436


YUST, Copy Right Reserved
ex
• Write some code to sum in AX the element
of 10-element array W defined by
• W DW 10,20,30,40,50,60,70,80,90,100

W.P.Fang Department of CSIE 437


YUST, Copy Right Reserved
– XOR AX,AX
– XOR BX,BX
– MOV CX,10
• ADDNOS:
– ADD AX,W[BX]
– ADD BX,2
– LOOP ADDNOS

W.P.Fang Department of CSIE 438


YUST, Copy Right Reserved
ex
• Suppose ALPHA is declared as
• ALPHA DW 0123h,0456h,0789h,0ABCDh
• In the segment address by DS, suppose that
• BX contains 2 offset 0002 contains 1084h
• SI contains 4 offset 0004 contains 2BACh
• DI contains 1
• Tell which of the following instruction are legal, if legal, give the source
offset address and the number moved
a. MOV AX,[ALPHA+BX]
b. MOV BX,[BX+2]
c. MOV CX,ALPHA[SI]
d. MOV AX,-2[SI]
e. MOV BX,[ALPHA+3+DI]
f. MOV AX,[BX]2
g. ADD BX,[ALPHA+AX]

W.P.Fang Department of CSIE 439


YUST, Copy Right Reserved
Source offset Number moved
A ALPHA +2 0456h
B 2+2=4 2BACh
C ALPHA+4 0789h
D -2+4=2 1084h
E ALPHA+3+1=ALPHA+4 0789h
F Illegal form of source
operand
g Illegal sourceW.P.Fang
register
Department of CSIE 440
YUST, Copy Right Reserved
ex
• Replace each lowercase letter in the
following string by its upper case
equivalent. Use index addressing mode
• MSG DB ‘this is a message’

W.P.Fang Department of CSIE 441


YUST, Copy Right Reserved
– MOV CX,17
– XOR SI,SI
• TOP:
– CMP MSG[SI],’‘
– JE NEXT
– AND MSG[SI],0DFh
• NEXT:
– INC SI
– LOOP TOP
W.P.Fang Department of CSIE 442
YUST, Copy Right Reserved
The PRT operator and LABEL
pseudo-op
• Operands of an instruction must be of the
same type
• If one operand is a constant, the
assembler attempts to infer the type from
the other operand

W.P.Fang Department of CSIE 443


YUST, Copy Right Reserved
• MOV [BX],1 ;illegal ,can’t assemble

• Shall be
• MOV BYTE PTR [BX],1
• MOV WORD PTR [BX],1

W.P.Fang Department of CSIE 444


YUST, Copy Right Reserved
ex
• LEA SI,MSG
• MOV BYTE PRT [SI],’T’

W.P.Fang Department of CSIE 445


YUST, Copy Right Reserved
ex
• Using index mode
– XOR SI,SI
– MOV MSG[SI],’T’
– Ps:here it is not necessary to use th PTR
operator, because MSG is a byte variable

W.P.Fang Department of CSIE 446


YUST, Copy Right Reserved
Use PTR to override a type
Type PTR address_expression
Ex:
DOLLARS DB 1Ah
CENTS DB 52h
MOV AX,DOLLARS ;illegal
MOV AX,WORD PRT
DOLLARS;AL=dollars,AH=cents

W.P.Fang Department of CSIE 447


YUST, Copy Right Reserved
The LABEL Pseudo-Op
• Declaration types MONEY as a word
variable, and the components DOLLARS
and CENTS as byte variables
– MONEY LABEL WORD
– DOLLAR DB 1Ah
– CENTS DB 52h

W.P.Fang Department of CSIE 448


YUST, Copy Right Reserved
• MOV AX,MONEY; AL=dollars,AH=cents

W.P.Fang Department of CSIE 449


YUST, Copy Right Reserved
Segment override
• Specify an relative to one of the other
segment registers
• Segment_register:[pointer_register]

W.P.Fang Department of CSIE 450


YUST, Copy Right Reserved
• MOV AX,ES:[SI]

W.P.Fang Department of CSIE 451


YUST, Copy Right Reserved
Accessing the stack
• Move the top three words on the stack into
AX,BX, and CX without changing the stack

W.P.Fang Department of CSIE 452


YUST, Copy Right Reserved
• MOV BP,SP
• MOV AX,[BP]
• MOV BX,[BP+2]
• MOV,[BP+4]

W.P.Fang Department of CSIE 453


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

W.P.Fang Department of CSIE 454


YUST, Copy Right Reserved
• PGM10_2.asm

W.P.Fang Department of CSIE 455


YUST, Copy Right Reserved
Two-dimensional arrays
• Memory is one-dimensional ,the elements
of a two-dimensional array must be stored
sequentially
• Two common used ways
– Row-major order
– Column-major order

W.P.Fang Department of CSIE 456


YUST, Copy Right Reserved
• B DW 10,20,30,40
• DW 50,60,70,80
• DW 90,100,110,120

W.P.Fang Department of CSIE 457


YUST, Copy Right Reserved
• MxN
– A[i][j] è A+((i-1)xN+(j-1))xS
– A[i][j] è A+((i-1)+(j-1)xM)xS

W.P.Fang Department of CSIE 458


YUST, Copy Right Reserved
Based indexed addressing mode
• Offset address of the operand is the sum
of
– The contents of a base register (BX or BP)
– The contents of an index register (SI or DI)
– Optionally, a variable’s offset address
– Optionally, a constant (positive or negative)

W.P.Fang Department of CSIE 459


YUST, Copy Right Reserved
• Variable[base_register][index_register]
• [base_register+index_register+variable+co
nstant]
• Variable[base_register+index_register+co
nstant]
• Constant[base_register+index_register+va
riable]

W.P.Fang Department of CSIE 460


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

W.P.Fang Department of CSIE 461


YUST, Copy Right Reserved
• (1)
– MOV X,28
– XOR SI,SI
– MOV CX,7
• CLEAR:
– MOV A[BX][SI],0
– ADD SI,2
– LOOP CLEAR

W.P.Fang Department of CSIE 462


YUST, Copy Right Reserved
• (2)
– MOV SI,6
– XOR BX,BX
– MOV CX,5
• CLEAR:
– MOV A[BX][SI],0
– ADD BX,1
– LOOP CLEAR

W.P.Fang Department of CSIE 463


YUST, Copy Right Reserved
The XFLAT instruction
• A no-operand instruction that can be used
to convert a byte value into another value
that comes from a table
• The byte to be convert must be in AL, and
BX has the offset address of the
conversion table

W.P.Fang Department of CSIE 464


YUST, Copy Right Reserved
• The instruction
– Add the contents of Al to the address n BX to
produce an address within the table
– Replaces the contents of AL by the value
found at that address

W.P.Fang Department of CSIE 465


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

W.P.Fang Department of CSIE 467


YUST, Copy Right Reserved
The string instructions
• Copy string
• Search a string for a particular byte or
word
• Store characters in a string
• Compare strings of characters
alphabetically

W.P.Fang Department of CSIE 468


YUST, Copy Right Reserved
Direction flag
• DF (direction flag)
– determine the direction in which string
operations will proceed.
– These operations are implemented by the two
index register SI and DI

W.P.Fang Department of CSIE 469


YUST, Copy Right Reserved
• STRING1 DB ‘ABCDE’

• If DF=0,SI and DI proceed in the direction


of increasing memory addresses from left
to right across the string.
• If DF=1, SI and DI proceed in the direction
of decreasing memory address: from right
to left
W.P.Fang Department of CSIE 470
YUST, Copy Right Reserved
CLD and STD
• CLD clear direction flag
• STD set direction flag

W.P.Fang Department of CSIE 471


YUST, Copy Right Reserved
Moving a string
• MOVSB
• Copies the contents of the byte addressed
by DS:SI, to the byte address by ES:DI

W.P.Fang Department of CSIE 472


YUST, Copy Right Reserved
ex
• .DATA
• STRING 1 DB ‘HELLO’
• STRING2 DB 5 DUP(?)
• MOV AX,@DATA
• MOV DS,AX
• MOV EX,AX
• LEA SI,STRING1
• LEA DI,STRING2
• CLD
MOVSB
• MOVSB

W.P.Fang Department of CSIE 473


YUST, Copy Right Reserved
Move entire string
• REP MOVSB

• Ex:
– CLD
– LEA SI,STRING1
– LEA DI,STRING2
– MOV CX,5
– REP MOVSB
W.P.Fang Department of CSIE 474
YUST, Copy Right Reserved
Ex:
• Write instructions to copy STRING1 into
STRING2 in reverse order

W.P.Fang Department of CSIE 475


YUST, Copy Right Reserved
– LEA SI,STRING1+4
– LEA DI,STRNIG2
– STD
– MOV CX,5
• MOVE:
– MOVSB
– ADD DI,2
– LOOP MOVE

W.P.Fang Department of CSIE 476


YUST, Copy Right Reserved
Move word string
• MOVSW
• Moves a word from the source string to the
destination string

W.P.Fang Department of CSIE 477


YUST, Copy Right Reserved
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 478


YUST, Copy Right Reserved
• 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 479


YUST, Copy Right Reserved
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 480


YUST, Copy Right Reserved
ex
• MOV AX,@DATA
• MOV ES,AX
• LEA DI,STRING1
• CLD
• MOV AL,’A’
• STOSB
• STOSB

W.P.Fang Department of CSIE 481


YUST, Copy Right Reserved
Reading and storing a character
string
• Use INT 21h, function 1 and STOSB

W.P.Fang Department of CSIE 482


YUST, Copy Right Reserved
• 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 483
YUST, Copy Right Reserved
• READ_STR PROC NEAR
– PUSH AX
– PUSH DI
– CLD
– XOR BX,BX
– MOV AH,1
– INT 21H
• WHILE1:
– CMP AL,0DH
– JE END_WHILE1
– CMP AL,8H
– JNE ELSE1
– DEC DI
– DEC BX
– JMP READ
• ELSE1:
– STOSB
– INC BX
• READ:
– INT 21H
– JMP WHILE1
• END_WHILE1:
– POP DI
– POP AX
– RET
• READ_STR ENDP

W.P.Fang Department of CSIE 484


YUST, Copy Right Reserved
Load string
• LODSB
– Moves the byte addressed by DS:SI into
AL,SI is then incremented if DF=0 or
decremented if DF=1
• LODSW
– Moves the byte addressed by DS:SI into
AX,SI is then incremented by 2 if DF=0 or
decremented by 2 if DF=1

W.P.Fang Department of CSIE 485


YUST, Copy Right Reserved
Displaying a character string
• FOR count times DO
– Load a string character into AL
– Move it to DL
– Output character
• END_FOR

W.P.Fang Department of CSIE 486


YUST, Copy Right Reserved
• Let Si =offset of string,BX=no. of chars. To
display
– MOV CX,BX
– JCXZ P_EXIT
– CLD
– MOV AH,2
• TOP:
– LODSB
– MOV DL,AL
– INT 21H
– LOOP TOP
– P_EXIT:
W.P.Fang Department of CSIE 487
YUST, Copy Right Reserved
Scan string
• SCANSB
• Examine a string for a target byte
• The target byte is contained in AL.
• SCANSB subtracts the string byte pointed
to by ES:DI from contents of AL and uses
the result to set the flags
• The result is not stored

W.P.Fang Department of CSIE 488


YUST, Copy Right Reserved
• SCANSW
– If not found ZF=0

W.P.Fang Department of CSIE 489


YUST, Copy Right Reserved
• REPNZ (repeat while not zero)
• REPNE (the same as above)
• Ex:
– REPNE SCANSB

W.P.Fang Department of CSIE 490


YUST, Copy Right Reserved
Ex:counting vowels and
consonants
• PGM11_4

W.P.Fang Department of CSIE 491


YUST, Copy Right Reserved
Compare string
• CMPSB
– Subtracts the byte with address ES:DI from
the byte with address DS:SI, and sets the
flags. the result is not stored
• CMPSW

W.P.Fang Department of CSIE 492


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

You might also like