Results 1 to 6 of 6

Thread: MIPS anyone? Need Help

  1. #1
    Join Date
    Nov 2014
    Posts
    12
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default 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

  2. #2
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    @Brandon; @slacky; < these people are smart, the probably know it

  3. #3
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    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.

  4. #4
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    This is incredibly simple. Just put the malloc section in a loop until v0 returns 0.

  5. #5
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    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.

  6. #6
    Join Date
    Nov 2014
    Posts
    12
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    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?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •