Homework Answers: Memory Access

 

  1. lui    $t1, 0x1001
  2. Any of the following will work:
         addi    $t1, $t1, 0x0c
         addi    $t1, $t1, 12
         ori     $t1, $t1, 0x000c
  3.      lui     $t1, 0x1001
         ori     $t1, $t1, 0x002c
  1. 0x10010000
  2. 0x10010004
  3. 0x10010008
  4.      ori     $t1, $t0, 8
              -- or --
         addi    $t1, $t0, 8
  5.      sw      $t2, 4($t0)
  6.      lb      $t3, 11($t0)
  7.      lw      $t3, 4($t0)
         addi    $t3, $t3, 1
         sw      $t3, 4($t0)
  8.      lw      $t1, length
         lw      $t2, width
         mult    $t1, $t2
         mflo    $t3
         sw      $t3, area
  9.      lw      $a0, area
         li      $v0, 1
         syscall
  10.      lui     $t0, 0x1001
         lw      $t1, 0($t0)
         lw      $t2, 4($t0)
         mult    $t1, $t2
         mflo    $t3
         sw      $t3, 8($t0)
    
  11.      lui     $t0, 0x1001
         lw      $a0, 8($t0)
         li      $v0, 1
         syscall
  12.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of nums
    loop:
         li      $v0, 4                 # code to print string
         ori     $a0, $t0, 508          # addr of prompt
         syscall
         li      $v0, 5                 # code to read an int
         syscall
         sw      $v0, 0($t3)            # store the int in the array
         addi    $t3, $t3, 4            # move to next array element
         addi    $t2, $t2, 1            # increment loop counter
         blt     $t2, $t1, loop         # keep looping till $t2 == 100
    
  13.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of nums
    loop:
         li      $v0, 4                 # code to print string
         ori     $a0, $t0, 508          # addr of prompt
         syscall
         li      $v0, 5                 # code to read an int
         syscall
         beqz    $v0, done              # if user enters zero, stop reading
         sw      $v0, 0($t3)            # store the int in the array
         addi    $t3, $t3, 4            # move to next array element
         addi    $t2, $t2, 1            # increment loop counter
         blt     $t2, $t1, loop         # keep looping till $t2 == 100
    done:
         sw      $t2, 404($t0)          # store number of numbers entered in count
    
  14.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of current array element
         li      $v0, 1                 # syscall code to print int
    loop:
         bge     $t2, $t1, done
         lw      $a0, 0($t3)            # current array element in $a0
         syscall                        # print current array element
    
         addi    $t2, $t2, 1            # increment loop counter
         addi    $t3, 4                 # move down to next array element
         b       loop
    done:
    
  15.      lui     $t0, 0x1001
         li      $t1, 50                # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of current array element
         li      $v0, 1                 # syscall code to print int
    loop:
         bge     $t2, $t1, done
         lw      $a0, 0($t3)            # current array element in $a0
         syscall                        # print current array element
    
         addi    $t2, $t2, 1            # increment loop counter
         addi    $t3, 8                 # move down two array elements
         b       loop
    done:
    
  16.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of current array element
         lw      $t4, 0($t0)            # first element is largest so far
    loop:
         bge     $t2, $t1, done
         lw      $t5, 0($t3)            # current array element in $t5
         ble     $t5, $t4, nobig        # check for bigger than so far
         move    $t4, $t5               # save new biggest
    nobig:
         addi    $t2, $t2, 1            # increment loop counter
         addi    $t3, 4                 # move to next array element
         b       loop
    done:
         li      $v0, 1
         move    $a0, $t4
         syscall                        # print largest
    
  17.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         move    $t3, $t0               # addr of first array element
         add     $t3, 396               # addr of last array element
         li      $v0, 1                 # syscall code to print int
    loop:
         bge     $t2, $t1, done
         lw      $a0, 0($t3)            # current array element in $a0
         syscall                        # print current array element
    
         addi    $t2, $t2, 1            # increment loop counter
         addi    $t3, $t3, -4           # move back to prev array element
         b       loop
    done:
    
  18. lui $t0, 0x1001 li $t1, 100 # loop limit move $t2, $0 # loop counter move $t3, $t0 ori $t3, $t3, 408 # $t3 is addr of mychars loop: li $v0, 4 # code to print string ori $a0, $t0, 525 # addr of prompt syscall li $v0, 12 # code to read a char syscall sw $v0, 0($t3) # store the int in the array addi $t3, $t3, 1 # move to next array element addi $t2, $t2, 1 # increment loop counter blt $t2, $t1, loop # keep looping till $t2 == 100
  19.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         addi    $t3, $t0, 408          # addr of mychars
    loop:
         li      $v0, 11                # code to print a char
         lbu     $a0, 0($t3)            # load char from array into $a0
         syscall
         addi    $t3, $t3, 1            # move to next array element
         addi    $t2, $t2, 1            # increment loop counter
         blt     $t2, $t1, loop         # keep looping till $t2 == 100
    
  20.      lui     $t0, 0x1001
         li      $t1, 100               # loop limit
         move    $t2, $0                # loop counter
         addi    $t3, $t0, 408          # addr of mychars
         li      $t4, 0x61              # ascii code for 'a'
         ori     $t5, $0, 0             # $t5 = 0, count of a's
    loop:
         lbu     $t6, 0($t3)            # load array element
         bne     $t6, $t4, noa          # jump around if char is not a
         addi    $t5, $t5, 1            # add to number of a's
    noa:
         addi    $t3, $t3, 1            # move to next array element
         addi    $t2, $t2, 1            # increment loop counter
         blt     $t2, $t1, loop         # keep looping till $t2 == 100
    
         move    $v0, 1                 # code to print an int
         move    $a0, $t5               # print num of a's
         syscall
    

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

© Copyright Emmi Schatz 2012