100 likes | 178 Views
Software Constructs. What they are …. The “IF THEN” construct is used when a sequence of instructions must be executed only if a condition is satisfied. Pseudo-Code: IF (Condition = True) THEN BEGIN Statement1 . . . . . . . . . StatementN END Assembly:
E N D
Software Constructs What they are ….. Sofware Constructs
The “IF THEN” construct is used when a sequence of instructions must be executed only if a condition is satisfied. Pseudo-Code: IF (Condition = True) THEN BEGIN Statement1 . . . . . . . . . StatementN END Assembly: CMP AL,Condition JNE Cont Statement1 . . . . . . . . . StatementN Cont: . . . . . . . . . . The “IF THEN ….” Construct Flowchart Sofware Constructs
Write a procedure that increments the content of AH only if the content of AL is equal to 50H. IF THEN :- Example 1 Sofware Constructs
Write a procedure that adds BL to AL. If the sum is greater than 255 then increment the contents of AH. IF THEN :- Example 2 Sofware Constructs
The “IF THEN …ELSE” construct is used when a sequence of instructions must be executed only if a condition is satisfied, while another sequence must be executed if the condition is not satisfied.. Pseudo-Code: IF (Condition = True) THEN BEGIN Statement1; Statement2; END ELSE BEGIN Statement1; Statement2; END Assembly: CMP AL,Contition JNE No Yes_Statements JMP Cont No: No_Statements Cont: . . . . . . . . . . The “IF THEN…ELSE” Construct Flowchart Sofware Constructs
CASE Structure • CASE structure is a multi-way branch instruction: CASE expression Values_1: Statements to perform if expression = values_1 Values_2: Statements to perform if expression = values_2 . . . Values_N: Statements to perform if expression = values N END_CASE Sofware Constructs
Conversion of Case Structure • Consider: CASE AX 1 or 2: Add 1 to BX 3 or 4: Add 1 to CX End_CASE Sofware Constructs
Conversion of Case Structure (cont’d) • FIRST THOUGHTBETTER CODE ;CASE AX ; CASE AX ;1 or 2 ; 1 or 2 CMP AX,1 CMP AX,1 JE ADD_BX JE ADD_BX CMP AX,2 CMP AX,2 JE ADD_BX JE ADD_BX ;3 or 4 ; 3 or 4 CMP AX,3 CMP AX,3 JE ADD_CX JE ADD_CX CMP AX,4 CMP AX,4 JE ADD_CX JNE END_CASE_AX JMP END_CASE_AX ADD_CX: ADD_BX: INC CX INC BX JMP END_CASE_AX ADD_BX: ADD_CX: INC BX INC CX END_CASE_AX: END_CASE_AX: Sofware Constructs
Branches with Compound Conditions • AND conditions (TRUE IF both conditions are true) • How do you implement: IF AX=1 AND BX=1 THEN ADD 1 TO CX END_IF • In AL we could code: ;IF AX=1 and BX=1 CMP AX,1 JNE END_IF CMP BX,1 JNE END_IF ;Then add 1 to CX INC CX END_IF: Sofware Constructs
Branches with Comp. Conditions (cont’d) • OR conditions (True if either condition is true, false IF both conditions are false) • How do you implement: IF AX=1 or BX=1 Then Add 1 to CX END_IF • In AL we could code: ; IF AX=1 or BX=1 CMP AX,1 JE ADD_CX CMP BX,1 JNE END_IF ADD_CX: INC CX END_IF: Sofware Constructs