Skip to main content
Engineering LibreTexts

7.6: Nested Code Blocks

  • Page ID
    27139
  • \( \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}}\)

    It is common in most algorithms to have nested code blocks. A simple example would be a program which calculates the sum of all values from 0 to n, where the user enters values for n until a -1 if entered. In addition there is a constraint on the input that only positive values of n be considered, and any negative values of n will produce an error.

    This program consists of: a sentinel control loop, to get the user input; an if statement, to check that the input is greater than 0; and a counter control loop. The if statement is nested inside of the sentinel control block, and the counter loop is nested inside of the if-else statement. Now the important of being able to structure this program using pseudo code, and to translate the pseudo code into assembly, becomes important.

    The pseudo code for this algorithm follows. This pseudo code can be implemented in any HLL if the reader wants to assure themselves that it works, but it is fairly straight forward and should be easy to understand.

    int n = prompt("Enter a value for the summation n, -1 to stop"); 
    while (n != -1)
    {
        if (n < -1) 
        {
            print("Negative input is invalid");
        }
        else 
        {
            int total = 0
            for (int i = 0; i < n; i++)
            {
                total = total + i;
            }
            print("The summation is " + total);
        }
    }
    

    The program to implement this pseudo code is much larger and more complex. Implementing the program without first producing the pseudo code and translating it to assembly, even for a relatively simple algorithm such as this, is difficult and often yields unfavorable results (see the exercises at the end of the chapter).

    However the translation of this pseudo code into assembly is a relatively straight forward process, as will be illustrated here.

    1. Begin by implementing the outer most block, the sentinel control block. Your code should look similar to the following:
      # Sentinel Control Loop
      la $a0, prompt
      jal PromptInt
      move $s0, $v0
      start_outer_loop:
          sne $t1, $s0, -1
          beqz $t1, end_outer_loop
          
          # code block
          
          la $a0, prompt
          jal PromptInt
          move $s0, $v0
          b start_outer_loop
      end_outer_loop:
      .data
          prompt: .asciiz "Enter an integer, -1 to stop: "
      
    2. The code block in the sentinel loop in the above fragment is now replaced by the if-else statement to check for valid input. When completed, your code should look similar to the following:
      # Sentinel Control Loop
      la $a0, prompt
      jal PromptInt
      move $s0, $v0
      start_outer_loop:
          sne $t1, $s0, -1
          beqz $t1, end_outer_loop
          
          # If test for valid input 
          slti $t1, $s0, -1
          beqz $t1, else
              #if block
              b end_if
          else:
              #else block
          end_if:
          
          la $a0, prompt
          jal PromptInt
          move $s0, $v0
          b start_outer_loop
      end_outer_loop:    
      
    3. The if block in the above code fragment is replaced by the error message, and the else block is replaced by the sentinel control loop. This entire code fragment is then placed in the program, resulting in the following complete program.
      Program 7-9: Program illustrating nested blocks
      
      .text
          # Sentinel Control Loop 
          la $a0, prompt
          jal PromptInt
          move $s0, $v0 
          start_outer_loop:
              sne $t1, $s0, -1
              beqz $t1, end_outer_loop
              
                      # If test for valid input slti $t1, $s0, -1
                      beqz $t1, else
                      la $a0, error
                      jal PrintString
                          b end_if
                      else:
                          # summation loop
                          li $s1, 0
                          li $s2, 0 # initialize total
                  
                          start_inner_loop:
                              sle $t1, $s1, $s0
                              beqz $t1, end_inner_loop
                      
                              add $s2, $s2, $s1
                      
                              addi $s1, $s1, 1
                              b start_inner_loop
                          end_inner_loop:
                          la $a0, output
                          move $a1, $s2
                          jal PrintInt
                  end_if:
              la $a0, prompt
              jal PromptInt
              move $s0, $v0
              b start_outer_loop
          end_outer_loop:
          jal Exit      
      .data
          prompt: .asciiz "\nEnter an integer, -1 to stop: "
          error:  .asciiz "\nValues for n must be > 0"    
          output: .asciiz "\nThe total is: "
      .include "utils.asm"    
      

    Though somewhat long, this assembly code is straight forward to produce, and relatively easy to follow, particularly if the documentation at the start of the program includes pseudo code for the algorithm that was used.


    This page titled 7.6: Nested Code Blocks 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?