Problem Set: Conditionals and Loops

 

  1. Given the following code:
    	.data
    prompt:	.asciiz "enter a number: "
    	.globl	main
    	.text
    main:
    	li	$t1, 10
    	li	$t2, 5
    	li	$t3, 2
    	li	$v0, 4
    	la	$a0, prompt
    	syscall
    	li	$v0, 5
    	syscall
    	move	$t0, $v0
    	bgtz	$t0, one
    	add	$t3, $t1, $t2
    one:
    	addi	$t2, $t2, -2
    	mult	$t0, $t3
    	mflo	$t4
    
    1. If the user enters 3, what will be the values at the end of execution in the following registers: $t0, $t1, $t2, $t3, and $t4?
    2. If the user enters -4, what will be the values at the end of execution in the following registers: $t0, $t1, $t2, $t3, and $t4?
  2. Given the following code:
    	.data
    prompt:	.asciiz "enter a number: "
    	.globl	main
    
    	.text
    main:
    	li	$t2, 5
    	li	$t3, 4
    	addi	$t4, $zero, 3
    	li	$v0, 4
    	la	$a0, prompt
    	syscall
    	li	$v0, 5
    	syscall
    	move	$t0, $v0
    	li	$v0, 4
    	syscall
    	li	$v0, 5
    	syscall
    	move	$t1, $v0
    	blt	$t0, $t1, one
    	add	$t5, $t1, $t2
    	b	two
    one:
    	add	$t5, $t0, $t2
    two:
    	mult	$t3, $t5
    	mflo	$t6
    	div	$t6, $t4
    	mfhi	$t2
    	mflo	$t3
    
    1. If the user enters 5 and 2, what will be the values at the end of execution in the following registers: $t0, $t1, $t2, $t3, $t4, $t5, and $t6?
    2. If the user enters 3 and 8, what will be the values at the end of execution in the following registers: $t0, $t1, $t2, $t3, $t4, $t5, and $t6?

  3. What are the values in $t0, $t1, and $t2 at the end of execution of the following code?
    	li	$t0, 2
    	li	$t1, 15
    	addi	$t2, $zero, 0
    top:
    	bgt	$t0, $t1, end
    	add	$t2, $t2, $t0
    	addi	$t0, $t0, 2
    	b	top
    end:
    
  4. What are the values in $t0, $t1, $t2, $t3, $t4, $t5, $s0, and $s1 at the end of execution of the following code if the user enters: 10 5 15 6 10 20 9 3 18?
    	.data
    prompt:	.asciiz "enter a number: "
    	.globl	main
    
    	.text
    main:
    	li	$t0, 1
    	li	$t1, 3
    	li	$t3, 1
    	move	$t4, $zero
    	li	$v0, 4
    one:	bgt	$t0, $t1, six
    	move	$t5, $zero
    	li	$v0, 4
    	la	$a0, prompt
    	syscall
    	li	$v0, 5
    	syscall
    	move	$s0, $v0
    	addi	$t2, $zero, 0
    two:	bgt	$t2, $t3, five
    	li	$v0, 4
    	la	$a0, prompt
    	syscall
    	li	$v0, 5
    	syscall
    	move	$s1, $v0
    	blt	$s0, $s1, three
    	add	$t5, $t5, $s0
    	b	four
    three:	add	$t5, $t5, $s1
    four:	addi	$t2, $t2, 1
    	b	two
    five:	add	$t4, $t4, $t5
    	addi	$t0, $t0, 1
    	b	one
    six:
    
  5. Show the assembler instructions equivalent to each of the following. Be sure to show where each numbered block of code will be placed.

  6. if $t1 != $t2 && $t2 > $t5 {
       .
       . (1)
       .
    }
    else {
       .
       . (2)
       .
    }
    .
    . (3)
    .
    
    
  7. if ($t1 <= $t2 || $t3 > $t4) {
       .
       . (1)
       .
    }
    else {
       .
       . (2)
       .
    }
    .
    . (3)
    .
    
    
  8. if $t1 > $t2 + $t3 {
       .
       . (1)
       .
    }
    else if $t3 + $t1 == $t4 {
       .
       . (2)
       .
    }
    else {
       .
       . (3)
       .
    }
    .
    . (4)
    .
    
    
  9.    if $t1 <= $t2 {
          .
          . (1)
          .
          if $t3 != 0 {
             .
             . (2)
             .
          }
          else {
             .
             . (3)
             .
          }
       }
       else {
          .
          . (4)
          .
       }
       .
       . (5)
       .
    
    
  10. if $t1 >= $t2 {
       if $t0 < $t3 {
          .
          . (1)
          .
       }
       else {
          .
          . (2)
          .
       }
       .
       . (3)
       .
    }
    else {
       if $t0 != 10 {
          .
          . (4)
          .
       }
    }
    .
    . (5)
    .
    
    
  11. if $t1 <= $t2 + 1 {
       .
       . (1)
       .
    }
    else {
       if $t1 > $t2 || $t1 == 0 {
          .
          . (2)
          .
       }
       else {
          .
          . (3)
          .
       }
       .
       . (4)
       .
    }
    .
    . (5)
    .
    
    
  12. $t1 = 10
    while $t1 != 0 {
       $t2 = $t2 + $t3
       $t1 = $t1 - 1
    }
    
    
  13. $t3 = 0
    for ($t1 = 0 ; $t1 < $t2 ; $t1++) {
       print prompt: "Enter a num: "
       read num
       $t2 = num * num
       print $t2
    }
    
    
  14. $t2 = 0
    $t3 = 0
    for ($t1 = 0 ; $t1 < 100 ; $t1++) {
       print prompt: "Enter a num: "
       read num
       if num is even
          $t2 = $t2 + num
       else
          t3 = $t3 + num
    }
    print "Sum of evens: " $t2
    print "Sum of odds: " $t3
    
    
  15. xton = 1
    print prompt: "Enter a num: "
    read x
    while (x != 0) {
       print "Enter exponent: "
       read n
       for (i = 1 ; i <= n ; i++) {
          xton = xton * x
       }
       print "x^n = "
       print xton
       xton = 1
       print prompt: "Enter a num: "
       read x
    }
    
    

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2020