Assembly language & MIPS anyone?

Soldato
Joined
4 Nov 2003
Posts
5,738
Location
Edinburgh
Longshot, i'm doing some work now thats covering this, but before i type out a massive help me post, i wondered if anyone here has any decent knowledge in this? And if so can be bothered, i can see why you wouldn't i'm hating it right now :p

Essentially trying to get my head around stacks, and calling procedures calculating an polynomial expression recursively (well i think the question doesn't mention this but i assume its like that).

If you think you can help me, i seriously owe pints, and i'll post exactly what i need, otherwise avoid this horrid thread! :D
 
Some CPUs do this with a specific PUSH/POP type of operation, some do this with basic register stores. Those with hard coded PUSH/POP often have limits on the number of 'frames' (ie set of registers) that can be stored hence limiting the depth of recursion. There other form is usually limited by the stack size in memory.

This is exactly what i need to be doing yeah, as for what i'm pregramming for, i have no idea. I'm just being told what to use from my uni :p.

I'm using PCSpim (windows version of xspim) my question is here:

For the second section of the practical, you will write a program that calculates the
expression 4x2 + 6x + 2, taking x as input, using the previously constructed calcula-
tor. Note that you will have to convert the rst part of the practical to a function
(method) and call it appropriately, using the MIPS convention for passing parame-
ters and returning values, otherwise it will be considered incorrect and marks will be
deducted.

The first part being about building a simple calculator which i'm fine with, but now i'm a little stuck as to how i use the built in stack to call the different methods properly, i could bodge this but it states in the question not too :p :D
 
Last edited:
Code:
#Factorial

			.text
			.globl main
main:
			subu $sp,$sp,32 # Stack frame is 32 bytes long
			sw $ra,20($sp) # Save return address
			sw $fp,16($sp) # Save old frame pointer
			addiu $fp,$sp,28 # Set up frame pointer
			
			li $a0,10 # Put argument (10) in $a0
			jal fact # Call factorial function
			la $a0,$LC # Put format string in $a0
			move $a1,$v0 # Move fact result to $a1
			jal printf # Call the print function
			
			lw $ra,20($sp) # Restore return address
			lw $fp,16($sp) # Restore frame pointer
			addiu $sp,$sp,32 # Pop stack frame
			jr $ra # Return to caller
			.data
$LC:
			.asciiz “The factorial of 10 is %d\n\000”
			.text
fact:
			subu $sp,$sp,32 # Stack frame is 32 bytes long
			sw $ra,20($sp) # Save return address
			sw $fp,16($sp) # Save frame pointer
			addiu $fp,$sp,28 # Set up frame pointer
			sw $a0,0($fp) # Save argument (n)
			
			lw $v0,0($fp) # Load n
			bgtz $v0,$L2 # Branch if n > 0
			li $v0,1 # Return 1
			jr $L1 # Jump to code to return
$L2:
			lw $v1,0($fp) # Load n
			subu $v0,$v1,1 # Compute n - 1
			move $a0,$v0 # Move value to $a0
			jal fact # Call factorial function
			lw $v1,0($fp) # Load n
			mul $v0,$v0,$v1 # Compute fact(n-1) * n

$L1: 		# Result is in $v0
			lw $ra, 20($sp) # Restore $ra
			lw $fp, 16($sp) # Restore $fp
			addiu $sp, $sp, 32 # Pop stack
			jr $ra # Return to caller

Thats code i've have in my notes for computing 10! can anyone decipher any of the above and explain it to me, i understand all the theory behind stacks (LILO LIFO etc...) but i can't put it into practice in this god damn laguage. Also doesn't help that even after copying and pasting that from a pdf PCspim throws parser errors on line 23/24?!?! So i can't run it line by line to see whats actually going on... This is pretty crap :D

P.S.(I'm ****ing in the wind here i think anyone know any active programming forums?)
 
:D Hello, i thought this thread would have died so left it, and no i didn't get the email (or check for yours till now, i'd actually given up on it and written it off as a few percent i've got to make up else where, but bugger me i'll pull and all nighter and try and get it sorted again. FFS if i knew i had till monday i would have busted a grove this weekend. Grumbles about hating this course at present... I feel like i'm pretty in over my head with this stuff...
 
Back
Top Bottom