# CS/COE 0447 - Spring 2009 - Sample problems for Exam 2 # # this program has a function to compute a 15-bit unsigned multiply with # an assembly implementation of the slow add-shift multiplier # .data mesg: .asciiz "The product was:" .text # set up multiplicand ($a0) and multiplier ($a1) li $a0,13 li $a1,39 jal _umul15 # print the product ($v0) la $a0,mesg move $a1,$v0 jal _print_result # terminate the program! li $v0,10 syscall # _umul15 - perform 15x15 bit unsigned multiplication # # desc: uses the "slow shift-add" written as assembly # args: $a0 holds multiplicand (M) # $a1 holds multiplier (R) # rets: $v0 holds the product # trash: $s0,$s1,$s2,$s3 are trashed # # note: this could easily do 16x16. what would you change below? # _umul15: move $v0,$0 # $v0 is product - initially cleared li $s1,15 # $s1 is number of steps is 15 move $s2,$a0 # $s2 is multiplicand M (shifted left) move $s3,$a1 # $s3 is multiplier R (shifted right) _umul15_loop: andi $s0,$s3,0x1 # get lsb(R) in $s0 beq $s0,$0,_umul15_shift # skip add when lsb(R)==0 addu $v0,$v0,$s2 # add M when lsb(R)==1 _umul15_shift: sll $s2,$s2,1 # shift M left by 1 srl $s3,$s3,1 # shift R right by 1 addi $s1,$s1,-1 # decrement step count bne $s1,$0,_umul15_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