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

Lecture 3

microprocessor and assembly language course lecture 3

Uploaded by

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

Lecture 3

microprocessor and assembly language course lecture 3

Uploaded by

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

Lecture 3

Arithmetic, Logic
&
control flow Instructions
By
Dr. Mohammed Y.M Alnaham

Dr. Mohammed Y.M Alnaham 1


Overview

At the end of this lecture, we will know


about:
 Arithmetic and Logic Instructions
 Decision making and repeating statement
 Jump and loop instructions
Dr. Mohammed Y.M Alnaham 2
Assembly:- InstructionsArithmetic and Logic 86
Instructions
• Add& Sub — Integer Addition
 The add instruction adds together its two operands,
storing the result in its first operand.
 Note, whereas both operands may be registers, at
most one operand may be a memory location.
 The sub instruction stores in the value of its first
operand the result of subtracting the value of its
second operand from the value of its first operand. As
3 with add Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsArithmetic and Logic 86
Instructions
• Syntax of add&sub
Examples for add Examples for sub
• add <reg>,<reg>

• add <reg>,<mem> add eax, 10; EAX ← sub al, ah ; AL ← AL


• add <mem>,<reg> EAX + 10. - AH sub eax, 216 ;
• add <reg>,<con> add BYTE PTR [var], subtract 216 from the
• Add <mem>,<con> 10 ; add 10 to the value stored in EAX

single byte stored at

4
memory address var
Dr. Mohammed Y.M Alnaham
Assembly:- InstructionsArithmetic and Logic 86
Instructions
inc, dec — Increment, Decrement

• The inc instruction increments the contents of its


operand by one.
• The dec instruction decrements the contents of its
operand by one.

5 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsArithmetic and Logic 86
Instructions
• Syntax of inc, dec
Examples for dec&inc insteructions
• inc <reg>
dec eax; subtract one from the
• inc <mem>
contents of EAX.
• dec <reg>
inc DWORD PTR [var]; add one to the
• dec <mem> 32-bit integer stored at location var

6 Dr. Mohammed Y.M Alnaham


Assembly:- InstructionsArithmetic and Logic 86
Instructions

Examples for and&or Examples for


• Syntax of and&or&xor
and eax, 0Fh; clear xor
all but the last 4 bits xor edx, edx ;
• And,or,xor <reg>,<reg>

• And,or,xor <reg>,<mem> of EAX. set the contents of


• And,or,xor <mem>,<reg> or eax, FFh; don’t EDX to zero.
• And,or,xor <reg>,<con>
change of the
• And,or,xor <mem>,<con>
7
content of eax
Dr. Mohammed Y.M Alnaham
Not&Neg Instructions

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

8 Dr. Mohammed Y.M Alnaham


Flag Register For 8086
Microprocessor

Dr. Mohammed Y.M Alnaham 9


Jump and Loop
• Jump and Loop instructions transfers control to another
program

• The transfers can be unconditional or conditional

• Depends on a particular combination of status flags


settings

• Conversion of algorithm is easier in assembly language


Dr. Mohammed Y.M Alnaham 10
Short Conditional Jumps
• Unlike JMP instruction that does an
unconditional jump, there are
instructions that do a conditional
jumps (jump only when some
conditions are in act). These
instructions are divided in three
groups,

• First: group just test single flag,

• Second: compares numbers as


signed,

• Third: compares numbers as


Dr. Mohammed Y.M Alnaham 11
Example of Jump
PRINT_LOOP:
.MODEL SMALL
INT 21H
.STACK 100H INC DL

.CODE DEC CX

JNZ PRINT_LOOP ;
MAIN PROC
; return to DOS

MOV AH,2 MOV AH,4Ch ;DOS exit

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.

• If the preceding instruction contains zero (i.e. CX=0) then the


program goes to execute DOS return instructions.

• PRINT_LOOP is the first statement label

• Labels are needed to refer another instruction

• Labels end with colon (:)

• Labels are placed on a line byDr. Mohammed


themselves
Y.M Alnaham
to make it stand out. 13
Conditional Jumps
• JNZ is an example of Conditional Jump

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

 The CPU looks at the FLAGS register (it reflects the


result of last thing that processor did)

 If the conditions for the jump (combination of status


flag settings) are true, the CPU adjusts the IP to point
to the destination label.

 The instruction at this label will be done next.

 If the jump condition is false, then IP is not altered


and naturally the next instruction is performed.
Example(cont’d…)

In the previous example:

 The JNZ PRINT_LOOP is executed by


inspecting ZF

 If ZF=0 then control is transferred to


PRINT_LOOP and continues

 If the ZF=1 then the program goes on to


execute next (i.e. MOV AH,4CH)
CMP Instruction
 CMP destination , source
 Compares the destination with source by computing
contents
 It computes by…

destination contents - source contents


 The result is not stored but the FLAGS are affected
 The OPERANDS of CMP may not both be Memory
Locations
 Destination may not be CONSTANT
 CMP is just like SUB. However result is not stored in
destination.
Signed Conditional Jumps
• Jump if Greater than ZF = 0 and
JG or • Jump if Not Less than or SF = OF(Overflow
JNLE
Equal to Flag)
• Jump if Greater than or
JGE or Equal to
SF = OF
JNL • Jump if Not less than or
Equal to
• Jump if less than
JL or • Jump if not greater than SF<>OF
JNGE
or equal

JLE or • Jump if less than or Equal


ZF = 1 or SF<> OF
JNG • Jump if not greater than
Unsigned Conditional Jumps
• Jump if Above
JA or ZF = 0 and
• Jump if Not Below or Equal
JNBE CF = 0
to

JAE or • Jump if Above or Equal to


CF = 0
JNB • Jump if Not Below

JB or • Jump if Below
CF = 1
JNAE • Jump if not Above or Equal

JBE or • Jump if Below or Equal


CF=1 or ZF = 1
JNA • Jump if Not Above
Single-Flag Jumps
JE or • Jump if Equal
JZ Jump if equal to Zero ZF = 1

JNE or • Jump if Not Equal


JNZ Jump if Not Zero ZF = 0

CF = 1
JC • Jump if Carry CF = 0
JNC • Jump if no Carry CF=0

JO • Jump if Overflow CF=1 or ZF = 1

JNO • Jump if No Overflow OF=1


JS Jump if Sign Negative SF = 1

JNS Jump if Non-Negative Sign SF =0


JP/JPE Jump if Parity Even PF=1
JNP/JPO Jump if parity Odd PF=1
Conditional Jumps
Interpretation
 Signed JUMPs correspond to an analogous unsigned JUMPs (i.e.
JG is equivalent to JA)
 Signed jump’s operates on ZF, SF and OF
 Unsigned JUMP’s operates on ZF and CF
 Using wrong kind of JUMP can lead to wrong results. For
example: for AX= 7FFFh and BX= 8000h

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

 To deal with ASCII character set, either Signed or


Unsigned jumps may be used.

i.e. sign bit of a byte in a character is


always zero.

 However, while comparing extended ASCII


characters (80h to FFh ), UNSIGNED jumps should
be used.
JMP Instruction

 JMP instruction causes an unconditional


transfer of control.

JMP destination

 Destination is usually a label in the same


segment as the JMP itself.


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

 As the Jump is so primitive, it is difficult to


code an algorithm with jumps without some
guidelines.

 The High-level languages (conditional IF-ELSE


and While loops) can be simulated in
assembly.
Branching Structures
 Branching structures enable a program to
take different paths, depending on
conditions.

 Here, We will look at three structures.

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.

• lf It is true, the true-branch statements are


executed.

• lf It is false, nothing is done, and the program goes


on to whatever follows.

• Example: to Replace a number in AX by its absolute


value…

IF AX < 0 CMP AX, 0


THEN JNL END_IF
replace AX by NEG AX
-AX END_IF:
END_IF
IF-THEN-ELSE

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.

• If It is true, the true-branch statements are executed.

• If It is false then False-branch statements are executed.

• Example: Suppose AL and BL contain extended ASCII


characters. Display the one that comes first in the
character sequence…
MOV AH,2
IF AL <= BL
CMP
THEN AL,BL ;AL<=BL ?
Display the JNBE ELSE_
character in AL MOV DL,AL
ELSE JMP DISPLAY
Display the ELSE_:
MOV DL,BL
character in BL
DISPLAY:
END IF INT 2lh
CASE
A CASE is a multi-way branch structure that tests a
register, variable, or expression for particular values or a
range of values.

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

condition_1' AND condition_2’

or

condition_1 OR condition_2

 Where condition 1 and condition:2 are either true or false. We


will refer to the

 First of these as an AND condition and to the second as an OR


condition.
Example : AND
 An AND condition is true if and only if Condition_1 and
Condition_2 are both true. Likewise, if either condition is false,
then the whole thing is false.

 Read a character, and if it's an uppercase letter, display


it.

Read a character (into AL)

IF ('A'<= character) and (character <= 'Z')

THEN

display character

END IF
Converting to Assembly
CMP AL, 'Z'
;read a character

MOV AH,1 JNGE END_lF ;no


exit
INT 21H
MOV DL, AL.
if ('A' <= char> and
(chai: <= 'Z')
MOV AH, 2
CMP AL, ‘A ;char >’A’
INT 21H
JNGE END_lF ;no exit
END_IF:
OR Conditions
 Condition_1 OR condition_2 is true if at least one of the conditions is true;
it is only false when both conditions are false.

 Read a character. If it's "y" or "Y", display it; otherwise, terminate


the program.

Read a character (into AL)

IF (character = ‘y' OR (character = 'Y'J

THEN

display it

ELSE

terminate the program

END IF
Assembly Conversion
MOV AH,1 THEN:

INT 21H MOV AH,2


;prepare to display
CMP AL,’y’
MOV CL,AL
;AL==‘y’ ;get char

JE THEN INT 21H


;display it
CMP AL, 'Y'
;char ~ 'Y'? JMP END IF
;and exit -
JE THEN
ELSE_:
;yes, go to display
it MOV AH, 4CH

JMP ELSE_ INT 21H


;no - Terminate ;DOS exit

END_IF:
Looping Structure
 A loop Is a sequence of instructions that is
repeated.

 The number of times to repeat may be known in


advance, or

 It may depend on conditions

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,

FOR loop_count times DO

Statements

END_FOR

• The LOOP instruction can be used to implement a FOR loop. i.e.

LOOP destination_label

• 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,
FOR LOOP
• The control is transferred to
destination label until CX
becomes 0.

• A FOR LOOP can be


implemented using the LOOP
instruction:

TOP:

;initialize CX to loop_count

;body of the loop

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.

 if CX contains 0 when the loop is entered, the


LOOP instruction causes CX to be decremented to
FFFFh

 The loop is then executed FFFFh=65535 times


more!

 To Prevent this, the instruction JCXZ (jump if CX is


zero) may be used before the loop. Its syntax

JCXZ destination_label
Use of JCXZ
• If CX contains 0, control transferred to the
destination label. So a loop implemented as follows
is bypassed if CX is 0:

JCXZ SKIP

TOP:

;body of the loop

LOOP TOP

SKIP:
WHILE LOOP

• This WHILE LOOP


depends on a
condition.

• WHILE condition DO

statements

END_WHILE
WHILE LOOP
 The condition is checked at the top of the loop.

 If true, the statements are executed;

 If false, the program goes on to whatever


follows.

 It is possible the condition will be false initially, in


which case the loop body ls not executed at all.

 The loop executes as long as the condition is true


• Example: WHILE LOOP
Write some code to count the number of characters in an input
line.
MOV DX,0 ; char
count

MOV AH,1
• Initialize count to 0
INT 21H

• Read a character WHILE_:

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,

 So, you must make sure that any variables


involved in the condition are initialized
before the loop is entered.

 So you read a character before entering the


loop, and read another one at the bottom.

 The label WHILE_: .is used because WHILE is a


reserved word
REPEAT LOOP

REPEAT

statements

UNTIL condition

In a REPEAT…UNTIL loop, the statements are


executed, and then the condition is checked.

• If true, the loop terminates;

• If false, control branches to the top of the loop.


 Write code to read characters until a
blank is read.

MOV AH,1
REPEAT
REPEAT:
read a
character INT 21H

UNTIL character is a CMP AL,' '


BLANK
JNE REPEAT
Difference between WHILE and
REPEAT
 Use of a WHILE loop or a REPEAT loop Is a matter of personal
preference.

 The advantage of a WHILE is that the loop can be bypassed


if the terminating, condition is initially false.

 Whereas the statements in a REPEAT must be done at least


once.

 However, the code for a REPEAT loop Is likely to be a little


shorter because there is only a conditional jump at the end,

 But a WHILE loop has two jumps: a conditional jump at the


top and a JMP at the bottom.

You might also like