• No results found

Assembly Language

N/A
N/A
Protected

Academic year: 2021

Share "Assembly Language"

Copied!
493
0
0

Loading.... (view fulltext now)

Full text

(1)

Assembly Language

(2)

Reference

• Ytha Yu, Charles Marut, Assembly

Language Programming and Organziation

of the IBM PC

(3)

requirement

• Exercise+ Program homework 40%

• Midterm examination 30%

• Final examination 30%

• Ps:

(4)

Outline

• Microcomputer Systems

• Representation of Numbers and

Characters

• Organization of the IBM Personal

Computers

• Introduction to IBM PC Assembly

Language

(5)

Outline

• Flow Control Instructions

• Logic, Shift, ad Rotate Instructions

• The Stack and Introduction to Procedures

• Multiplication and Division Instructions

• Arrays and Addressing Modes

• The String Instructions

(6)

Outline

• Macros

• Memory Management

• BIOS and DOS Interrupt

• Color Graphics

• Recursion

• Advanced Arithmetic

(7)

schdular

6

5

4

3

2

1

備註

進度

日期

(8)

schdular

15

14

13

12

11

10

備註

進度

日期

(9)
(10)

Microcomputer Systems

• Overview

– Introduction to the architeture of

microcomputers in general and to the IBM PC

in particular.

(11)

Microcomputer System

(component)

• The components o a microcomputer

system

– System unit

– Keyboard

– display screen

– Disk drives

(12)

Microcomputer System

(component)

• Computer circuits

– Central processing unit (CPU)

– I/O circuits

• Microcomputer

– CPU is a single-chip processor

(microprocessor)

(13)

Microcomputer System

(component)

• Bytes – eight bits

• Address – identified by a number

• Content– the data stored in a memory byte.

content

:

0 1 1 0 0 0 0 1

Address

:

3

2

(14)

Microcomputer System

(component)

• Example 1.1 Suppose a processor uses

20 bits for an address. How many memory

bytes can be accessed?

(15)

Microcomputer System

(component)

• Word – typical microcomputer, two bytes

form a word

• Bit position

Byte bit position

Word bit position

(16)

Microcomputer system

(component)

• RAM and ROM

• Bus

(17)

Microcomputer system

(component)

• In any case, each instruction that the CPU

executes is a bit string

• This language of 0’s and 1’s is called machine

language

• The instructions performed by a CPU are called

instruction set.

(18)

Microcomputer system

(component)

• EU (Execution Unit)

– Arithmetic

• +-*/

– Logic unit

• AND, OR, NOT

– Registers

(19)

Microcomputer system

(component)

• EIU ( Bus Interface Unit)

– Communication between the EU and the

memory or I/O circuits

– Registers

(20)

Microcomputer system

(component)

(21)

Microcomputer system

(instruction execution)

• Instruction Execution

• Machine instruction

– Opcode : the type of operation

– Operand : given as memory address to he

data to be operated on.

(22)

Microcomputer system

(instruction execution)

• Fetch-execute cycle

– Fetch

• Fetch an instruction from memory

• Decde he inctruction to determine the operation

• Fetch data from memory if necessary.

– Execute

(23)

Microcomputer system

(I/O Devices)

• Magnetics disks

• Keyboard

• Display monitor

• printers

(24)

Microcomputer system

(programming languages)

• Machine language

Add 4 to AX

00000101 00000100 00000000

Fetch the content of memory word

to and put it in register AX

10100001 00000000 00000000

operation

Machine instruction

(25)

Microcomputer system

(programming languages)

• Assembly language

Add 4 to AX

ADD AX,4

Fetch the content of location A and

put it in register AX

MOV AX,A

operation

Assembly language

(26)

Microcomputer system

(programming languages)

• Assembler

• compiler

(27)

Microcomputer system

(programming languages)

• Advantage of high-level languages

– Closer to natural language

– Reduce Coding time

– Executed on any machine

• Advantage of assembly language

– Efficiency

(28)

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

(29)

Microcomputer system

(Debug)

• assemble A [address]

• compare C range address

• dump D [range]

• enter E address [list]

• fill F range list

• go G [=address] [addresses]

• hex H value1 value2

• input I port

• load L [address] [drive] [firstsector] [number]

• move M range address

(30)

Microcomputer system

(Debug)

• proceed P [=address] [number]

• quit Q

• register R [register]

• search S range list

• trace T [=address] [value]

• unassemble U [range]

• write W [address] [drive] [firstsector] [number]

• allocate expanded memory XA [#pages]

• deallocate expanded memory XD [handle]

• map expanded memory pages XM [Lpage] [Ppage] [handle]

• display expanded memory status XS

(31)

Microcomputer system

(tasm)

• Syntax: TASM [options] source [,object] [,listing] [,xref] • /a,/s Alphabetic or Source-code segment ordering • /c Generate cross-reference in listing

• /dSYM[=VAL] Define symbol SYM = 0, or = value VAL • /e,/r Emulated or Real floating-point instructions • /h,/? Display this help screen

• /iPATH Search PATH for include files

• /jCMD Jam in an assembler directive CMD (eg. /jIDEAL) • /kh# Hash table capacity # symbols

• /l,/la Generate listing: l=normal listing, la=expanded listing

• /ml,/mx,/mu Case sensitivity on symbols: ml=all, mx=globals, mu=none • /mv# Set maximum valid length for symbols

• /m# Allow # multiple passes to resolve forward references • /n Suppress symbol tables in listing

• /os,/o,/op,/oi Object code: standard, standard w/overlays, Phar Lap, or IBM • /p Check for code segment overrides in protected mode

• /q Suppress OBJ records not needed for linking • /t Suppress messages if successful assembly

(32)

Microcomputer system

(tlink)

Turbo Link Version 3.01 Copyright (c) 1987, 1990 Borland International

Syntax: TLINK objfiles, exefile, mapfile, libfiles

@xxxx indicates use response file xxxx

Options: /m = map file with publics

/x = no map file at all

/i = initialize all segments

/l = include source line numbers

/s = detailed map of segments

/n = no default libraries

/d = warn if duplicate symbols in libraries

/c = lower case significant in symbols

/3 = enable 32-bit processing

/v = include full symbolic debug information

/e = ignore Extended Dictionary

(33)

Glossary

• Add-in board or card

• Address

• Address bus

• Arithmetic and logic unit, ALU

• Assembler

(34)

Glossary

• Bus

• Bus interface unit, BIU

• Byte

• Central processing unit, CPU

• Clock period

• Clock pulse

• Clock rate

(35)

Glossary

• Compiler

• Contents

• Control bus

• Data bus

• Digital circuits disk drive

• Execution unit ,EU

(36)

Glossary

• Firmware

• Fixed disk

• Floppy diskhardcopy

• Hard disk

• I/O devices

• I/O ports

• Intruction pointer,IP

• Instruction set

(37)

Glossary

• Machine language

• Mega

• Megabyte, MB

• Megahertz, MHz

• Memory byte (circuit)

• Memory location

(38)

Glossary

• Motherboard

• Opcode

• Operand

• Peripheral (device)

• Random access memory

• RAM

(39)

Glossary

• System board

• Video adapter

• word

(40)

Representation of numbers

and characters

(41)

Representation of numbers and

characters

(42)
(43)

• Ex:

• 8A2Dh=?

• 11101=?

• 2BD4h=?

• Convert 95 to binary

• Convert 2B3Ch to binary

(44)

• Add

• Subtraction

• Find one’s complement of 5 (16 bits)

• Find the two’s complement of 5

(45)

ASCII code

• American Standard Code for Information

Interchange

(46)

Homework

• Chap 2

– 2.

– 7.d

– 8.c

(47)

Representation of numbers and

characters

• Number system

– Binary

– Hexadecimal

• Conversion

• Addition and subtraction

• Character representation

(48)

Representation of numbers and

characters

(Glossary)

• ASCII (American Standard Code for

Information Interchange) code

• Binary number system

• Hexadecimal number system

• Least significant bit lsb

• most significant bit msb

(49)

Representation of numbers and

characters

(Glossary)

• Signed integer

• Two’s complement of a binary number

• Unsigned integer

(50)

Organization of the IBM

Personal Computers

(51)

Organization of the IBM Personal

Computers

• Overall structure of the IBM PC

– The memory organization

– I/O ports

– DOS routines

– BIOS routines

(52)

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

(53)

Register

• Data register

• Address register

• Status register

• Segment

• Pointer

• Index register

(54)

• Ax

• Bx

• Cx

• Dx

• Cs

• Ds

• Ss

• Es

• Si

• Di

• Sp

• Bp

(55)

Data register

• AX(accumulator)

• BX(base register)

• CX(count register)

• DX(data register)

(56)

Segment register

• CS -- code segment

• DS – data segment

• SS – stack segment

• ES – access second data segment (extra

segment)

(57)

• Memory segment

• Segment:offset address

(58)

Logical address

• Ex:

• A4FB:4872h

• A4FB0+4872h=A9822h(20-bit physical

address)

(59)

• For the memory location whose physical

address is specified by 1256Ah,give the

address in segment:offset form for

(60)

• 1256Ah=12560h+X

• 1256Ah=12400h+Y

• X=Ah,Y=16Ah

(61)

• A memory location has physical address

80FD2h. In what segment does it have

offset BFD2h

(62)

• Physical address=segmentx10h+offset

• Segmentx10h=physical address-offset

• 80FD2h-BFD2h=75000h

(63)

Pointer and index register

• SP – stack pointer

– Used in conjunction with SS for accessing

stack segment.

• BP – Base pointer

– Used primarily to access data on the stack.

– Unlike SP, we can also use BP to access data

in the other segment

(64)

• SI – Source Index

– Point to memory locations in the data

segment addressed by DS.

– By incrementing the contents of SI, we can

easily access consecutive memory locations

• DI – Destination Index

– Same functions as SI

– For string operation

(65)

Instruction Pointer

• IP

– Use register CS ad IP to access instruction

– CS à segment number of next instruction

– IP à offset

– Can not be directly manipulated by an

instruction

(66)

Flags

• Status flags

– Ex:ZF(zero flag)

• Controls

– Ex: IF(interrupt flag) =0

à input from the

keyboard are ignored by the processor.

(67)

The operating system

• Reading and executing the commands

typed by the user

• Performing I/O operations

• Generating error messages

(68)

• Dos routine that services user commands

is called COMMAND.COM

(69)

BIOS

• Machine specific

• DOS I/O operations are ultimately carried

out by the BIOS routines

• Addresses of the BIOS routine – interrupt

vectors

(70)

Memory

Application program

area

A0000h

Video

B0000h

Video

C0000h

Reserved

D0000h

Reserved

E0000h

Reserved

F0000h

Bios

(71)

EGA

3c0h-3cfh

Parallel printer port 1

378h-37fh

Hard disk

320h-32fh

Serial port (COM2)

2f8h-2ffh

Game controller

200h-20fh

Keyboard controller

60h-63h

Interrupt controller

20h-21h

description

Port address

(72)

Start-up operation

• Power up

• CS register is set to FFFFh

• IP is set to 0000h

à FFFF0h (in ROM)

• First check for system and memory errors

• Initialize the interrupt vectors and BIOS

(73)

Introduction to IBM PC

Assembly Language

(74)

Introduction to IBM PC Assembly

Language

• Syntax

– Name operation operand(s) comment

• Ex:

– START: MOV CX,5 ;initialize counter

– MAIN PROC

(75)

• Name Field

– 1 to 31 characters long

– Letters, digits, special characters ?.@_$%

– Embeded blanks are not allowed

(76)

Operation feilds

• Operands field

– An instruction may have zero, one , or two

operand

– Ex:

– NOP

– INC AX

(77)

Comment fields

• A semicolon marks the beginning of this

field

• The assembler ignores anything typed

after the semicolon.

• Ex:

(78)

Program data

• The processor operates only on binary

data

• The assembler must translate all data

representation into binary number

(79)

Illegal hex number – doesn’t end in

“H”

1B4D

Hex

1B4DH

Illegal– contains a nondigit character

1,234

Decimal

-21843D

Decimal

64223

Binary

11011B

Decimal

11011

TYPE

NUMBER

(80)

characters

Define quaword

DQ

Define double word

DD

Define word

DW

Define byte

DB

Stands for

Pseudo-op

(81)

• Byte variable

– Name DB initial_value

• Ex:

(82)

• Word variable

– Name DW initial_value

• Ex:

(83)

• Array

– B_ARRAY DB 10H,20H,30H

• String

– LETTERS DB ‘ABC’

– LETTERS DB 41H,42H,43H

– MSG DB ‘HELLO’,0AH,0DH,’$’

(84)

• Named Constanted

– EQU (Equates)

– Name EQU constant

• Ex:

– LF EQU 0AH

(85)

variables

• Byte variables

• Name DB initial_value

• Ex:

• ALPHA DB

• BYT DB ?

(86)

Word variables

• Name DW initial_value

• Ex:

(87)

array

• B_ARRAY DB

10H,20H,30H

20h

201h

B_ARRAY+1

10h

200h

B_ARRAY

contents

Address

Symbol

(88)

Character string

• LETTERS DB ‘ABC’

• LETTERS DB 41H,42H,43H

• MSG DB ‘HELLO’,0AH,0DH,’$’

• MSG DB

48H,45H,4CH,4CH,4FH,0AH,0DH,24H

(89)

Names constants

• Name EQU constant

• Ex:

– LF EQU 0AH

– MOV DL,0AH

– MOV DL,LF

– Prompt EQU ‘TYPE YOUR NAME’

– MSG DB PROMPT

(90)

A few basic instructions

• MOV

• XCHG

• ADD

• SUB

• INC

• DEC

(91)

• MOV

destination,source

No

Yes

Yes

yes

General

register

constant

Memory

location

Segment

register

General

register

Destination

operand

Source

operand

(92)

• XCHG destination,

source

Yes

Yes

Generation

Memory

location

General

register

Destination

operand

Source

operand

(93)

• XCHG AH,BL

• XCHG AX,WORD1

1A

00

AH AL

05

00

AH AL

(94)

Restrictions on MOV and XCHG

• MOV or XCHG between memory location

is not allowed

• Ex: MOV WORD1,WORD2 ; illegal

• MOV AX,WORD2

(95)

• ADD destination, source

• SUB destination, source

Yes

Yes

General register

Memory location

General register

Destination

operand

Source operand

(96)

• INC destination

• DEC destination

• Ex:

– INC WORD1

– DEC BYTE1

(97)

• NEG destination

• By two’s complement

• Ex:

(98)

Type agreement of operands

• The operands of the preceding

two-operand instruction must be of the same

type.

(99)

Translation of High-Level language

to assembly labguage

• B=A

– MOV AX,A

– MOV B,AX

• A=5-A

– MOV AX,5

– SUB AX,A

– MOV A,AX

(100)

• A=B-2*A

– MOV AX,B

– SUB AX,A

– SUB AX,A

– MOV A,AX

(101)
(102)

Program structure

(Memory model)

Code in more than one segment

Data in more than one segment

No array larger than 64k bytes

LARGE

Code one segment

Data in more than one segment

COMPACT

Code in more than one segment

Data in one segment

MEDIUM

Code in one segment

Data in one segment

SMALL

Description

Model

(103)

Data segment

• Contains all the variable definitions

• .DATA

• WORD1 DW2

• WORD2 DW 5

• MSG DB ‘THIS IS A MESSAGE’

• MASK EQU 10010010B

(104)

Stack segment

• .STACK size

• Ex:

– .STACK 100H

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

the stack data

(105)

Code segment

• .CODE name

• There is no need for a name in a SMALL

program

• Name PROC

(106)

• Ex:

– .CODE

– MAIN PROC

– ;main procedure instructions

– MAIN ENDP

(107)

Putting it together

• .MODEL SMALL

• .STACK 100H

• .DATA

• ;data definitions go here

• .CODE

• MAIN PROC

• ;instructions go here

• MAIN ENDP

(108)

Input and output instructions

• IN

• OUT

• Most applications programs do not use IN

and OUT because

– Port addresses vary among computer model

– It’s much easier to program I/O with the

(109)

INT instruction

(110)

INT 21h

Single-character output

2

Single-key input

1

routine

Function number

(111)

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

(112)

• MOV AH,1

• INT 21h

(113)

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:

(114)

• MOV Ah,2

• MOV DL,’?’

• INT 21h

(115)

Tab

HT

9

Backspace

BS

8

Beep (sounds a

tone)

BEL

7

Function

Symbol

ASCII code

(HEX)

(116)

INT 21h

Function 9:

• Display a string:

– DX=offset address of string

(117)

• Ex:

(118)

LEA

• INT21h, function 9, expects the offset

address of the character string to be DX

• LEA destination, source

• EX:

(119)

Program segment prefix

• When a program is loaded in memory,

DOS prefaces it with a 256 byte program

segment prefix(PSP)

• PSP contains information about the

program,

• DOS places its segment number in both

DS and ES before executing the program

(120)

• MOV AX,@DATA

• MOV DS,AX

(121)

Terminating a program

(122)

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

(123)

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

(124)

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

(125)

MOV CHAR,AL

LEA DX,MSG2

MOV AH,9

INT 21h

MOV AH,4CH

INT 21h

MAIN ENDP

END MAIN

(126)

Exercise

• Using only MOV,ADD,SUB,INC,DEC and

NEG translate below into assembly (A,B,C

are word)

• A=B-A

• A=-(A+1)

• C=A+B

(127)

homework

• Ch4

– 4

– 8

– 12

• If programming exercise please print out

code and result (hard copy)

(128)

• Write a program to

– Display a “?”

– Read two decimal digits whose sum is less

than 10

– Display them and their sum on the next line,

with an appropriate message

(129)

• Write a program to

– Display “?”

– Read three initials

– Display them in the middle of an 11x11 box of

asterisks

(130)

The processor status and the

FLAGS Register

CF

0

PF

AF

ZF

SF

TF

IF

DF

OF

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

(131)

• The status flags are located in bits 0,and

11

• Control flags are located in bits 8,9, and

10

(132)

Status flags

• Reflect the result of an operation

• Ex:

– If SUB AX,AX is executed, the zero flag

become 1

(133)

Status Flags

ZF

Zero flag

6

AF

Auxiliary carry

flag

4

PF

Parity flag

2

CF

Carry flag

0

symbol

name

bit

(134)

Control Flags

IF

Interrupt flag

9

TF

Trap flag

8

Symbol

name

bit

(135)

Carry flag

• CF=1 if

– There is a carry out from the most significant

bit (msb) on addition

– There is a borrow into themsb on subtraction

– Affected by shift and rotate instruction

(136)

Parity flags

• PF=1 if

– The low byte of a result has an even number

of one bits

(137)

AuxiliRy carry flag

• AF=1 if

– There is a carry out from bit 3 on addition

– Borrow into bit 3 on substraction

– Used in binary-coded decimal (BCD)

operations

(138)

Zero flag

• ZF=1 for a zero result

(139)

Sign Flag

• SF=1 if the msb of a result is 1

• Means the result is negative if giving a

signed interpretation

(140)

Overflow Flag

(141)

Overflow

• Is associated with the fact that the range

of numbers that be represented in a

(142)

Overflow examples

• Four possible outcome

– No overflow

– Signed overflow

– Unsigned overflow

(143)

• Unsigned overflow but not signed overflow

– AX=FFFFh

– BX=0001h

– ADD AX,BX

1111 1111 1111 1111

+ 0000 0000 0000 0001

(144)

---• Signed but not unsigned overflow

– AX=BX=7FFFh

– ADD AX,BX

0111 1111 1111 1111

+ 0111 1111 1111 1111

---1111 ---1111 ---1111 1110

(145)

How the processor indicates

overflow

• OF=1 for signed overflow

• CF=1 for unsigned overflow

• In determining overflow, the processor

does not interpret the result as either

signed or unsigned.

(146)

How the processor determines that

overflow occurred

• Limit the discussion to addition and

substraction

• Unsigned overflow

– On addition,Means that the correct answer is

larger than the biggest unsigned number

– On substraction, means that the correct

answer is smaller than 0

(147)

• Signed overflow

– On addition

• Signed overflow occurs when the sum has a different sign

– On substraction

• With different signed like adding number of the same sign.

• Ex:

• A-(-B)=A+B

• -A-(+B)=-A+ -B

(148)

• In addition of number with different signs,

overflow is impossible

• In substraction of numbers with the same

sign cannot give overflow

(149)

• Processor uses the following method to

set the OF

– If the carries into and out of the msb don’t

match

– There is a carry out but no carry in

– Then signed overflow has occurred

à OF is

set to 1

(150)

How Instructions affect the flags

All except CF

INC/DEC

All

ADD/SUB

None

MOV/XCHG

Affects flags

instruction

(151)

example

• ADD AX,BX, where AX contains

FFFFh,BX contains FFFh

(152)

example

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

contains 80h

(153)

example

• SUB AX,BX, where AX contains 8000h

and BX contains 0001h

(154)

example

(155)

example

(156)

example

(157)

The DEBUG Program

• An environment in which a program may

be tested.

(158)

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

(159)

-r

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC

0B8E:0000 B80040 MOV

AX,4000

-r

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0000 NV UP EI PL NZ NA PO NC

0B8E:0000 B80040 MOV

AX,4000

-t

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0003 NV UP EI PL NZ NA PO NC

0B8E:0003 03C0 ADD

AX,AX

(160)

-t

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0005 OV UP EI NG NZ NA PE NC

0B8E:0005 2DFFFF SUB

AX,FFFF

-t

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=0008 NV UP EI NG NZ AC PO CY

0B8E:0008 F7D8 NEG

AX

-t

(161)

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

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

0B8E:000A 40 INC AX

-t

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

DS=0B7E ES=0B7E SS=0B8F CS=0B8E IP=000B OV UP EI NG NZ AC PE CY

0B8E:000B B44C MOV

AH,4C

-g

Program terminated normally

-q

(162)

homework

• Ch5

– 1.e

– 2.b

– 3.d

– 4.e

(163)
(164)

Flow Control Instructions

• The jump and loop instructions transfer

control to another part of the program.

• This transfer can be

– Unconditional

– Depend on a particular combination of status

flag settings

(165)

Jump example

TITLE PGM6_1:IBM CHARACTER DISPLAY

.MODEL

SMALL

.STACK

100H

.CODE

MAIN

PROC

MOV

AH,2

MOV

CX,256

MOV

DL,0

PRINT_LOOP:

int

21h

INC

DL

DEC

CX

JNE

PRINT_LOOP

MOV

AH,4CH

(166)
(167)

• JNZ:jump if not zero

• CX:loop counter

(168)

Conditional jump

(169)

Conditional jumps

• Range of a conditional jump

– Precede no more than 126 bytes

(170)

How the CPU implements a

conditional jump

• CPU looks at the flags register

• If the conditions for the jump are true, the

CPU adjusts the IP to point to the

destination label.

• If the condition is false, the IP is not

altered

(171)

• JNZ

– Inspecting ZF

• If ZF=0,transfer to label

• If ZF=1, next line

(172)

Conditional jumps

• Three categories

– The signed jumps

– Unsigned jumps

– Single-flag jumps

(173)

Signed jumps

SF<>OF

Jump if less than

JL/JNGE

SF=OF

Jump if greater than or

equal to

Jump if not less than or

equal to

JGE/JNL

ZF=0 and SF=OF

Jump if greater than

Jump if not less than or

equal to

JG/JNLE

Condition for jumps

description

(174)

Unsigned conditional jumps

CF=1

Jump if below

Jump if not above or

JB/JNAE

CF=0

Jump if above or equal

Jump if not below

JAE/JNB

CF=0 and ZF=0

Jump if above

Jump if not below or

equal

JA/JNBE

Condition for jumps

description

(175)

Single-Flag jumps

OF=0

Jump if no overflow

JNO

OF=1

Jump if overflow

JO

CF=0

Jump if no carry

JNC

CF=1

Jump if carry

JC

ZF=0

jump if not equal

Jump if not zero

JNE/JNZ

ZF=1

Jump if equal

Jump if equal to zero

JE/JZ

Condition for jumps

description

(176)

The CMP instruction

• CMP destination , source

• Compares destination and source by

computing (destination)-(source)

• the result is not stored, but flags are

affected

(177)

• CMP AX,BX

• JG BELOW

• Where AX=7,BX=0001

• Result is 7FFFh-0001=7FFEh

• ZF=SF=OF=0

(178)

• DEC AX

(179)

Signed versus unsigned jumps

• Each of the signed jumps corresponds to

an analogous unsigned jump

• Using the wrong kind of jump can lead to

incorrect results

(180)

• Ex:

– If AX=7FFFh

– BX=8000h

– CMP AX,BX

– JA BELOW

• Because 7FFFh<8000h in an unsigned

sense àdoes not jump to BELOW

(181)

Working with Characters

• Example. Suppose AX and BX contain

signed numbers, write some code to put

the biggest one in CX

• Solution:

– MOV CX,AX

– CMP BX,CX

– JLE NEXT

(182)

The JMP instruction

• JMP destination

• JMP can be used to get around the range

restriction of a conditional jump

(183)

• TOP:

• <<if too far>>

• DEC CX

• JNZ TOP

(184)

• TOP:

– DEC CX

– JNZ BOTTOM

– JMP EXIT

• BOTTOM:

– JMP TOP

• EXIT:

(185)

High-Level Language structure

• IF-THEN

• IF-THEN-ELSE

• CASE

• Branch with compound conditions

• AND conditions

(186)

IF-THEN

• IF condition is true

• THEN

– Execute true-branch statements

(187)

• Ex :replace the number in AX by its

absolute value

(188)

• IF AX<0

• THEN

– Replace AX by –AX

(189)

• CMP AX,0

– JNL END_IF

– NEG AX

(190)

IF-THEN-ELSE

• IF condition is true

– THEN

• Execute true-branch statements

– ELSE

• Execute false-branch statements

(191)

• Ex: suppose AL and BL contain extended

ASCII characters. Display the one the

(192)

• IF AL<=BL

– THEN

• Display the character in AL

– ELSE

• Display the character in BL

(193)

– MOV AH,2

– CMP AL,BL

– JNBE ELSE_

– MOV DL,AL

– JMP DISPLAY

• ELSE_:

– MOV DL,BL

• DISPLAY:

– INT 21h

• END_IF

(194)

Exercise

• Write assembly code for each of the following decision

structures

• a.

– IF AX<0

– THEN

• PUT -1 IN BX

– END_IF

• b.

• IF AL<0

– THEN put FFh in AH

• Put FFh in AH

(195)

Exercise

• C.

• Suppose DL contains the ASCII code oa a

character

• (IF DL>=“A”) AND (DL<= ‘Z’)

• THEN

(196)

• D.

– If AX<BX

• THEN

– IF BX<CX

» THEN

» Put 0 in AX

– ELSE

» Put 0 in BX

(197)

• E

• IF(AX<BX) OR (BX<CX)

– THEN

• Put 0 in BX

– ELSE

• Put 1 in DX

– END_IF

(198)

HW

(199)

CASE

• Multiway branch structure

• CASE expression

– Value1:statement 1

– Value2:statement 2

– :

(200)

ex

• If AX contains a negative number, put -1 in

BX;if AX contains 0, put 0 in BX;if AX

References

Related documents