# CS/COE 0447 - Spring 2009 - Sample problems for Exam 2 # # this program has a function to compute a 32-bit unsigned multiply with # an assembly implementation of the fast add-shift multiplier # .data mesg: .asciiz "The low half of product was:" .text # set up multiplicand ($a0) and multiplier ($a1) li $a0,10 li $a1,110 jal _umul32 # print the low half of the product ($v0) la $a0,mesg move $a1,$v0 jal _print_result # terminate the program! li $v0,10 syscall # _umul32 - perform 32x32 bit unsigned multiplication # # desc: uses the "fast shift-add" written as assembly # args: $a0 holds multiplicand (M) # $a1 holds multiplier (R) # rets: $v0 holds low bits of product (lowhalf(P)) # $v1 holds hi bits of product (hihalf(P)) # trash: $s0,$s1 are trashed # _umul32: move $v1,$0 # clear upper bits of product move $v0,$a1 # set lower bits to multiplier li $s1,32 # number of steps _umul32_loop: and $s0,$v0,0x1 # get lsb(P) in $s0 beq $s0,$0,_umul32_shift # skip add when lsb(P)==0 addu $v1,$v1,$a0 # add M when lsb(P)==1 _umul32_shift: srl $v0,$v0,1 # right shift lowhalf(P) sll $s0,$v1,31 # get lsb(hihalf(P)) into msb or $v0,$v0,$s0 # msb(lowhalf(P))=lsb(hihalf(P)) srl $v1,$v1,1 # right shift hihalf(P) addi $s1,$s1,-1 # decrement step count bne $s1,$0,_umul32_loop # cont until all steps done jr $ra # return # _print_result - prints string prompt, followed by a number # # desc: prints C-format string: "%s %d\n" # args: $a0 holds pointer to null-terminated string # $a1 holds the number # rets: none # trash: $s0, $s1 # .data _print_result_ws: .asciiz " " _print_result_nl: .asciiz "\n" .text _print_result: move $s0,$v0 move $s1,$a0 li $v0,4 syscall la $a0,_print_result_ws syscall move $a0,$a1 li $v0,1 syscall la $a0,_print_result_nl li $v0,4 syscall move $v0,$s0 move $a0,$s1 jr $ra