-
MIPS anyone? Need Help
Write a MIPS assembly language program that requests dynamic allocation of memory from the operating system (malloc function) until the operating system will not allocate any more.
The malloc() library function is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. malloc takes an argument of type int to specify the number of bytes needed from the heap, and returns the base memory address of the block.
The following code segment shows how to use malloc() in assembly programs.
Code:
//assume you need a memory block to save an array of int. The array size is in register s1.
li t0, 4 #Because each integer is 4 bytes, then n integers needs n*4 bytes
mul a0, s1, t0 #calculate the number of bytes needed in memory to save n integers
jal malloc #call malloc function. It takes one argument that specifies the number of bytes to be allocated in memory.
#It returns a memory address through v0. If v0 is zero,
#memory allocation fails, and you can terminate your program. Otherwise, v0 is the base address.
#You can use the space from v0 to v0+4(n-1) to store n integers
beq v0, zero, done
move s5, v0 #save the base address at register s5
-
@Brandon; @slacky; < these people are smart, the probably know it ;)
-
Hold on, you're just asking us to do your homework. You aren't even actually asking for help on any given part of it, just for us to do the whole thing.
-
This is incredibly simple. Just put the malloc section in a loop until v0 returns 0.
-
Here's what happens when I get bored.
Code:
.text
main:
li $t0, 0 #initialize counter
loop:
addi $t0, $t0, 1 #add one to counter
li $a0, 4 #we want to allocate one int at a time
li $v0, 9 #syscall 9 sbrk
syscall #call
move $t1, $v0 #store value in temp
li $v0, 1 #print int v0
move $a0, $t0 #print value in t0
syscall #print
move $v0, $a0 #move value into v0
bne $v0, $zero, loop #if v0 != 0 loop
li $v0, 1 #print int v0
move $a0, $t0 #value of t0
syscall #print
jr $ra #return to caller
If you go line by line with whatever mips simulator you use, you will see that it works...BUT if you just click run it will go into an infinite loop.
-
Thanks so lets say if i want to do a MIPS assembly program that prints out the memory address in hex and the contents of every location between "main" . Is that handled the same way?