0% found this document useful (0 votes)
35 views29 pages

14 Assembly - Lecture5

The document outlines various programming concepts related to logic and control, including address types, jump instructions, and loop structures. It details the functions of general-purpose registers and flags, as well as conditional and unconditional jumps. Additionally, it provides pseudocode examples and assembly code implementations for IF-THEN-ELSE, AND/OR conditions, and different types of loops such as WHILE and REPEAT.

Uploaded by

jm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views29 pages

14 Assembly - Lecture5

The document outlines various programming concepts related to logic and control, including address types, jump instructions, and loop structures. It details the functions of general-purpose registers and flags, as well as conditional and unconditional jumps. Additionally, it provides pseudocode examples and assembly code implementations for IF-THEN-ELSE, AND/OR conditions, and different types of loops such as WHILE and REPEAT.

Uploaded by

jm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Program Logic and Control

Chapter Outline

➢Short, near and far address


➢JMP Instruction
➢The CMP Instruction
➢Conditional Jump instruction
➢The Loop instruction
➢While Loop
➢REPEAT Loop
General-purpose Registers
AX
• Accumulator register
• Where most arithmetic and logical computations take place

BX
• Base register
• Used to hold indirect addresses

CX
• Count register
• Used to count iterations in a loop or specify numbers

DX
• Data register
• Holds overflow from certain arithmetic operations
• Holds I/O addresses when accessing data
Flags Registers
• “program status registers”
• Used by processor as a series of 16 individual bits which keep
track of certain conditions that happened when the PC is running.
• Flags are considered set when they are equal to 1, and cleared
when they are equal to 0.
Flags Registers
• Carry Flag (CF) – contains a carry (0 or 1) from the high-order (leftmost
bit) bit following arithmetic operations and some shift and rotate
operations
• Parity Flag (PF) – contains a check of the low order (rightmost) 8-bit of
data operations. An odd number of data bits sets the flag to 0 and an even
number to 1.
• Zero Flag (ZF) – set as a result of arithmetic or compare operations. A
nonzero result sets it to 0 and a zero result to 1. 0 means no (the result is
not equal to zero) and 1 means yes (the result equals zero).
• Sign Flag (SF) – set according to the sign (the high-order or leftmost bit)
after an arithmetic operation. Positive results set this to 0, negative
results set this to 1.
• Overflow Flag (OF) – set if an overflow occurs (results to operations that
cannot be stored in the limited space of a register)
Short,near,and far addresses
1- A short address, limited to a distance of -128 to 127 bytes

2- A near address, limited to a distance of -32,768 to 32,767 bytes

3- A far address, which may be within the same segment at a distance


over 32K or in other segment

SHORT NEAR FAR


JMP YES YES YES
JXXX(conditional jump) YES YES NO
LOOP YES NO NO
CALL NlA YES YES
Unconditional Jumps - The JMP Instruction
• The JMP (jump) instruction causes an unconditional transfer of
control (unconditional jump).

• Syntax:
JMP destination
JMP SHORT/NEAR/FAR address

•Example

JMP L10
……..

L10: INC CX
Unconditional Jumps - The JMP Instruction
•Backward and Forward jumps

Backward:

L10:
…….
JMP L10

Forward:
JMP L10
…….
L10:
The CMP Instruction
• The jump condition is often provided by the CMP (compare)
instruction

• Syntax:
CMP destination, source

• Compares by computing destination contents minus source contents.

• The result is not stored, but the flags are affected.

• Destination may not be a constant.

• CMP is just like SUB, except that destination is not changed.


Conditional Jumps
• Syntax
Jxxx destination_label

• Example
JNZ PRINT_LOOP

• If the condition for the jump is true, the next instruction to be


executed is the one at destinaltion_label (PRINT_LOOP), which
may precede or follow the jump instruction itself.

• If the condition is false, the instruction immediately following the


jump is done next.

• For JNZ, the condition is that the result of the previous operation is
not zero.
Conditional Jumps
• Signed Jumps: used for signed interpretations.

Symbol Description Condition for Jumps


JG/JNLE jump if greater than ZF = 0 & SF = OF
jump if not less than or equal
JGE/JNL jump if greater than or equal SF = OF
jump if not less than
JL/JNGE jump if less than SF <> OF
jump if not greater than or equal
JLE/JNG jump if less than or equal ZF = 1 or SF <> OF
jump if not greater than
Conditional Jumps
• Unsigned Jumps: used for unsigned interpretations.

Symbol Description Condition for Jumps


JA/JNBE jump if above CF = 0 & ZF = 0
jump if not below or equal
JAE/JNB jump if above or equal CF = 0
jump if not below
JB/JNAE jump if below CF = 1
jump if not above or equal
JBE/JNA jump if below or equal CF = 1 or ZF = 1
jump if not above
Conditional Jumps
• Single Flag Jumps: operates on settings of individual flags.

Symbol Description Condition for Jumps


JE/JZ jump if equal/ jump if equal to 0 ZF = 1
JNE/JNZ jump if not equal/ jump if not 0 ZF = 0
JC jump if carry CF = 1
JNC jump if no carry CF = 0
JO jump if overflow OF = 1
JNO jump if no overflow OF = 0
JS jump if sign negative SF = 1
JNS jump if nonnegative sign SF = 0
JP/JPE jump if parity even PF = 1
JNP/JPO jump if parity odd PF = 0
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:
LOOP SHORT address

• 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
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.

• 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.

• 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 bypassed 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)

You might also like