0% found this document useful (0 votes)
32 views

Assembly Lecture 04

Uploaded by

aro534721
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Assembly Lecture 04

Uploaded by

aro534721
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture 4

Loop instruction & logic instructions


Chapter Outline
The Loop instruction
While Loop
REPEAT Loop
 Logic Instructions
 AND, OR, XOR Instructions
 NOT Instruction
 TEST Instruction
IF-THEN-ELSE
• Example: Suppose AL and BL contain extended ASCII characters.
Display the one that comes first in the character sequence.
• Solution:
Pseudocode:
IF AL <= BL
THEN
display the character in AL
ELSE
display the character in BL
END_IF

continue
IF-THEN-ELSE
It can be coded as follows:
; if AL <= BL
CMP AL, BL ; AL <= BL?
JNBE ELSE_ ; no, display char in BL
; then ; AL <= BL
MOV DL, AL ; move char to be displayed
JMP DISPLAY ; go to display
ELSE_: ; BL < AL
MOV DL, BL
DISPLAY:
MOV AH, 2 ; prepare to display
INT 21h ; display it
Branches with compound Conditions
• Sometimes the branching condition in an IF or CASE takes the
form:

condition_1 AND condition_2 AND condition


or
condition_1 OR condition_2 OR condition

where condition_1 and condition_2 are either true or false.


AND Condition
• An AND condition is true if and only if all conditions are true.

• Example: Read a character, and if it’s an uppercase letter, display


it.
• Solution:
Pseudocode:
Read a character (into AL)
IF ('A' <= character) and (character <= 'Z')
THEN
display character
END_IF

continue
AND Condition
It can be coded as follows:
; read a character
MOV AH,1 ; prepare to read
INT 21h ; char in AL
; if ('A' <= char) and (char <='Z')
CMP AL, 'A' ; char >= 'A'?
JNGE END_IF ; no, exit
CMP AL, 'Z' ; char <= 'Z'?
JNLE END_IF ; no, exit
; then display char
MOV DL, AL ; get char
MOV AH, 2 ; prepare to display
INT 21h ; display char
END_IF:
OR Condition
• An OR condition is true if at least one of the conditions is true.
• Example: Read a character. If it’s 'y' or 'Y', display it; otherwise,
terminate the program.
• Solution:
Pseudocode:
Read a character (into AL)
IF (character = 'y') or (character = 'Y')
THEN
display character
ELSE
terminate the program
END_IF
continue
OR Condition
It can be coded as follows:
; read a character
MOV AH,1 ; prepare to read
INT 21h ; char in AL
; if (char = 'y') or (char = 'Y')
CMP AL, 'y' ; char = 'y'?
JE THEN ; yes, go to display it
CMP AL, 'Y' ; char = 'Y'?
JE THEN ; yes, go to display it
JMP ELSE_ ; no, terminate
THEN:
MOV DL, AL ; get char
MOV AH, 2 ; prepare to display
INT 21h ; display char
JMP END_IF ; and exit
ELSE_:
MOV AH, 4Ch
INT 21h ; DOS exit
END_IF:
Loop Instruction
• The LOOP instruction can be used to implement a for loop.
• Syntax:
SHORT address LOOP

• 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 < > 0) control transfers to destination_label
else the next instruction after LOOP is done.
Loop Instruction
• Using the instruction LOOP, a FOR loop can be implemented as
follows:

; initialize CX to loop_count
TOP:
; body of the loop
LOOP TOP
• In for loop the loop statements are repeated a known number of
times.
FOR Loop
• Example: Write some code to display a row of 80 stars.
• Solution:
Pseudocode:
FOR 80 times DO ; what if CX =0?
display '*'
END_IF MOV CX, 80
It can be coded as follows: MOV AH, 2
MOV CX, 80 MOV DL, '*'
MOV AH, 2 JCXZ SKIP ;jump if CX=0
MOV DL, '*' TOP:
TOP: INT 21h
INT 21h LOOP TOP
LOOP TOP SKIP:
WHILE Loop
• This loop depends on a condition.
• The condition is checked at the top; if true the statements are
executed; if false program goes into whatever follow.

• Pseudocode:
WHILE condition DO
statements
END_WHILE
WHILE Loop
• Example: Write some code to count the number of characters in an
input line.
• Solution:
Pseudocode:
initialize count to 0
read a character
WHILE character <> carriage_return DO
count = count + 1
read character
END_WHILE

continue
WHILE Loop
It can be coded as follows:
MOV DX, 0 ; DX counts characters
MOV AH, 1 ; prepare to read
INT 21h ; character in AL
WHILE_:
CMP AL, 0Dh ; CR?
JE END_WHILE ; yes, exit
INC DX ; not CR, increment count
INT 21h ; read a character
JMP WHILE_ ; loop back
END_WHILE:
REPEAT Loop
• This loop depends on a condition.
• the statements are executed then the condition is checked. If true
the loop terminates; if false, control branches to the top of the
loop.

• Pseudocode:
REPEAT
Statements
UNTIL conditions
REPEAT Loop
• Example: write code to read characters until a blank is read

• Pseudocode:
REPEAT
Read character
UNTIL character is blank The code is:
MOV AH,1
REAPEAT:
INT 21H
CMP AL,’ ‘
JNE REAPEAT
WHILE Versus REPEAT
• Use of a WHILE loop or a REPEAT loop is a matter of personal
preference.

• A WHILE loop can be bypasses if the terminating condition is


initially false. (a REPEAT loop must be done at least once)

• The code for a REPEAT loop is likely to be a little shorter because


there is only one jump. (WHILE loops has two jumps)
Logic Instructions
• Logic instructions can be used to clear, set , and examine individual
bits in a register or variable.

• Logic instructions:
• AND.
• OR.
• XOR.
• NOT.
• TEST.

Logic Instructions 3
Introduction
• Logic instructions can be used to change the bit pattern in a byte
or word.

• The ability to manipulate bits is generally absent in high-level


languages (except C) and is an important reason for
programming in
assembly language.

• It is used to check bit in a register or memory location, clear or


set a register contents

Logic Instructions 2
AND, OR, XOR Instructions

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

10101010 10101010 10101010


AND 11110000 OR 11110000 XOR 11110000
= 10100000 = 11111010 = 01011010

Logic Instructions 4
AND, OR, XOR Instructions
• Syntax register or memory location

AND destination, source constant, register, or memory location


OR destination, source
XOR destination, source

• The result of the operation is stored in the destination.

• Memory-to-memory operations are not allowed.

• Effect on flags:
• SF, ZF, PF reflect the result.
• AF is undefined.
• CF = OF = 0.

Logic Instructions 5
AND, OR, XOR Instructions
• One use of AND, OR, and XOR is to selectively modify the bits in
the destination.

• To do this, we construct a source bit pattern known as a mask.

• The mask bits are chosen so that the corresponding destination bits
are modified in the desired manner when the instruction is executed.

• To choose the mask bits, we make use of the following properties:


• b AND 1 = b • b OR 0 = b • b XOR 0 = b
• b AND 0 = 0 • b OR 1 =1 • b XOR 1 = ~b
where b represent a bit (0 or 1).

Logic Instructions 6
AND, OR, XOR Instructions
• From these, we may conclude that:

• The AND instruction can be used to clear specific destination


bits while preserving the others.
(0 mask bit clears - 1 mask bit preserves).

• The OR instruction can be used to set specific destination bits


while preserving the others.
(1 mask bit sets - 0 mask bit preserves).

• The XOR instruction can be used to complement specific


destination bits while preserving the others.
(1 mask bit complements - 0 mask bit preserves).

Logic Instructions 7
AND, OR, XOR Instructions
• Clear the sign bit of AL while leaving the other bits unchanged?

AND AL, 7Fh (01111111)


• Set the msb & lsb of AL while preserving the other bits?

OR AL, 81h (10000001)

• Change the sign bit of DX?

XOR DX, 8000h (10000000)

Logic Instructions 8
AND, OR, XOR Instructions
• Convert an ASCII digit to a number?

Character code
0 00110000
1 00110001
: :
9 00111001
AND AL, 0Fh

Logic Instructions 9
AND, OR, XOR Instructions
• Convert the lowercase character in DL to uppercase?

Character code character code


a 01100001 A 01000001
b 01100010 B 01000010
: : : :
z 01111010 C 01011010
AND DL, 0DFh (01001101)

Logic Instructions 10
AND, OR, XOR Instructions
• Clear AX?

MOV AX, 0 Three bytes machine code


or
SUB AX, AX Two bytes machine code (efficient)
or
XOR AX,AX Two bytes machine code (efficient)

• Clear A?

MOV A, 0 (memory location)

Logic Instructions 11
AND, OR, XOR Instructions
• Test CX for zero?

OR CX, CX

CX is unchanged but if CX = 0 then ZF = 1.

It can be used as an alternative to

CMP CX,0

Logic Instructions 12
NOT Instruction

a NOT a
0 1
1 0

NOT 10101010
= 01010101

Logic Instructions 13
NOT Instruction
• The NOT instruction performs the one’s complement operation on
the destination.

• Syntax:

NOT destination

• There is no effect on the status flags.

• Complement the bits in AX?

NOT AX

Logic Instructions 14
TEST Instruction
• The TEST instruction performs an AND operation of the
destination with the source but does not change the destination
contents.
• The purpose of the TEST instruction is to set the status flags.

• Syntax:

TEST destination, source


• Effect on flags:
• SF, ZF, PF reflect the result.
• AF is undefined.
• CF = OF = 0.

Logic Instructions 15
TEST Instruction
• The TEST instruction can be used to examine individual bits in an
operand.
• The mask should contain:
• 1’s in the bit positions to be tested.
• 0’s elsewhere.

• Jump to BELOW if AL contains an even number?

TEST AL,1
JZ BELOW

Logic Instructions 16

You might also like