Download - High-level language structures
![Page 1: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/1.jpg)
21/04/23 CAP221 1
High-level language structures
![Page 2: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/2.jpg)
21/04/23 CAP221 2
Branching structuresIF-THEN
• IF condition is true
THEN
execute true-branch statements
End_if
![Page 3: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/3.jpg)
21/04/23 CAP221 3
Branching structuresIF-THEN
false truecondition
True branchstatements
![Page 4: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/4.jpg)
21/04/23 CAP221 4
example
• Replace the number in AX by its absolute value.
• Pseudo code algorithm is
IF AX <0
Then
replace AX by –AX
END_IF
![Page 5: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/5.jpg)
21/04/23 CAP221 5
example
• Code:
CMP AX,0 ;AX<0?
JNL END_IF ;no, exit
NEG AX ;yes, change sign
END_IF:
![Page 6: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/6.jpg)
21/04/23 CAP221 6
IF_THEN_ELSE
• IF condition is true
THEN
execute true-branch statements
ELSE
execute FALSE-branch statements
End_IF
![Page 7: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/7.jpg)
21/04/23 CAP221 7
IF_THEN_ELSE
FALSE TRUECONDITION
TRUE BRANCHSTATEMENTS
FALSE BRANCHSTATEMENTS
![Page 8: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/8.jpg)
21/04/23 CAP221 8
CASE• A case is a multiway branch structure that
tests a register, variable, or expression for particular values or range of values. The general form is as follows:
case expression values_1: statements_1 values_2: statements_2 ………….. values_n: statements_n
END_CASE
![Page 9: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/9.jpg)
21/04/23 CAP221 9
CASE
EXPRESSION
STATEMENTS_1 STATEMENTS_2 STATEMENTS_N
VALUE_1 VALUE_2 VALUE_N…….
![Page 10: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/10.jpg)
21/04/23 CAP221 10
EXAMPLE
• IF AX contains a negative number, put -1 in BX; if AX contains 0, put 0 in BX; if AX contains a positive number, put 1 in BX.
![Page 11: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/11.jpg)
21/04/23 CAP221 11
example
• Pseudo code algorithm is
Case AX
< 0:put -1 in BX
= 0:put 0 in BX
> 0:put 1 in BX
END_CASE
![Page 12: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/12.jpg)
21/04/23 CAP221 12
ExampleCode: CMP AX,0 JL NEGATIVE JE ZERO JG POSITIVE NEGATIVE: MOV BX,-1 JMP END_CASE
ZERO: MOV BX,0 JMP END_CASEPOSITIVE: MOV BX,1END_CASE
•NOTE: only one CMP is needed, because jump instructions do not affect the flags.
![Page 13: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/13.jpg)
21/04/23 CAP221 13
example
• If AL contains 1 or 3, display “o”; if AL contains 2 or 4, display “e”.
• Pseudo code algorithm is
• Case AL
1, 3: display “o”
2, 4: display “e”
End_case
![Page 14: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/14.jpg)
21/04/23 CAP221 14
example
Code:CMP AL,1
JE ODD
CMP AL,3
JE ODD
CMP AL,2
JE EVEN
CMP AL,4
JE EVEN
JMP END_
ODD:
MOV DL, ’o’
JMP DIS_
EVEN:
MOV DL, ’e’
END_:
![Page 15: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/15.jpg)
21/04/23 CAP221 15
Branches with compound conditions
• Sometimes the branching condition in an IF or CASE takes the form
• Condition_1 and condition_2
• Condition_1 or condition_2
![Page 16: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/16.jpg)
21/04/23 CAP221 16
AND conditions
• 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.
![Page 17: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/17.jpg)
21/04/23 CAP221 17
Example
• Read a character, and if it’s an uppercase letter, display it.
• Pseudo code algorithm is
read a character (into AL)
If (‘A’ <= character) and (character <=‘Z’)
Then
Display character
End_if
![Page 18: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/18.jpg)
21/04/23 CAP221 18
example
• Code:
MOV ah,1
INT 21h
CMP al,’A’
JL end_if
CMP al,’Z’
JG end_if
MOV DL,AL
MOV AH,2
INT 21H
End_if:
![Page 19: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/19.jpg)
21/04/23 CAP221 19
OR conditions
• condition_1 OR condition_2 is true if at least one of the conditions is true.
• if both conditions are false, then the whole thing is false.
![Page 20: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/20.jpg)
21/04/23 CAP221 20
example
• Read a character. If it’s “y” or “Y”, display it; otherwise, terminate the program.
• Pseudo code algorithm is Read a character (into AL) if (character = “y” or character= “Y”) Then Display character Else Terminate the program End_if
![Page 21: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/21.jpg)
21/04/23 CAP221 21
example
MOV AH,1 INT 21h CMP AL, ’y’ JE THEN CMP AL,’Y’ JE THEN JMP ELSE_
THEN: MOV AH,2 MOV DL,AL INT 21H JMP END_IF ELSE_:MOV AH,4CHINT 21HEND_IF:
![Page 22: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/22.jpg)
21/04/23 CAP221 22
Looping structures
• A loop is a sequence of instructions that are repeated.
• For loop:
this is a loop structure in which the loop statements are repeated a known number of times (a count-controlled loop).
![Page 23: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/23.jpg)
21/04/23 CAP221 23
For loop_count times do statementsend_for
Initialize count
statements
Count = count - 1
Count=0False
True
![Page 24: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/24.jpg)
21/04/23 CAP221 24
LOOP instruction
• The LOOP instruction can be used to implement a FOR loop.
• General form:
LOOP des_label
The counter for the loop is the register CX.
![Page 25: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/25.jpg)
21/04/23 CAP221 25
LOOP instruction• CX is initialized to loop_count.• Execution of the LOOP instruction causes
CX to be decremented automatically.• If CX is not 0, control transfers to
des_label.• If CX =0, the next instruction after LOOP is
done.• Des_label must precede the LOOP
instruction by no more than 126 bytes.
![Page 26: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/26.jpg)
21/04/23 CAP221 26
LOOP instruction
• So, a FOR loop can be implemented as follows.
; initialize CX to loop_count
Des_label:
; body of the loop
loop des_label
![Page 27: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/27.jpg)
21/04/23 CAP221 27
example
• Write a count-controlled loop to display a row of 80 stars.
• Pseudo code algorithm is
For 80 times do
display ‘*’
end_for
![Page 28: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/28.jpg)
21/04/23 CAP221 28
example
MOV CX,80
MOV AH,2
MOV DL,’*’
TOP:
INT 21H
LOOP TOP
![Page 29: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/29.jpg)
21/04/23 CAP221 29
LOOP instruction
• A loop instruction is executed at least once.
• If CX = 0 when the loop is entered, the loop instruction causes CX to be decremented to FFFFH.
• So, the loop is executed 65535 more times.
![Page 30: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/30.jpg)
21/04/23 CAP221 30
LOOP instruction
• Solution:
use the instruction JCXZ (jump if CX is zero) before the loop.
JCXZ des_label
![Page 31: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/31.jpg)
21/04/23 CAP221 31
LOOP instruction
• A loop implemented as follows is bypassed if CX is 0.
JCXZ skip
top:
;body of the loop
loop top
Skip:
![Page 32: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/32.jpg)
21/04/23 CAP221 32
While Loop
• This loop depends on a condition.
• Pseudo code
while condition do
statements
end_while
![Page 33: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/33.jpg)
21/04/23 CAP221 33
While Loop
statements
conditionTrue
False
![Page 34: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/34.jpg)
21/04/23 CAP221 34
example• Write some code to count the number of
characters in an input line.
• Pseudo code
initialize count to 0.
read a character
while character <> carriage_return do
count=count + 1
read a character
end_while
![Page 35: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/35.jpg)
21/04/23 CAP221 35
example MOV DX,0 MOV AH,1 INT 21HWHILE_: CMP AL,0DH JE END_ INC DX INT 21H JMP WHILE_END_ :
![Page 36: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/36.jpg)
21/04/23 CAP221 36
Repeat loop
• Another conditional loop is the repeat loop. Pseudo code
repeat
statements
until condition
![Page 37: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/37.jpg)
21/04/23 CAP221 37
statements
condition
True
False
Repeat loop
![Page 38: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/38.jpg)
21/04/23 CAP221 38
example
• Write some code to read characters until a blank is read.
Pseudo code
Repeat
read a character
until character is a blank
![Page 39: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/39.jpg)
21/04/23 CAP221 39
example
MOV AH,1
REPEAT:
INT 21H
CMP AL,’ ’
JNE REPEAT
![Page 40: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/40.jpg)
21/04/23 CAP221 40
WHILE versus REPEAT
• In a while loop, the loop can be bypassed if the terminating condition is initially false.
• The statements in a repeat must be done at least once.
• Code for a repeat loop is likely to be 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.
![Page 41: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/41.jpg)
21/04/23 CAP221 41
Programming with high-level structures
• Top-down program design:
The original problem is solved by solving a series of sub problems, each of which is easier to solve than the original problem.
![Page 42: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/42.jpg)
21/04/23 CAP221 42
examplePrompt the user to enter a line of text.On the next line, display the capital letter
entered that comes first alphabetically and the one that comes last. If no capital letters are entered, display “No capital letter”.
sample execution: type a line of text: THE QUICK BROWN FOX JUMPED. first capital = B last capital = X
![Page 43: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/43.jpg)
21/04/23 CAP221 43
Top down program design
First refinement:
1. Display the opening message.
2. Read and process a line of text.
3. Display the results.
![Page 44: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/44.jpg)
21/04/23 CAP221 44
example TITLE PGM6_2 : FIRST LAST CAPITALS.MODEL SMALL.STACK 100H.DATAPROMPT DB 'Type a line of text ',0dH,0AH,'$'NOCAP_MSG DB 0DH,0AH,' No Capitals $'CAP_MSG DB 0DH,0AH,'FIRST CAPITAL = 'FIRST DB '[' DB ' LAST CAPITAL = 'LAST DB '@ $'
![Page 45: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/45.jpg)
21/04/23 CAP221 45
.CODEMAIN PROC; initialize DS MOV AX,@DATA MOV DS, AX; first step display opening message MOV AH,9 LEA DX,PROMPT INT 21H
Step 1
![Page 46: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/46.jpg)
21/04/23 CAP221 46
Step 2: read and process a line of text Read a character.While character is not a carriage return doIf character is a capital letter Then If character precedes first capital Then First capital = character End_if If character follows last capital Then Last capital = character End_ifEnd_ifRead a characterEnd_while
![Page 47: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/47.jpg)
21/04/23 CAP221 47
;read and process a line of text MOV AH,1 INT 21HWHILE_:; while character is not carriage ;return do CMP AL,0dH JE END_WHILE; if character is a capital letter CMP AL,'A' JNGE END_IF CMP AL,'Z' JNLE END_IF; then; if character precedes FIRST ;capital CMP AL,FIRST JNL CHECK_LAST
; then FIRST CAPITAL = ;character MOV FIRST,AL; end_ifCHECK_LAST :; if character follows last capital CMP AL,LAST JNG END_IF; then LAST CAPITAL = character MOV LAST,AL;end_ifEND_IF:; read a character INT 21H JMP WHILE_END_WHILE :
![Page 48: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/48.jpg)
21/04/23 CAP221 48
Step 3: display the results
if no capitals were typed
then
display “no capitals”
Else
display first capital and last capital
End_if
![Page 49: High-level language structures](https://reader035.vdocuments.mx/reader035/viewer/2022070405/56814015550346895dab5c27/html5/thumbnails/49.jpg)
21/04/23 CAP221 49
; display results MOV AH,9; if no capital were typed CMP FIRST, '[' JNE CAPS; then LEA DX,NOCAP_MSG JMP DISPLAYCAPS : LEA DX,CAP_MSGDISPLAY : INT 21H;end_if; DOS exit MOV AH,4CH INT 21HMAIN ENDP END MAIN