Looping Structures in Assembly Language

 
Assembly Language
 
Part VI
Looping Structures
 
Department of Computer Science, Faculty of Science, Chiang Mai University
 
Outline
 
Branches  with Compound Conditions
Looping Structures
Programming with High-Level Structures
 
204231: Computer Organization and Architecture
 
2
 
AND Conditions
 
condition_1 AND condition_2
An AND condition is true if and only if
condition_1 and condition_2  are both true.
If either condition is false, then the whole
thing is false.
 
204231: Computer Organization and Architecture
 
3
 
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
 
204231: Computer Organization and Architecture
 
4
 
Read a character, and
if it’s an uppercase letter, display it.
 
; 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:
 
204231: Computer Organization and Architecture
 
5
 
OR Conditions
 
condition_1 OR condition_2
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.
 
204231: Computer Organization and Architecture
 
6
 
Read a character, and if it is “y” or “Y”,
display it; otherwise, terminate the program.
 
Read a character (into AL)
IF (character = ‘y’) or (character = ‘Y’)
 
THEN
  
display it
 
ELSE
  
terminate the program
END_IF
 
204231: Computer Organization and Architecture
 
7
 
Read a character, and if it is “y” or “Y”,
display it; otherwise, terminate the program.
 
; read a character
  
MOV
 
AH, 1
  
; prepare to read
  
INT
 
21H
  
; char in AL
; if (character = ‘y’) or (character = ‘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
 
204231: Computer Organization and Architecture
 
8
 
Read a character, and if it is “y” or “Y”,
display it; otherwise, terminate the program.
 
THEN:
  
MOV
 
AH, 2
  
; prepare to display
  
MOV
 
DL, AL
 
; get char
  
INT
 
21H
  
; display it
  
JMP
 
END_IF
 
; end exit
ELSE_:
  
MOV
 
AH, 4CH
  
INT
 
21H
  
; DOS exit
END_IF:
 
204231: Computer Organization and Architecture
 
9
 
FOR LOOP
 
FOR  loop_count  times  DO
 
statements
END_FOR
 
204231: Computer Organization and Architecture
 
10
 
The LOOP instruction
 
  
; initialize CX to loop_count
 
TOP:
   
; body of the loop
   
LOOP TOP
 
204231: Computer Organization and Architecture
 
11
 
The LOOP instruction
 
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
.
If CX <> 0, control transfers to destination_label.
If CX = 0, the next instruction after LOOP is done.
destination_label must precede the LOOP
instruction by no more than 126 bytes.
 
204231: Computer Organization and Architecture
 
12
 
Write a count-controlled loop to
display a row of 80 stars.
 
  
FOR  80  times  DO
   
display ‘*’
  
END_FOR
 
  
MOV
 
CX, 80
  
; number of stars to display
  
MOV
 
AH, 2
  
; display character function
  
MOV
 
DL, ‘*’
  
; character to display
TOP:
  
INT
 
21h
  
; display a star
  
LOOP
 
TOP
  
; repeat 80 times
 
204231: Computer Organization and Architecture
 
13
 
The Instruction JCXZ (Jump If CX Is zero)
 
JCXZ
 
destination_label
If CX contains 0 when the loop is entered,
the LOOP instruction causes CX to be
decremented to FFFFh, and the loop is then
executed FFFFh = 65535 more times!
To prevent this, the instruction JCXZ may be
used before the loop.
 
204231: Computer Organization and Architecture
 
14
 
The Instruction JCXZ (Jump If CX Is zero)
 
  
JCXZ
 
SKIP
 
TOP:
   
; body of the loop
   
LOOP TOP
 
SKIP:
 
204231: Computer Organization and Architecture
 
15
 
WHILE LOOP
 
WHILE  condition  DO
 
statements
END_WHILE
 
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.
 
204231: Computer Organization and Architecture
 
16
 
Write some code to count the number
of characters in an input line.
 
Initialize count to 0
read a character
WHILE  character <> carriage_return  DO
 
count = count + 1
 
 read a character
END_WHILE
 
204231: Computer Organization and Architecture
 
17
 
Write some code to count the number
of characters in an input line.
 
  
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_:
 
204231: Computer Organization and Architecture
 
18
 
REPEAT LOOP
 
REPEAT
 
statements
UNTIL  condition
 
In a REPEAT … UNTIL  loop, the statements are
executed, and then the conditions is checked.
If 
true
, the 
loop terminates
; if false, control
branched to the top of the loop.
 
204231: Computer Organization and Architecture
 
19
 
Write some code to read characters
until a blank is read.
 
REPEAT
 
read a character
UNTIL  character is a blank
 
  
MOV
 
AH, 1
  
; prepare to read
REPEAT:
  
INT
 
21H
  
; char in AL
; until
  
CMP
 
AL, ‘ ‘
  
; a blank?
  
JNE
 
REPEAT
 
; no, keep reading
 
204231: Computer Organization and Architecture
 
20
 
Programming with High-Level Structures
 
  
Type a line of text:
  
THE QUICK BROWN FOR JUMPED.
  
First capital = B Last capital = W
 
204231: Computer Organization and Architecture
 
21
 
Read and Process a Line of Text
 
Read a character
WHILE character is not a carriage return DO
 
IF character is a capital letter (‘A’ <= character AND character <= ‘Z’)
  
THEN
   
IF character precedes first capital
    
THEN
 
first capital = character
   
END IF
   
IF character follows last capital
    
THEN
 
last capital = character
   
END IF
 
END IF
Read a character
END_WHILE
 
204231: Computer Organization and Architecture
 
22
 
Display the rResults
 
IF no capitals were typed,
 
THEN
  
display “No capitals”
 
ELSE
  
display first capital and last capital
END_IF
 
204231: Computer Organization and Architecture
 
23
 
Reference
 
Ytha Yu and Charles Marut, 
Assembly
Language Programming and Organization of
the IBM PC
. New York: McGraw-Hill, 1992.
 
204231: Computer Organization and Architecture
 
24
Slide Note
Embed
Share

Delve into the concepts of branches with compound conditions, AND and OR conditions, and handling uppercase letters and specific characters in Assembly Language through looping structures. Explore how to read characters, make comparisons, and display results based on the conditions met, providing a foundation in computer organization and architecture.

  • Assembly Language
  • Looping Structures
  • Computer Science
  • Computer Organization
  • Architecture

Uploaded on Jul 25, 2024 | 3 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Assembly Language Part VI Looping Structures Department of Computer Science, Faculty of Science, Chiang Mai University

  2. Outline Branches with Compound Conditions Looping Structures Programming with High-Level Structures 2 204231: Computer Organization and Architecture

  3. AND Conditions condition_1 AND condition_2 An AND condition is true if and only if condition_1 and condition_2 are both true. If either condition is false, then the whole thing is false. 3 204231: Computer Organization and Architecture

  4. 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 4 204231: Computer Organization and Architecture

  5. Read a character, and if it s an uppercase letter, display it. ; read a character MOV INT ; if ( A <= char) and (char <= Z ) CMP JNGE CMP JNLE ; then display char MOV MOV INT END_IF: AH, 1 21H ; prepare to read ; char in AL AL, A END_IF AL, Z END_IF ; char >= A ? ; no, exit ; char <= Z ? ; no, exit DL, AL AH, 2 21H ; get char ; prepare to display ; display char 5 204231: Computer Organization and Architecture

  6. OR Conditions condition_1 OR condition_2 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. 6 204231: Computer Organization and Architecture

  7. Read a character, and if it is y or Y, display it; otherwise, terminate the program. Read a character (into AL) IF (character = y ) or (character = Y ) THEN display it ELSE terminate the program END_IF 7 204231: Computer Organization and Architecture

  8. Read a character, and if it is y or Y, display it; otherwise, terminate the program. ; read a character MOV AH, 1 INT ; if (character = y ) or (character = Y ) CMP AL, y JE THEN CMP AL, Y JE THEN JMP ELSE_ ; prepare to read ; char in AL 21H ; char = y ? ; yes, go to display it ; char = Y ? ; yes, go to display it ; no, terminate 8 204231: Computer Organization and Architecture

  9. Read a character, and if it is y or Y, display it; otherwise, terminate the program. THEN: MOV AH, 2 MOV DL, AL INT 21H JMP END_IF ELSE_: MOV AH, 4CH INT END_IF: ; prepare to display ; get char ; display it ; end exit 21H ; DOS exit 9 204231: Computer Organization and Architecture

  10. FOR LOOP FOR loop_count times DO statements END_FOR 10 204231: Computer Organization and Architecture

  11. The LOOP instruction ; initialize CX to loop_count TOP: ; body of the loop LOOP TOP 11 204231: Computer Organization and Architecture

  12. The LOOP instruction LOOP 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. If CX = 0, the next instruction after LOOP is done. destination_label must precede the LOOP instruction by no more than 126 bytes. destination_label 12 204231: Computer Organization and Architecture

  13. Write a count-controlled loop to display a row of 80 stars. FOR 80 times DO display * END_FOR MOV CX, 80 MOV AH, 2 MOV DL, * ; number of stars to display ; display character function ; character to display TOP: INT LOOP TOP 21h ; display a star ; repeat 80 times 13 204231: Computer Organization and Architecture

  14. The Instruction JCXZ (Jump If CX Is zero) JCXZ If CX contains 0 when the loop is entered, the LOOP instruction causes CX to be decremented to FFFFh, and the loop is then executed FFFFh = 65535 more times! To prevent this, the instruction JCXZ may be used before the loop. destination_label 14 204231: Computer Organization and Architecture

  15. The Instruction JCXZ (Jump If CX Is zero) JCXZ SKIP TOP: ; body of the loop LOOP TOP SKIP: 15 204231: Computer Organization and Architecture

  16. WHILE LOOP WHILE condition DO statements END_WHILE 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. 16 204231: Computer Organization and Architecture

  17. Write some code to count the number of characters in an input line. Initialize count to 0 read a character WHILE character <> carriage_return DO count = count + 1 read a character END_WHILE 17 204231: Computer Organization and Architecture

  18. Write some code to count the number of characters in an input line. MOV DX, 0 MOV AH, 1 INT ; DX counts characters ; prepare to read ; character in AL 21H WHILE_: CMP AL, 0DH JE END_WHILE ; yes, exit INC DX INT 21H JMP WHILE_ END_WHILE_: ; CR? ; not CR, increment count ; read a character ; loop back 18 204231: Computer Organization and Architecture

  19. REPEAT LOOP REPEAT statements UNTIL condition In a REPEAT UNTIL loop, the statements are executed, and then the conditions is checked. If true, the loop terminates; if false, control branched to the top of the loop. 19 204231: Computer Organization and Architecture

  20. Write some code to read characters until a blank is read. REPEAT read a character UNTIL character is a blank MOV AH, 1 ; prepare to read REPEAT: INT 21H ; char in AL ; until CMP AL, JNE ; a blank? ; no, keep reading REPEAT 20 204231: Computer Organization and Architecture

  21. Programming with High-Level Structures Type a line of text: THE QUICK BROWN FOR JUMPED. First capital = B Last capital = W 21 204231: Computer Organization and Architecture

  22. Read and Process a Line of Text Read a character WHILE character is not a carriage return DO IF character is a capital letter ( A <= character AND character <= Z ) THEN IF character precedes first capital THEN first capital = character END IF IF character follows last capital THEN last capital = character END IF END IF Read a character END_WHILE 22 204231: Computer Organization and Architecture

  23. Display the rResults IF no capitals were typed, THEN display No capitals ELSE display first capital and last capital END_IF 23 204231: Computer Organization and Architecture

  24. Reference Ytha Yu and Charles Marut, Assembly Language Programming and Organization of the IBM PC. New York: McGraw-Hill, 1992. 24 204231: Computer Organization and Architecture

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#