sections in this module City College of San Francisco - CS270
Computer Architecture

Module: MIPS-III
module list

Problems-MIPSIII

The code for each of these problems is in the online/mipsIII directory. Note: each of the programs here has a main() function that calls them. That function, which is provided, uses the stack. For now, you will just be writing the leaf functions (below), but you should look over the main functions for each to see how the stack is used. We will cover non-leaf functions in the next module.

arradd.s

int arradd(int result[], int arr1[], int arr2[], int nelem)

This function adds two arrays, placing the sum of each corresponding element in a third (result) array.

The code for arradd:

#
# arradd.s - add two arrays and store
#    the sums of each pair of elements
#    in a parallel third array
#
#  int arradd(int *result, int *arr1, int *arr2, int nelem);
#
#  returns 0 (fail) iff one of result, arr1 or arr2 is NULL
#
    .text
    .globl arradd
arradd:
    # uses one S register and needs room for ra and its arguments.
    li      $v0,0       # default failure status
    # fail if any pointer is NULL or if nelems is <= 0
    beq     $a0,$zero,.Larrbad
    beq     $a1,$zero,.Larrbad
    beq     $a2,$zero,.Larrbad
    ble     $a3,$zero,.Larrbad
    # finally ready to start
    li      $t7,0       # index
.Larrloop:
    beq     $t7,$a3,.Larrdone   # if index == nelems we are done
    sll     $t4,$t7,2   # convert index to nbytes
    addu    $t0,$a1,$t4 # &arr1[index]
    lw      $t1,0($t0)  # arr1[index]
    addu    $t0,$a2,$t4 # &arr2[index]
    lw      $t2,0($t0)  # arr2[index]
    add     $v0,$t1,$t2
    addu    $t0,$a0,$t4 # & result[index]
    sw      $v0,0($t0)  # result[index] = $v0
    addi    $t7,$t7,1   # index++
    b       .Larrloop
.Larrdone:
    li      $v0,1
.Larrbad:
    jr      $ra

arradd_all.s, which includes a copy of arradd.s, calls it with sample arrays. The code for arradd.s and arradd_all.s is in the mipsIII directory.

encodertype

Our next example is a function to encode an R-type instruction, given its parts. It has the prototype

unsigned encodertype(int opc, int rs, int rt, int rd, int shamt, int funct)

It is, of course, a leaf.

#
# unsigned encodertype(int opc, int rs, int rt, int rd, int shamt, int funct)
#
# on input, opc, rs, rt, rd are in $a0..$a4
#    shamt, funct are on the stack
#
# we will use $t0 for the resulting instruction
#    and $t1,$t2 for shamt, funct
# we will NOT assume that the inputs are in the correct range: they will
# be masked!
#
    .globl  encodertype
encodertype:
    li  $t0,0       # start with result == 0
    and $a0,0x3f    # opc is 6 bits
    add $t0,$a0,$zero   # result is just opc
#
    sll $t0,$t0,5   # make room for rs
    and $a1,0x1f    # mask rs
    or  $t0,$t0,$a1 # and or in rs
#
    sll $t0,$t0,5   # make room for rt
    and $a2,0x1f    # mask rt
    or  $t0,$t0,$a2 # and or in rt
#
    sll $t0,$t0,5   # make room for rd
    and $a3,0x1f    # mask rd
    or  $t0,$t0,$a3 # and or in rd
#
    sll $t0,$t0,5   # make room for shamt
    lw  $t1,16($sp)  # get shamt
    and $t1,0x1f    # mask shamt
    or  $t0,$t0,$t1 # and or in shamt
#
    sll $t0,$t0,6   # make room for funct
    lw  $t2,20($sp)  # get funct
    and $t2,0x3f    # mask funct
    or  $t0,$t0,$t2 # and or in funct
#
#   DONE!
    move    $v0,$t0
    jr      $ra

The example code for encodertype.s is in the online/mipsIII directory. The program includes a main function that requests each field from the user, then encodes the instruction and outputs it. No checking is done to ensure it is a valid R-type instruction, however.


Prev This page was made entirely with free software on linux:  
Kompozer
and Openoffice.org    
Next

Copyright 2015 Greg Boyd - All Rights Reserved.