Computer and Information Sciences College Computer Science Department

  • Slides: 23
Download presentation
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization

Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language

Lecture 5 and Lecture 6 Chapter 7 –Program Logic and Control

Lecture 5 and Lecture 6 Chapter 7 –Program Logic and Control

Chapter Outline Ø Short, near and far address Ø JMP Instruction Ø The CMP

Chapter Outline Ø Short, near and far address Ø JMP Instruction Ø The CMP Instruction Ø Conditional Jump instruction Ø The Loop instruction Ø While Loop Ø REPEAT Loop

Short, Near and Far addresses 1 - A short address, limited to a distance

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 32 K or in other segment SHORT NEAR FAR JMP YES YES JXXX(conditional jump) YES NO LOOP YES NO NO CALL Nl. A YES

Unconditional Jumps - The JMP Instruction • The JMP (jump) instruction causes an unconditional

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 ……. . L 10: L 10 INC CX • Backward and Forward jumps L 10: ……. JMP L 10; Backward ; ; ; ; ; ; ; ; ; ; JMP L 10; Forward ……. L 10:

The CMP Instruction • The jump condition is often provided by the CMP (compare)

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.

Flags Carry flag Overflow Parity flag Direction Auxiliary flag Interrupt enable Trap Zero Sign

Flags Carry flag Overflow Parity flag Direction Auxiliary flag Interrupt enable Trap Zero Sign 6 are status/ conditional flags 3 are control flag

Flag Register • Conditional flags: – They are set according to some results of

Flag Register • Conditional flags: – They are set according to some results of arithmetic operation. You do not need to alter the value yourself. • Control flags: – Used to control some operations of the MPU. These flags are to be set by you in order to achieve some specific purposes. Flag Bit no. 15 14 13 12 O D I T S Z 11 10 9 8 7 6 A 5 4 P 3 2 C 1 0 • CF (carry) Contains carry from leftmost bit following arithmetic, also contains last bit from a shift or rotate operation. Example: (11110000)+(10100001) =10010001 SF =1 , CF=1 , ZF=0 , OF=0 , PF=0

Flag Register • OF (overflow) The overflow flag is set to 1 if the

Flag Register • OF (overflow) The overflow flag is set to 1 if the result of an arithmetic operation is too big for the microprocessor’s maximum word size, otherwise it is reset to 0 • DF (direction) Indicates left or right for moving or comparing string data. • IF (interrupt) Indicates whether external interrupts are being processed or ignored. • TF (trap) Permits operation of the processor in single step mode.

Flag Register • SF (sign) Contains the resulting sign of an arithmetic operation (1=negative;

Flag Register • SF (sign) Contains the resulting sign of an arithmetic operation (1=negative; 0=positive) • ZF (zero) Indicates when the result of arithmetic or a comparison is zero. (1= zero; 0= nonzero) • AF (auxiliary carry) Contains carry out of bit 3 into bit 4 for specialized arithmetic. • PF (parity) is set to 1 to indicate whether the result of the last operation contains either an even number of 1’s (even parity) or an odd number of 1’s (odd parity) depending on the microprocessor. .

Conditional Jumps • Syntax Jxxx destination_label • Example JNZ PRINT_LOOP • If the condition

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 instruction 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 JG/JNLE JGE/JNL JL/JNGE JLE/JNG

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

Conditional Jumps • Unsigned Jumps: used for unsigned interpretations. Symbol JA/JNBE JAE/JNB JB/JNAE JBE/JNA

Conditional Jumps • Unsigned Jumps: used for unsigned interpretations. Symbol JA/JNBE JAE/JNB JB/JNAE JBE/JNA Description jump if above jump if not below or equal jump if above or equal jump if not below jump if not above or equal jump if below or equal jump if not above Condition for Jumps CF = 0 & ZF = 0 CF = 1 or ZF = 1

Conditional Jumps • Single Flag Jumps: operates on settings of individual flags. Symbol JE/JZ

Conditional Jumps • Single Flag Jumps: operates on settings of individual flags. Symbol JE/JZ JNE/JNZ JC JNC JO JNO JS JNS JP/JPE JNP/JPO Description jump if equal/ jump if equal to 0 jump if not equal/ jump if not 0 jump if carry jump if no carry jump if overflow jump if no overflow jump if sign negative jump if nonnegative sign jump if parity even jump if parity odd Condition for Jumps ZF = 1 ZF = 0 CF = 1 CF = 0 OF = 1 OF = 0 SF = 1 SF = 0 PF = 1 PF = 0

IF-THEN-ELSE • Example: Suppose AL and BL contain extended ASCII characters. Display the one

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 CMP AL, BL ; AL <= BL? THEN JNBE ELSE_ ; no, display char in BL display the character in AL ; AL <= BL ELSE MOV DL, AL ; move char to be displayed display the character in BL JMP DISPLAY ; go to display END_IF ELSE_: ; BL < AL MOV DL, BL DISPLAY: MOV AH, 2 ; prepare to display INT 21 h ; display it

Branches with compound Conditions • Sometimes the branching condition in an IF or CASE

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 • 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. • Pseudocode: Read a character (into AL) IF ('A' <= character) and (character <= 'Z') THEN display character END_IF

AND Condition It can be coded as follows: ; read a character MOV AH,

AND Condition It can be coded as follows: ; read a character MOV AH, 1 ; prepare to read INT 21 h ; 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 21 h ; display char END_IF:

OR Condition • An OR condition is true if at least one of the

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,

OR Condition It can be coded as follows: ; read a character MOV AH, 1 INT 21 h ; prepare to read ; char in AL CMP JE JMP AL, 'y' THEN AL, 'Y' THEN ELSE_ ; char = 'y'? ; yes, go to display it ; char = 'Y'? ; yes, go to display it ; no, terminate MOV INT JMP DL, AL AH, 2 21 h END_IF ; get char ; prepare to display ; display char ; and exit ; if (char = 'y') or (char = 'Y') THEN: ELSE_: MOV AH, 4 Ch INT 21 h END_IF: ; DOS exit

Loop Instruction • The LOOP instruction can be used to implement a For loop.

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. TOP: ; initialize CX to loop_count ; body of the loop LOOP TOP

FOR Loop • Example: Write some code to display a row of 80 stars.

FOR Loop • Example: Write some code to display a row of 80 stars. • Solution: Pseudocode: ; what if CX =0? FOR 80 times DO display '*' MOV CX, 80 END_IF MOV AH, 2 It can be coded as follows: MOV DL, '*' MOV CX, 80 JCXZ SKIP ; jump if CX=0 MOV AH, 2 TOP: MOV DL, '*' INT 21 h TOP: LOOP TOP INT 21 h SKIP: LOOP TOP

WHILE Loop • This loop depends on a condition. • Pseudocode: MOV DX, 0

WHILE Loop • This loop depends on a condition. • Pseudocode: MOV DX, 0 ; DX counts characters WHILE condition DO MOV AH, 1 ; prepare to read statements INT 21 h ; character in AL END_WHILE_: • Pseudocode: CMP AL, 0 Dh ; CR? initialize count to 0 JE END_WHILE ; yes, exit read a character ; not CR, increment count WHILE INC DX character <> carriage_return DO INT 21 h ; read a character count = count + 1 JMP WHILE_ EN ; loop back read character D_WHILE: END_WHILE Example: Write some code to count the number of characters in an input line.

REPEAT Loop • This loop depends on a condition. • Pseudocode: REPEAT Statements UNTIL

REPEAT Loop • This loop depends on a condition. • Pseudocode: REPEAT Statements UNTIL conditions • Example: write code to read characters until a blank is read • Pseudocode: REPEAT Read character UNTIL character is blank MOV AH, 1 REAPEAT: INT 21 H CMP AL, ’ ‘ JNE REAPEAT