Lecture 3
Lecture 3
Arithmetic, Logic
&
control flow Instructions
By
Dr. Mohammed Y.M Alnaham
4
memory address var
Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsArithmetic and Logic 86
Instructions
inc, dec — Increment, Decrement
not — Bitwise Logical NotLogically negates the operand contents (that is, flips all bit values in the operand).
Syntax
not <reg>
not <mem>
Example
not BYTE PTR [var] — negate all bits in the byte at the memory location var.
neg — NegatePerforms the two's complement negation of the operand contents.
Syntax
neg <reg>
neg <mem>
Example
neg eax — EAX → - EAX
.CODE DEC CX
JNZ PRINT_LOOP ;
MAIN PROC
; return to DOS
INT 21H
MOV CX,256
MAIN ENDP
MOV DL,0 ; ASCII of null Dr. Mohammed Y.M Alnaham
END MAIN
12
JNZ (Jump if Not Zero)
• JNZ is the instruction that controls loop.
• If result of preceding instruction is not Zero Then the JNZ transfers the
control to the instruction at label PRINT_LOOP.
Instruction:
• JXXX destination_label
• if the condition for the jump is true, the next
instruction to be executed is at
destination_label
• If condition is false, the instruction following the
jump is done next.
• i.e. for JNZ if the preceding instruction is non-
zero
Implementation of
Conditional JUMP by CPU
JB or • Jump if Below
CF = 1
JNAE • Jump if not Above or Equal
CF = 1
JC • Jump if Carry CF = 0
JNC • Jump if no Carry CF=0
CMP AX,BX
JA BELOW
Even though 7FFFh>8000h in a signed sense, the program does
not jump to label BELOW. Because
In unsigned sense (JA) 7FFFh < 8000h
Working with Characters
JMP destination
JMP Vs JNZ
TOP:
DEC CX
TOP:
JNZ BOTTOM ; keep
looping till CX>0
DEC CX
JMP EXIT
JNZ TOP
BOTTOM:
MOV AX,BX JMP TOP
EXIT:
MOV AX,BX
High-Level Language
Structures
Jump can be used to implement branches and
loops
1. IF-THEN
2. IF-THEN-ELSE
3. CASE
IF-THEN
IF condition is true.
THEN
execute true-
branch statements
END_IF
A Pseudo Code , Algorithm and Code
for IF-THEN
• The condition is an expression that is either true or
false.
IF condition is true
THEN
execute true-
branch statements
ELSE
execute false-
branch statements
END_IF
A Pseudo Code and algorithm and Code for
IF-THEN-ELSE
• The condition is an expression that is either true or false.
CASE Expression
Values_1:
Statement_1
Values_2:
Statement_2
Values_n:
Statement_n
END_CASE
CASE
• Example: If AX contains a negative number, put
-1 in BX; if AX contains 0, put 0 in BX; and if AX
contains a positive number, put 1 in BX.
CMP AX,O
JL NEGATIVE
JE ZERO
JG POSITIVE
CASE AX
NEGATIVE:
<0 : put -1 in BX MOV BX,-1
=0 : put 0 in BX JMP END_CASE
ZERO:
>0 : put +l in BX MOV BX,0
END_CASE JMP END_CASE
POSITIVE:
MOV BX, l
END_CASE:
Branches with Compound
Conditions
Sometimes the branching condition in an IF or CASE takes the
form
or
condition_1 OR condition_2
THEN
display character
END IF
Converting to Assembly
CMP AL, 'Z'
;read a character
THEN
display it
ELSE
END IF
Assembly Conversion
MOV AH,1 THEN:
END_IF:
Looping Structure
A loop Is a sequence of instructions that is
repeated.
FOR LOOP
WHILE LOOP
REPEAT LOOP
FOR LOOP
• FOR LOOP is a loop structure in which the loop statements are repeated
a known number of times (a count-controlled loop). In pseudo
code,
Statements
END_FOR
LOOP destination_label
TOP:
;initialize CX to loop_count
LOOP TOP
Example:
MOV CX,0
• Write a count-
controlled loop to MOV AH,2
display a row of 80
stars:
MOV DL, '*'
FOR 80 times DO
TOP:
display ‘*’
INT 21H
END_FOR
LOOP TOP
JCXZ and The LOOP
FOR LOOP executes at least once.
JCXZ SKIP
TOP:
LOOP TOP
SKIP:
WHILE LOOP
• WHILE condition DO
statements
END_WHILE
WHILE LOOP
The condition is checked at the top of the loop.
MOV AH,1
• Initialize count to 0
INT 21H
CMP AL,0DH ; CR ?
• WHILE character <>
carriage_return DO JE END_WHILE ;yes,
exit
• count =count + 1
INC DX ; not CR so
inc
• read a character
INT 21H ; read
next char
• END_WHILE
JMP WHILE_ ; loop
WHILE LOOP Insights
A WHILE loop checks the terminating condition
at the top of the loop,
REPEAT
statements
UNTIL condition
MOV AH,1
REPEAT
REPEAT:
read a
character INT 21H