Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array size known only at runtime in MIPS

So I have a board game and the user is expected to enter the size of the board 3,4,5 ...will be 3x3, 4x4, 5x5, etc...

Here:

board: .word 0:100  # declare a board of size 100 and make ints '0' , O = 1, X = 2

As you can see, this is static declaration...I need to somehow make an array the SIZE of the user input found in t0 for example...

like image 840
Amzraptor Avatar asked Oct 17 '13 20:10

Amzraptor


People also ask

How can you determine the size of an array at runtime?

So deciding an array size at runtime is possible in modern C (>= C99) and code like the below is fine: int s; printf("Enter the array size: "); scanf("%d", &s); int a[s]; One obvious drawback of VLAs is that if s is quite big and the allocation of a could fail.

Can we input the size of array at run time?

You can't. Arrays in C have a size that has to be specified when it's declared.

What is an array that varies in size during runtime?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time).


1 Answers

It sounds like you need to allocate some memory on the heap. The MARS emulator syscall for that is $v0 = 9, $a0 = number of bytes to allocate, returns address of allocated memory in $v0. Source: MIPS syscall functions available in MARS

So your steps would be:

  • Get the array size from the user
  • Square it
  • Make syscall 9 with the size you calculated
like image 129
RobertB Avatar answered Nov 04 '22 09:11

RobertB