Thursday, December 24, 2009

Assembly Language Part 4

Conditional Branching in MASM
We have shown that the jump instructions can be used to implement branches and looops. However these instructions are difficult for beginners to apply as a substitute of high level branching statements.

In high level languages we use these basic structures -

IF-THEN
The if-then structure is the most common type of branching statement used. The pseudocode for IF-THEN is :


IF  CONDITION IS TRUE
     THEN EXECUTE TRUE BRANCH STATEMENTS
END_IF

MASM example:
Suppose we want to code the following pseudocode in MASM

IF AX< 0
    THEN REPLACE AX WITH ITS COMPLIMENTED VALUE
END_IF

The MASM code will be:

CMP AX,0
   JNL END_IF
   NEG AX

END_IF:

IF-THEN-ELSE
The if-then-else structure is the expansion of most common type of branching statement IF-THEN. The pseudocode for IF-THEN-ELSE is :

IF   Condition is TRUE
THEN
Execute true-branch statements
ELSE
Execute false-branch statements
END_IF

Lets explain by applying the following pseudocode in MASM

IF AL<= BL
  THEN
      DISPLAY THE CHARACTER IN AL
  ELSE
      DISPLAY THE CHARACTER IN BL
 END_IF

The MASM code will be -

MOV AH,2
CMP AL,BL
   JNBE ELSE_
   MOV DL,AL
   JMP DISPLAY_
ELSE_:
   MOV DL,BL
DISPLAY_:
   INT 21H

END_IF:


CASE
The case in high-level language is a multiway branch structure that tests a register, variable or expression for particular values or range of values. The pseudocode is :

CASE expression
    value_1: statement_1
    value_2: statement_2
    value_3: statement_3
    value_4: statement_4
    .................................
    value_n: statement_n
END_CASE

Consider the following pseudocode in MASM

IF AX a negative number
   THEN put -1 in BX
IF AX contains 0
   THEN put 0 in BX
IF AX contains a positive number
   THEN put 1 in BX

The MASM code will be -

CMP AX,0
   JL   NEGATIVE_
   JG   POSITIVE
 
   MOV BX,0
   JMP END_CASE

 NEGATIVE_:
   MOV BX,-1
   JMP END_CASE

POSITIVE_:
  MOV BX,1
  JMP END_CASE

END_CASE:


further reading:
http://www.cs.princeton.edu/

Assembly Language Programming and Organization of IBM PC
                                                                    Ytha Yu
                                                                    Charles Marut

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 !