Link to home
Start Free TrialLog in
Avatar of varBoomer
varBoomer

asked on

Is it possible to create a dynamic array in MIPS?

Hi experts,

My homework assignment is to take a number of base 10 from the user and convert it into a base of their choice.    I've been testing my code with just base 2 right now, and I'm suppose to store the intermediate values into an array.  For example:

Take 49 of base 10 and convert it to its equivalent in base 2.  49 = 110001

Since there are a total of 6 digits, I would store it in a .space 24.  But since the number of digits will vary depending on the number the user inputs, I don't know what size of an array to make, so I thought I would try to create, in a sense, a dynamic array.

When I know the size of the array I put
.data
array: .space 24

But, of course, this time I don't know the size.

My homework isn't about arrays, per se.  It's actually about functions, this is just the approach I'm taking.

Since it's getting late here, if I don't respond to your comments this evening I will tomorrow afternoon.

Thank you!

This is the code that I have so far (I hope it and my comments are readable!):

li $v0, 5				# input x
	syscall
	move $t0 $v0				# y = x
	
	li $s1 2				# test instruction
	mul $t1 $s1 2				# A = base times base
	add $t9 $t9 1				# slot counter
bGetSlotLoop:
	add $t1 $t1 $t1				# B = A^N
	add $t9 $t9 1				# slot counter
	ble $t1 $t0 bGetSlotLoop
	
	mul $t3 $t9 4                # arraySize = slot counter * word
	li $v0, 9
	move $a0, $t3
	syscall
	move $s0 $v0
	
	
	li $t1 0
	li $t3 0
bDivBase:
	div $t2 $t0 2		     # newDividend = nextDividend / newBase 
	mflo $t6		     
	mul $t7 $t6 2		     # newProduct = hiQuotient times newBase
	sub $t8 $t0 $t7		     # newNumber = oldDividend - newProduct
	sw $t8 0($t3)		     # newNumber[i] = remainder of quotient
	                             # ///This is where I know my problem is.   
        addi $t3 $t3 4		     
	move $t0 $t2
	bgt $t6 0 bDivBase           # while quotient > 0, divide by new base

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

The simplest approach would be to iterate a multiplication of the target base until the power exceeds the base10 value.

In your example, you would keep multiplying your power of two by two until it exceeded 49.  The number of iterations (maybe minus 1) is the number of digit positions you will need.
1 - your initial value
2
4
8
16
32
64 -- you can stop looping, since you can represent 49 with fewer 'digits'

if you ignore the 64, you have your 6 digit positions.  Even if you retained this position, it would always be zero (leading zero).
Avatar of varBoomer
varBoomer

ASKER

That's exactly what I'm trying to do, but that's just to get the size of the array I need.  I still don't know how to make the program decide how big to make the array.  Like, I could make a default size array to be any where from .space 24 to .space 40, but if the number the user enters is smaller than that space it will have leading zeros (because I will be reading the array backwards from memory) in the output, or if the number is bigger than the space I designate it could cause some errors, so I'm trying to figure out a way to have the program, depending on the power of the new base, create the size of the array.  I think it has something to do with the $sp register.  I'm still digging to see if this register will help me out in some way, but it's been tough going.

I can't help you with dynamic memory allocation.  It has been too long since I've done assembler programming and it wasn't on a MIPS system.  Such a dynamic memory allocation will likely require a system call of some sort.  Once you know how many characters you will need, you will use that value to allocate that many bytes.
ok, thanks.  I can't see myself needing this much in the future either, so I'm not surprised I can't find much information on it.  If I don't get another response in a day or two I'll distribute points for at least trying to help.
SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you don't get a solution after the Request Attention, you can request that this question be deleted.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial