Assembly Lecture 04
Assembly Lecture 04
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:
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.
• 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.
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
Logic Instructions 4
AND, OR, XOR Instructions
• Syntax register or memory location
• 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.
• The mask bits are chosen so that the corresponding destination bits
are modified in the desired manner when the instruction is executed.
Logic Instructions 6
AND, OR, XOR Instructions
• From these, we may conclude that:
Logic Instructions 7
AND, OR, XOR Instructions
• Clear the sign bit of AL while leaving the other bits unchanged?
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?
Logic Instructions 10
AND, OR, XOR Instructions
• Clear AX?
• Clear A?
Logic Instructions 11
AND, OR, XOR Instructions
• Test CX for zero?
OR CX, CX
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
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:
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.
TEST AL,1
JZ BELOW
Logic Instructions 16