Thursday, December 24, 2009

Assembly Language Part 5

Looping instructions in Assembly


What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point.

In assembly a loop is a sequence of instructions that is repeated like the loop to the left.
The final instruction (Tail) jumps (touches) to the first instruction (Head).

The basic form of looping in Assembly is

REPEAT UNTIL
REPEAT
      STATEMENTS
UNTIL CONDITION

For example if we want to read all input characters given by the user until he/she inputs

    MOV AH,1
REPEAT_:
    INT 21H
    CMP AL,' '
    JNE REPEAT_ 

Some other frequently used looping structures are-


FOR LOOP
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -

FOR loop_count times DO
     statements
END_FOR

In assembly we use the instruction LOOP to implement for loop. The general form is-

LOOP header_label

Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1. 

Let us implement it in the program to print 100 '*' in assembly.

MOV CX,80
MOV AH,2
MOV DL,'*'

TOP:                            ; header_label
   INT 21H
   LOOP TOP              



WHILE LOOP
These kind of loops depends on a condition. pseudocode -


WHILE   condition DO
   statements
END_WHILE

The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false  the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.

Lets count the number of character in the input line.

MOV DX,0
MOV AH,1
INT 21H

WHILE_:
     CMP AL,13
     JE END_WHILE
     INC  DX
     INT 21H
     JMP WHILE_

END_WHILE:


The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.

Further reading:
Assembly Language Programming and Organization of IBM PC
                                                                    Ytha Yu
                                                                    Charles Marut
http://www.cs.princeton.edu/

No comments:

Post a Comment

- - Important Notice - -





Programming google group is up. If you want to join request personally to any of the managers!




Education Blogs - BlogCatalog Blog Directory

Message from DIUCSP




This blog is open for all students of CS, CSE, CIS of Daffodil International University.


Join This blog to stay on touch with other students. If you have a gmail account then just sign in as the g-mail user and become a follower of the blog. You can then submit comments on our posts.

You will find information on different subjects of our course, discuss problems and have solution. Try to maintain the subjectwise discussion for early response. And remember this blog is not only for the genius or more than average CS, CSE, CIS students with great computer skills so dont feel shy in submitting your simple daily life problems (even the silliest ones) because I believe if you feel shy in learning now then you will feel shy all your life for not learning it at all.


Please be conscious about sharing your personal Data, DaffodilCSE blog moderator is not responsible for any risk you take, If you feel safe post comment as your own will, If you do not want to share your mail, comment anonymously. Thanks




They are with us

BlogCatalog

Earn from a cool looking gadget..! It works with Google adsense and any other advertising programs!
Grab now !