Skip to main content
Engineering LibreTexts

7.2: Simple If Statements

  • Page ID
    27135
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This section will deal with simple if statement, e.g. if statements that do not have any else conditions. This section will give two examples. The first shows how to translate a pseudo code program with a single logical condition in the if statement. The second shows how to handle complex logical conditions.

    7.2.1 Simple if statements in pseudo code

    This section will begin with a small example of an if statement.

    if (num > 0) 
    {
        print("Number is positive")
    }
    

    This program fragment looks simple, but as we have already seen there is a lot of complexity hidden in it. First, the variable num will not be directly useable in the program, and will have to be loaded into a register, and the subprogram for print must be created. But hidden in this program fragment are several conditions that are important in understanding the if statement.

    1. The statement (num > 0)is a statement in which the > operator is returning a logical (boolean) value to be evaluated. It might be easier if the statement was written as follows, which has exactly the same meaning.
      boolean flag = num > 0;
      if (flag) ...
      

      There is no mechanism for putting an expression in a branch statement, so the value of the boolean variable will have to be calculated before it will be used in assembly language, just as in this example.

      One peculiarity in assembly (as in C/C++) is that logical values are 0 (false) and anything else (true). This can lead to all sorts of strange consequences, so this behavior will not be allowed, and all boolean values will strictly be 0 (false) and 1 (true).

    2. Code blocks are the central organizing unit in this pseudo code. Any code between an open brace "{" and close brace "}" is considered part of a code block. Subprograms consist of a code block, and all control structures must use code blocks to implement the code within a condition.

    7.2.2 Simple if statement translated to assembly

    The assembly language program for the code fragment above is shown below.

    Program 7-1: A simple program to show an if statement
    
    .text
        # if (num > 0 )
        lw $t0, num
        sgt $t1, $t0, $zero     # $t1 is the boolean (num > 0)
        beqz $t1, end_if        # note: the code block is entered if
                                # if logical is true, skipped if false.
            # {
            # print ("Number is positive")
            la $a0, PositiveNumber
            jal PrintString
            # }
        end_if:
        jal Exit
    .data
        num: .word 5
        PositiveNumber: .asciiz "Number is positive"
    .include "utils.asm"
    

    In this code fragment comments are inserted to show how the pseudo code is translated into assembly. The following comments help to explain how this program works.

    1. The translation of (num > 0) takes 2 assembly instructions. The first loads num into $t1 so that the values can be used. The sgt $t1, $t0, $zero instruction loads the boolean value into $t1 so that it can be compared in the if test.
    2. The if test works by asking the question is the boolean value true? If the boolean value is true, then the code block is entered (the branch is not taken). If the test is false, branch to the end of the code block, and so the code block is not entered. This might appear backward, as the branch happens if the condition is false, but it is the easiest way to implement the logic (the exercises ask how to implement this if the branch is taken if the condition is true). The reader will quickly grow accustomed to this if used consistently, and doing things in a consistent manner is the best defense against annoying bugs.
    3. There are over 20 formats for the branch instruction in MARS. This text will only used 2. If the branch is based on a condition, the beqz will always be chosen. The seq, slt, sle, sgt, sge operators will be used to set the correct condition for the beqz operator. The only other branch used will be the unconditional branch instruction, b.
    4. When implementing a code block, the following will always be used. The final } for the code block will translate into a label. This label will be accessible from the branch statement which is determining whether or not to enter the code block. Thus the simple if code fragment above is translated by:
      1. calculating the boolean value to control entering the if statement code block.
      2. entering the code block if the boolean is true, or branching around it if it is false.

    7.2.3 Simple if statement with complex logical conditions

    While the example above showed how to translate a single logical condition, the question of how to translate complex logical conditions is more complex. Programmers might think that to translate a condition such as the one that follows requires complex programming logic.

    if((x > 0 && ((x%2) == 0)) # is x > 0 and even? 
    

    In fact one of the reasons programs became complex before structured programming became prevalent is that programmers would try to solve this type of complex logical condition using programming logic.

    The easy way to solve this problem is to realize that in a HLL, the compiler is going to reduce the complex logical condition into a single equation. So the complex if statement above would be translated into the equivalent of the following code fragment:

    boolean flag = ((x > 0) && ((x%2) == 0))
    if (flag)...
    

    This code fragment is easily translated into assembly language as follows:

    Program 7-2: Assembly logic for ((x > 0) && ((x%2) == 0))
    
    lw $t0, x
    sgt $t1, $t0, $zero
    rem $t2, $t0, 2
    and $t1, $t1, $t2
    beqz $t1, end_if
    

    Once again it is left as an exercise for the programmer to convince themselves that this is much easier than implementing the logical condition using logic and various branch statements.

    The true power of this method of handling logical conditions becomes apparent as the logical conditions become more complex. Consider the following logical condition:

    if((x > 0)&&((x % 2) ==0) && (x < 10)) #is 0 < x < 10 and even? 

    This can be translated into assembly language exactly as it appears in pseudo code.

    Program 7-3: Assembly language logic for ((x > 0) && ((x%2) == 0) && (x < 10))
     
    lw $t0, x
    sgt $t1, $t0, $zero
    li $t5, 10
    slt $t2, $t0, $t5
    rem $t3, $t0, 2
    and $t1, $t1, $t2
    and $t1, $t1, $t3
    beqz $t1, end_if
    

    This page titled 7.2: Simple If Statements is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III.

    • Was this article helpful?