go to previous page   go to home page   go to next page

Answer:

        # create the remaining nodes in a counting loop
        li      $s2, 2          # counter = 2
        li      $s3, 8          # upper limit
        
loop:   bgtu    $s2, $s3,done   # while (counter <= limit )
        
        # create a node 
 
          .  .  .  .  .
          
        addi    $s2, $s2, 1      # counter++
        b       loop
        
done:
        # end the list

First Loop Iteration

Now the loop body has been added to the code. The job of the loop body is to attach a node to the current node. The first iteration of the loop adds the second node. Here is the picture just as the loop body starts, followed by the picture just as the loop body ends:

       
loop:   bgtu    $s2,$s3,done      # while (counter <= limit )
        
        # create a node 
        li      $v0,9             # allocate memory
        li      $a0,8             # 8 bytes
        syscall                   # $v0 <-- address
        
        # link this node to the previous
                                  # $s1 = &(previous node)
        sw      $v0,4($s1)        # copy address of the new node
                                  # into the previous node
        
        # make the new node the current node
        move    $s1,$v0
        
        # initialize the node
        sw      $s2,0($s1)        # save the counter as 
                                  # the current nodes data

        addi    $s2,$s2,1         # counter++
        b       loop
        
done:

Other than the fact that the code is inside a loop, this is just as before. Look at the code and the picture to see how they match.

QUESTION 13:

At the top of the loop, which is the current node?

At the bottom of the loop, which is the current node?