Program Control Instructions
Microprocessors (66322) 1 Dr. Aladdin Masri
Read / Write Character using DOS
interrupt 21H
• To read or write a character, use the DOS interrupt 21H: INT 21H
• The interrupt uses function code depending on the value of AH to
specify its action:
• 01H: input character from keyboard
– Value is stored in AL
• 02H: output a character on screen
– Value found in DL
• 06H: input character from keyboard without echo (wait)
– Value in DL must equal to FFH
– Value is stored in AL
• 06H: output a character on screen
– Value found in DL but must not equal to FFH
• 09H: output a $-terminated string
– String address must be in DS:DX
Microprocessors (66322) 2 Dr. Aladdin Masri
Unconditional Jump (JMP)
• The jump instructions allow to:
– Skip program sections
– Branch to any part of memory for the next instruction
• Short jump allows jumps or branches to memory locations
within +127 and –128 bytes.
– from the address following the jump
• Near jump allows a branch or jump within ±32K bytes from
the instruction in the current code segment.
– The short and near jumps are often called intra-segment jumps.
– Both are also relative jumps
• Far jump allows a jump to any memory location within the
real memory system.
– Far jumps are called intersegment jumps.
Microprocessors (66322) 3 Dr. Aladdin Masri
The main forms of the JMP instruction
Microprocessors (66322) 4 Dr. Aladdin Masri
Short Jump
• When a jump references an address, a label normally identifies the address.
• Very rare to use an actual hexadecimal address with any jump instruction
• The JMP NEXT instruction is an example.
– It jumps to label NEXT for the next instruction
– The label must be followed by a colon (NEXT:) to allow an instruction to
reference it
– If a colon does not follow, we cannot jump to it
0000 XOR BX,BX
0002 START: MOV AX,1
0005 ADD AX,BX
0007 JMP NEXT
<skipped memory locations>
0020 NEXT: MOV BX,AX
0022 JMP START
Microprocessors (66322) 5 Dr. Aladdin Masri
Near Jump
• Signed displacement adds to the instruction pointer (IP) to
generate the jump address.
• Here, the jump is near jump
0000 XOR BX,BX
0002 START: MOV AX,1
0005 ADD AX,BX
0007 JMP NEXT
<skipped memory locations>
0200 NEXT: MOV BX,AX
0202 JMP START
Microprocessors (66322) 6 Dr. Aladdin Masri
Far Jump
• Obtains a new segment
and offset address to
accomplish the jump
• Example: Replaces the
contents of both CS
and IP
– CS = A300H
– Offset = 0127H
Microprocessors (66322) 7 Dr. Aladdin Masri
Far Jump
• To transfer control to a label outside the
current procedure, the label name must be
declared global.
• To define a label as global is to use:
1. A double colon (label::)
2. By the EXTRN label:FAR directive
• The far jump instruction sometimes appears
with the FAR PTR directive
Microprocessors (66322) 8 Dr. Aladdin Masri
Jumps with Register Operands
Indirect Jump
• Jump can also use a 16- or 32-bit register or a memory
pointer as an operand.
• JMP AX, for example, copies the contents of the AX
register into the IP.
– Allows a jump to any location within the current code
segment
• Example:
JMP NEAR PTR[DX]
JMP FAR PTR[ECX]
• A jump table can contain offset addresses for near
indirect jumps, or segment and offset addresses for far
indirect jumps
Microprocessors (66322) 9 Dr. Aladdin Masri
Jumps with Register Operands
Example
• An example to that read 1, 2, or 3 from the keyboard, then displayed
as 1, 2, or 3 using a jump table. Using DOS interrupt.
.MODEL SMALL ;select SMALL model
.DATA ;start data segment
TABLE DW ONE ;jump table (labels)
DW TWO
DW THREE
.CODE ;start code segment
TOP: MOV AH,1 ;read key into AL
INT 21H
SUB AL,31H ;convert to 00, 01 or 02
JB TOP ;if key < 1
CMP AL,2
JA TOP ;if key > 3
Microprocessors (66322) 10 Dr. Aladdin Masri
Jumps with Register Operands
Example
MOV AH,0 ;double key code
ADD AX,AX
MOV SI,OFFSET TABLE ;address TABLE
ADD SI,AX ;form lookup address
MOV AX,[SI]
JMP AX ;jump to ONE, TWO or THREE
ONE: MOV DL,’1’ ;get ASCII 1
JMP BOT
TWO: MOV DL,’2’ ;get ASCII 2
JMP BOT
THREE: MOV DL,’3’ ;get ASCII 3
BOT: MOV AH,2 ;display number
INT 21H
.EXIT ; returns to DOS
END
Microprocessors (66322) 11 Dr. Aladdin Masri
Indirect Jumps Using an Index
Example
• Same example to that read 1, 2, or 3 from the keyboard, then
displayed as 1, 2, or 3 using a jump table. Using DOS interrupt.
.MODEL SMALL ;select SMALL model
.DATA ;start data segment
TABLE DW ONE ;jump table (labels)
DW TWO
DW THREE
.CODE ;start code segment
TOP: MOV AH,1 ;read key into AL
INT 21H
SUB AL,31H ;convert to BCD
JB TOP ;if key < 1
CMP AL,2
JA TOP ;if key > 3
Microprocessors (66322) 12 Dr. Aladdin Masri
Indirect Jumps Using an Index
Example
MOV AH,0 ;double key code
ADD AX,AX
MOV SI,AX ;form lookup address
JMP TABLE[SI] ;jump to ONE, TWO or THREE
ONE: MOV DL,’1’ ;get ASCII 1
JMP BOT
TWO: MOV DL,’2’ ;get ASCII 2
JMP BOT
THREE: MOV DL,’3’ ;get ASCII 3
BOT: MOV AH,2 ;display number
INT 21H
.EXIT ; returns to DOS
END
Microprocessors (66322) 13 Dr. Aladdin Masri
Conditional Jumps and Conditional
Sets
• Conditional jump instructions test flag bits:
– sign (S), zero (Z), carry (C), parity (P), overflow (O)
• If the condition under test is true, a branch to the label
associated with the jump instruction occurs.
• If false, next sequential step in program executes
– For example, a JC will jump if the carry bit is set
• Most conditional jump instructions are straightforward as
they often test one flag bit although some test more than one
• Two sets of conditional jump instructions for magnitude
comparisons, because:
1. Both signed and unsigned numbers are used in
programming
2. The order of these numbers is different
Microprocessors (66322) 14 Dr. Aladdin Masri
Conditional Jumps and Conditional
Sets
• When signed numbers are compared, use the JG, JL,
JGE, JLE, JE, and JNE instructions.
– Terms greater and less refer to signed numbers
• When unsigned numbers are compared, use the JA, JB,
JAE, JBE, JE, and JNE instructions.
– Terms above and below refer to unsigned numbers
• Remaining conditional jumps test individual flag bits,
such as overflow and parity.
• All instructions have alternates, but many are not used
in programming because they do not usually fit the
condition under test.
– JE has an alternative opcode JZ
Microprocessors (66322) 15 Dr. Aladdin Masri
Conditional Jumps and Conditional
Sets
Microprocessors (66322) 16 Dr. Aladdin Masri
Example 1
• Write an assembly code using DOS model program that keep reading a key and displays it.
However, an ‘@’ key ends the program.
• Solution:
.MODEL SMALL
.CODE
MAIN:
MOV AH,6 ;read a key without waiting (defers from AH = 1)
MOV DL,0FFH
INT 21H
JE MAIN ;if no key typed
CMP AL, '@'
JE MAIN1 ;if an @ key
MOV AH,06H ;display key (echo)
MOV DL,AL
INT 21H
JMP MAIN ;repeat / infinite loop
MAIN1:
.EXIT ;exit to DOS
END
Microprocessors (66322) 17 Dr. Aladdin Masri
Example 2
• Write an assembly program that keeps reading characters from the keyboard. It
exits if the user enters the character '*' or the number of entered characters are 50.
Your program should do the following:
1. If the entered character is a digit, then display the letter ‘D’.
2. If it is a letter (capital or small), then display the letter ‘L’
3. Otherwise, display the Letter ‘C’
• Example: if you entered f, 9, F, #, 5, @, Q, *, your program display: L D L C D C L
• Solution:
.MODEL SMALL
.CODE
MOV Cl, 0 ; reset counter to 0
START:
MOV AH, 1 ; read a key with wait
INT 21H
CMP AL, '*' ; end program
JE L10
CMP AL, '0' ; compare for digits
JAE L1
Microprocessors (66322) 18 Dr. Aladdin Masri
Example 2
MOV DL, 'C' ; less than ‘0’ then it’s not digit nor letter (ASCII code)
JMP WRITE
L1:
CMP AL, '9'
JA L2 ; not digit
MOV DL, 'D' ; it’s a digit
JMP WRITE
L2:
CMP AL, 'A'
JAE L3
MOV DL, 'C' ; between ‘9’ and ‘A’: not digit nor letter (ASCII code)
JMP WRITE
L3:
CMP AL, 'Z'
JA L4
MOV DL, 'L' ; it’s a capital letter
JMP WRITE
Microprocessors (66322) 19 Dr. Aladdin Masri
Example 2
L4: CMP AL, 'a'
JAE L5
MOV DL, 'C' ; between ‘Z’ and ‘a’: not digit nor letter (ASCII code)
JMP WRITE
L5:
CMP AL, 'z'
JA L6
MOV DL, 'L' ; it’s a small letter
JMP WRITE
L6:
MOV DL, 'C' ; greater than ‘z’ then it’s not digit nor letter (ASCII code)
WRITE:
MOV AH, 2
INT 21H
INC CL
CMP CL, 50 ; find out if Cl = 50
JE L10
JMP START ; infinite loop
L10:
.EXIT ; exit to DOS
END
Microprocessors (66322) 20 Dr. Aladdin Masri
Exercises
1. Write an 8088/8086 assembly program that counts the
number of occurrence of the letter ‘A’ whether capital
or small (A or a) in the null terminated string that
starts at location STR. The result should be stored in
AL.
2. Write an 8088/8086 assembly program that displays
the 2-digit BCD number stored in AL (suppose
AL=48H).
3. Write an 8088/8086 assembly program that counts the
length of a null terminated string that starts at location
STR. The result should be moved to port 0E30H.
Assume string length will not exceed 255 character.
Microprocessors (66322) 21 Dr. Aladdin Masri
The Conditional Set Instructions
• 80386+ processors also contain conditional set
instructions.
– Conditions tested by conditional jumps put to work
with the conditional set instructions
• Conditional set instructions set a byte to either
01H or clear a byte to 00H, depending on the
outcome of the condition under test
• As an example, SETNC Var1 instruction
– Puts 01H in memory location Var1 if no carry
– When carry, it puts 00H
Microprocessors (66322) 22 Dr. Aladdin Masri
The Conditional Set Instructions
Microprocessors (66322) 23 Dr. Aladdin Masri
LOOP Instruction
• A combination of a decrement CX and the JNZ
conditional jump
• In 8086 – 80286 LOOP decrements CX (not CL).
– If CX != 0, it jumps to the address indicated by the
label
– If CX becomes 0, the next sequential instruction
executes
• In 80386+, LOOP decrements either CX or ECX,
depending upon instruction mode.
Microprocessors (66322) 24 Dr. Aladdin Masri
Conditional LOOPs
• LOOP instruction also has conditional forms: LOOPE and
LOOPNE
• LOOPE (loop while equal) instruction jumps if CX != 0
while an equal condition exists.
– will exit loop if the condition is not equal or the CX register
decrements to 0
• LOOPNE (loop while not equal) jumps if CX !=0 while a
not-equal condition exists
– will exit loop if the condition is equal or the CX register
decrements to 0
• Alternates exist for LOOPE and LOOPNE.
– LOOPE same as LOOPZ
– LOOPNE instruction is the same as LOOPNZ
Microprocessors (66322) 25 Dr. Aladdin Masri
LOOP Instruction – Example
• The following program calculates the sum of an array of words, save the
result in AX
.MODEL SMALL
.DATA
INTARRAY WORD 100H,200H,300H,400H
.CODE
MOV DI,OFFSET INTARRAY
MOV CX,LENGTHOF INTARRAY ;simply MOV CX, 4
MOV AX,0 ;rest sum
L1:
ADD AX, [DI]
INC DI
LOOP L1
.EXIT
END
Microprocessors (66322) 26 Dr. Aladdin Masri
Exercises
1. Write an assembly code to read three numbers named X, Y and Z, from the user.
Your program should find the smallest number and stores it in a fourth variable
W.
2. Rewrite the following C-code in assembly language:
for( int i=1; i<=12;i++){
cout<<"hi";
if(i%3==0){cout<<"\n"; continue;}
printf("_%d\t",i);
}
3. Rewrite the following C-code in assembly language:
while (x != y){
if (x > y)
x = x - y ;
else
y = y - x ;
}
Microprocessors (66322) 27 Dr. Aladdin Masri
6–2 Controlling the Flow of the Program
The dot “.” Commands
• Easier to use assembly language statements .IF,
.ELSE, .ELSEIF, and .ENDIF to control the flow of
the program than to use the correct conditional jump
statement.
• Other statements developed include .REPEAT–
.UNTIL and .WHILE–.ENDW
• The .BREAK and .CONTINUE statements are
available for use with the while loop.
• These statements always indicate a special assembly
language command to MASM
• The dot commands do not function using the Visual
C++ inline assembler
Microprocessors (66322) 28 Dr. Aladdin Masri
The dot “.” Commands – Example
• An implementation of the following C example, using the .WHILE and .IF directives
while ( opl < op2 ){
op1++ ;
if ( op2 == op3)
x = 2 ;
else
X = 3 ; }
• Solution:
.data
opl DW 2
op2 DW 4
op3 DW 5
. code
mov ax, opl
mov bx, op2
mov cx, op3
.WHILE ax < bx
inc ax
.IF bx == cx
mov X,2
.ELSE
mov X,3
.ENDIF
.ENDW
Microprocessors (66322) 29 Dr. Aladdin Masri
The dot “.” Commands – Example
• Convert the following C loop using .REPEAT - .UNTIL instructions
for(c=0;c!=9;c+=3);
• Solution:
mov cl, 0
.REPEAT
add cl, 3
.UNTIL cl==9
• Repeat the example using .WHILE instruction
mov cl, 0
.WHILE cl!=9
add cl, 3
.ENDW
Microprocessors (66322) 30 Dr. Aladdin Masri
The dot “.” Commands – Example
• Develop a short sequence of instructions that uses the
REPEAT-UNTIL construct to copy the contents of byte-
sized memory BLOCKA into byte-sized memory BLOCKB
until NULL is moved.
• Solution:
MOV SI,OFFSET BLOCKA
MOV DI,OFFSET BLOCKB
CLD
.REPEAT
LODSB
STOSB
.UNTIL AL == 0
Microprocessors (66322) 31 Dr. Aladdin Masri
6–3 Procedures – Subroutines
• Procedures are groups of instructions that perform one task, stored in
memory once, used as often as necessary.
– It saves memory space and makes it easier to develop software
– Disadvantage of procedure is time it takes the computer to link to, and return
from it.
• Syntax:
ProcedureName PROC Attribute
Statements-list
ProcedureName ENDP
1. ProcedureName may be any valid identifier.
2. Begins with the PROC directive and ends with the ENDP directive
3. Attribute is the type of procedure: NEAR or FAR
– Procedures that are to be used by all software (global) should be written as far
procedures.
– Procedures that are used by a given task (local) are normally defined as near
procedures.
• Most procedures are near procedures.
Microprocessors (66322) 32 Dr. Aladdin Masri
Procedures
• Procedure type can be followed by the USES
statement.
– USES allows any number of registers to be
automatically pushed to the stack and popped from
the stack within the procedure
• The CALL directive links to the procedure
• The RET (return) directive returns from the
procedure
Microprocessors (66322) 33 Dr. Aladdin Masri
Procedures – Examples
SUMS PROC NEAR
ADD AX,BX
ADD AX,CX
ADD AX,DX
RET
SUMS ENDP
SUMS1 PROC FAR
ADD AX,BX
ADD AX,CX
ADD AX,DX
RET
SUMS1 ENDP
SUMS3 PROC NEAR USES BX CX DX ;BX, CX, DX will be pushed automatically into the stack
ADD AX,BX
ADD AX,CX
ADD AX,DX
RET
SUMS ENDP
Microprocessors (66322) 34 Dr. Aladdin Masri
CALL and RET Instructions
• The CALL construction is a combination of a PUSH and a
JMP instruction.
– CALL instruction differs from the JMP instruction because it
pushes the address of the instruction following the CALL
(return address) on the stack.
– Transfers the flow of the program to the procedure.
• RET instruction removes an address from the stack so the
program returns to the instruction following the CALL.
• CALL instructions may contain a register operand.
– Always uses a 16-bit offset address
• An example CALL BX
– Pushes the contents of IP onto the stack
– Then jumps to the offset address, located in register BX, in the
current code segment
Microprocessors (66322) 35 Dr. Aladdin Masri
CALL and RET Instructions
• The effect of CALL and RET instructions on the stack and
instruction pointer.
Microprocessors (66322) 36 Dr. Aladdin Masri
CALLs with Register Operands
Example
• A DOS program that displays OK using the DISP procedure.
.MODEL SMALL
.CODE
MOV BX,OFFSET DISP ;load BX with offset DISP
MOV DL,’O’ ;display O
CALL BX
MOV DL,’K’ ;display K
CALL BX
.EXIT
DISP PROC NEAR
MOV AH,2
INT 21H
RET
DISP ENDP
END
Microprocessors (66322) 37 Dr. Aladdin Masri
Procedures – Example
• Write an assembly procedure to print a “Hello!” message using procedures
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 'Hello!$' ;define string to display
.CODE
MOV AX,@DATA
MOV DS,AX ;load DS with address of data segment
LEA DX,MSG ;set DX to point to string
CALL DISPLAY_DX ;call procedure
.EXIT
DISPLAY_DX PROC NEAR ;declare procedure DISPLAY_DX
MOV AH,09H ;define dos function number
INT 21H ;call dos function to display "hello!"
RET ;return to main
DISPLAY_DX ENDP ;end of procedure
END ;end of program
Microprocessors (66322) 38 Dr. Aladdin Masri
CALLs with Indirect Memory
Addresses
• Useful when different subroutines need to be chosen in a
program.
– Selection process is often keyed with a lookup table
• Essentially the same as the indirect jump that used a lookup
table for a jump address.
• Example:
.DATA
TABLE DW ONE ;address of procedure ZERO
DW TWO ;address of procedure ONE
DW THREE ;address of procedure TWO
.CODE
...
CALL TABLE[BX]
Microprocessors (66322) 39 Dr. Aladdin Masri
Exercises
1. Write a near procedure that cubes the contents of the CX register.
This procedure may not affect any register except CX.
2. Write a procedure that multiplies DI by SI and then divides the
result by 100H. Make sure that the result is left in AX upon
returning from the procedure. This procedure may not change any
register except AX.
3. Write a procedure that sums AX, BX, CX, and DX. If a carry
occurs, place a logic 1 in DI. If no carry occurs, place a 0 in DI.
The sum should be found in AX after the execution of your
procedure.
4. Write a procedure named multiply that computes the product of
two signed 16-bit operands. The operands will be passed in
registers SI and DI. The procedure should return the result on AX.
Write a program that uses the multiply procedure
Microprocessors (66322) 40 Dr. Aladdin Masri
Macros
• A group of instructions that perform one task, just as a procedure performs
one task.
– A procedure is accessed via a CALL instruction while technically there is no
CALL instruction involved with macros
– A macro and all instructions defined in the macro, is inserted in the program at
the point of usage
• Creating a macro is very similar to creating a new opcode
• A macro can be defined using the MACRO and ENDM directives.
macroname MACRO [parameter-l, parameter-2,...]
statement-list
ENDM
• Statements should be indented between macroname and ENDM
– The statements between the MACRO and ENDM directives are not assembled
until the macro is invoked.
• There can be any number of parameters in the macro definition, as long as
they are separated by commas.
Microprocessors (66322) 41 Dr. Aladdin Masri
Macros
• Using the REQ qualifier, can specify that a macro parameter is
required.
– If the macro is called without an argument to match the required
parameter, the assembler displays an error.
• A macro is invoked (called) by inserting its name into a program's
source code:
macroname argument-1, argument-2, ...
– Macroname must be the name of a macro defined prior to this point in
the source code.
• The order of arguments must correspond to the order of parameters,
but the number of arguments does not have to match the number of
parameters:
1. If too many arguments are passed, the assembler issues a warning.
2. If too few argument s are passed to a macro, the unfilled
parameters are left blank.
Microprocessors (66322) 42 Dr. Aladdin Masri
Macros – Example
• This macro moves the word-sized contents of memory
location B into word-sized memory location A
MOVE MACRO A,B
PUSH AX
MOV AX,B
MOV A,AX
POP AX
ENDM
• When used in the program:
MOVE VAR1,VAR2 ;move VAR2 into VAR1
Microprocessors (66322) 43 Dr. Aladdin Masri
Macros – Local Variables
• A local variable is one that appears in the
macro, but is not available outside the macro.
– To define use the LOCAL directive
• The LOCAL directive must always be used
on the line immediately following the
MACRO statement or an error occurs.
• The LOCAL statement may have up to 35
labels, all separated with commas.
Microprocessors (66322) 44 Dr. Aladdin Masri
Macros – Example
Microprocessors (66322) 45 Dr. Aladdin Masri
Macros – Example
• Write a macro called MUL32 that multiplies the 16-bit contents of
variable VAR1 to the 16-bit contents of variable VAR2. The result
should be stored in a third variable called RES. Make sure that this
macro may not change any register
• Solution:
MUL32 MACRO VAR1,VAR2,RES
PUSH AX
PUSH DX
MOV AX, VAR1
MUL VAR2
MOV RES, AX
MOV RES+2,DX
POP DX
POP AX
ENDM
Microprocessors (66322) 46 Dr. Aladdin Masri
Macros – Example
• Write a macro that sums a list of signed byte-sized data by the macro ADDM LIST,LENGTH. The
label LIST is the starting address of the data block and LENGTH is the number of data added. The
result must be a 16-bit sum found in AX at the end of the macro sequence.
• Solution:
ADDM MACRO LIST,LENGTH
PUSH CX
PUSH BX
PUSH SI
MOV CX,LENGTH
MOV SI,LIST
MOV BX,0 ;make the sum equals to zero
LP: MOV AL,[SI]
CBW ;signed value converted to word
ADD BX, AX
INC SI
LOOP LP
MOV AX,BX ;store the result in AX
POP SI
POP BX
POP CX
ENDM
Microprocessors (66322) 47 Dr. Aladdin Masri
Exercises
1. Write a macro to swap two memory locations
WORD1 and WORD2
2. Write a macro to display a ‘$’ terminated
string using DOS interrupt.
3. Write two macros; one to save register AX,
BX, CX and DX into the stack, and the other
to restore them from the stack
Microprocessors (66322) 48 Dr. Aladdin Masri
6–5 Introduction to Interrupts
• An interrupt is a hardware-generated CALL
– externally derived from a hardware signal
• Or a software-generated CALL
– Mainly through the INT instruction
• Either type interrupts the program by calling an interrupt service
procedure (ISP) or interrupt handler
• Interrupts access the ISP through a pointer or a vector stored in the
interrupt vector table
– Each entry is a 4-byte number stored in the first 1024 bytes of memory
in real mode.
– For example, INT 10H instruction calls the interrupt service procedure
whose address is stored beginning at memory location 40H (10H × 4)
• 256 different interrupt vectors.
– Each INT instruction has a numeric operand whose range is 0 to 255
(00H–FFH)
Microprocessors (66322) 49 Dr. Aladdin Masri
Introduction to Interrupts
Microprocessors (66322) 50 Dr. Aladdin Masri