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
 
Well that depends on the CPU - MIPS CPU?

I've done a far amount back in the day with RISC based CPUs and the 68K.

A stack is pretty simple. You normally have a register you keep allocated as "stack". Then you store all used registers in the routine on the stack at the start - called "Callee Saves". When you want to return you pull the registers off the stack to restore the values and then return.

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.
 
I built a MIPS processor in verilog and then used spim to run and test assembly language written for MIPS ooo the joy of assembly language :-P

I would use SPIM to emulate your code and there is a way of viewing values in each register. You can use this to view what your program is doing espec. since the MIPS processor only has something like 32 registers
 
Last edited:
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?)
 
Its a bad idea to copy text from PDF, I think when I did a similar piece of coursework, I had to hand type the coursework out to make it compile, on the other hand that might not be the problem :-P

Can you follow any of what that assembly code is doing??

I think the bodging bit is simply refering to insuring your using pre-defined registers correctly, i.e. the register reserved for return value is only used for the return value.

The stack, I can't totally remember everything about it, but I think there is a register reserved for frame pointer and stack pointer.

So when ever you put something on the stack you have to add to the frame or stack pointer to keep track of everything.

Is there anything in particular you don't get with the current code, ill try to remember lol?
 
The syntax problem might be to do with the quotes around the string:

“The factorial of 10 is %d\n\000”

It's using two different characters for the quotes to make it look nice (probably due to copy/paste out of the PDF). Try replacing it with:

"The factorial of 10 is %d\n\000"

As for the stack stuff, I should be able to explain it to you if you haven't made any progress with it. My MSN is in my trust.
 
haha nah nothing to be busted for, i'm in the same boat. this course is so messed up i have no idea what's going on.
hope i get it finished in time also
it was originally for thursday but got extended to monday, from what i have read above i figure he didn't get that email. will try send him a mail or something to let him know if he doesn't already
 
Last edited:
: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