Reputation: 1
I'm writing a program in assembly that simulates multiplication via iterative addition. I have the algorithm done, and for my first test case, I set my multiplicand to 50. Upon transferring this value, stored in $a0 and transferred to $s0, I get 000001001110 (78) instead of 000000110010 (50). For some reason, an extra 28 is added, and I can't seem to figure out where in my code this is happening. I'll paste all the relevant code, I'm at a loss here and would appreciate any help with streamlining or any oversights I've made. I also do use macros that I've defined in another file, simply for easy printing.
My test file:
.globl main
main:
TEST1:
li $a0, A
li $a1, BBB
li $a2, 6 # n: number of bits
jal ex.3.12
exit:
li $v0, 10
syscall
my file for 3.12 (the first version of MIPS multiplication by addition)
.include "./cs47.macro.asm"
.globl ex.3.12
.text
li $a0, 6
li $a1, 7
li $a2, 4
jal ex.3.12
print_reg ($s4)
exit
ex.3.12: #
addi $sp, $sp, -24
sw $ra, ($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
jal display.title # xxx
print_str_literal ("Step Action Multiplier Mutiplicand Product\n")
move $s0, $a0 # A = multiplicand
subi $s0, $s0, 28
move $s1, $a1 # B = multiplier
move $s2, $a2 # N = # bits for display, assume N <= 16
li $s4, 0 # product
# xxx print out the initial vals
print_str_literal ("0 Inital Vals " )
print_triple ($s1, $s0, $s4) # xxx print multiplier multiplicand product
print_newline ()
loop:
beq $s2, $0, done # if index $s2==0 quit loop
# xxx print out step numbers
sub $t0, $a2, $s2 # xxx t0 (step number) = N - s2 (index)
addi $t0, $t0, 1 # xxx t0 --
print_reg ($t0) # xxx t0 (step number) = N - 1 - s2 (index)
andi $s3, $s1, 1 # s3 = right most bit of multiplier s1.
beq $s3, 0, ZERO # if s3 == 0, then jump to ZERO
beq $s3 1, ONE
ONE: # addition happens
addu $s4, $s4, $s0 # if s3 == 1, s4 += s0 (multiplicand)
print_str_literal (" Prod=Prod+Mcand ")
print_triple($s1,$s0,$s4)
j ABC
ZERO : # no addition
print_str_literal (" lsb=0, no op " )
print_triple($s1, $s0, $s4)
j ABC
ABC:
# xxx
sll $s0, $s0, 1 # shift A (multiplicand) 1 bit to the left
print_reg ($t0)
print_str_literal(" Lshift Mcand ")
print_triple($s1, $s0, $s4)
srl $s1, $s1, 1 # shift B (multiplier) 1 bit to the right
print_reg ($t0)
print_str_literal(" Rshift Mplier ")
print_triple($s1, $s0, $s4)
print_newline ()
addi $s2, $s2 -1
j loop
done:
move $t0, $s4
print_str_literal("\nResult of A*B by iterative addition --> ")
print_reg($t0)
lw $ra, ($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
addi $sp, $sp, 24
jr $ra
The expected first line of results:
***********************************************************
A=50 B=10 N=6
************************************************************
Step Action Multiplier Multiplicand Product
0 Initial Vals 001010 000000110010 000000000000
The first line of results that I get without subtracting 28:
************************************************************
A=50 B=10 N=6
************************************************************
Step Action Multiplier Mutiplicand Product
0 Initial Vals 001010 000001001110 000000000000
Any help or insights would be greatly appreciated, thank you
Upvotes: 0
Views: 39