Skip to main content
Engineering LibreTexts

7.6: Nested Code Blocks

  • Page ID
    27139
  • 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.

    • Was this article helpful?