PDA

View Full Version : MIPS assembly code anyone? (Long shot).


LeperousDust
30-10-2008, 08:27
Before i go explaining my programming problem since its a little long winded and generic, does anyone here have any experience with assembly code for MIPS processors. I'm pretty much totally in over my head with stacks trying to get brain around probably simple recursion (i think). I need to do some work by 4pm today and i've got one of three questions completed. The first was bloomin easy though and they seem to ramp up after this :(

Mark
30-10-2008, 12:00
Assembly in general, maybe - it's been a while since I've done that 'in anger'. I'm not sure if I've ever worked with MIPS so if it's MIPS-specific then not much hope. :(

LeperousDust
30-10-2008, 12:38
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

Heres my current code:
# Simple Calculator
#
# Written by Alex Dodd
# Based on hex.s originally program from U. of Manchester for the ARM ISA

.data
prompt1: .asciiz "\nOperand1:\n"
prompt2: .asciiz "Operand2:\n"
prompt3: .asciiz "Operator:\n"
outmsg: .asciiz "\nAnswer:\n"
div0: .asciiz "\nDivde by zero:\n"


.globl main

.text
main:
# prompt for operand1
li $v0, 4
la $a0, prompt1
syscall

# Get operand1 from user
li $v0, 5
syscall

add $s0, $zero, $v0 # Keep the operand1 in $s0,

# prompt for operand2
li $v0, 4
la $a0, prompt2
syscall

# Get operand2 from user
li $v0, 5
syscall

add $s1, $zero, $v0 # Keep the operand2 in $s1,

# prompt for operator
li $v0, 4
la $a0, prompt3
syscall

# Get operator from user
li $v0, 12
syscall

add $s2, $zero, $v0 # Keep the operator in $s2,

# Output answer message
li $v0, 4
la $a0, outmsg
syscall
j addition

addition:add $t0, $zero, 43
bne $s2, $t0, subtract
add $s3, $s0, $s1
j print

subtract:
add $t0, $zero, 45
bne $s2, $t0, multiply
sub $s3, $s0, $s1
j print

multiply:
add $t0, $zero, 42
bne $s2, $t0, divide
mult $s0,$s1
mflo $s3
j print

divide:
beq $s1, $zero, error1
div $s0 $s1
mfhi $t1
mflo $s3
j print

print: li $v0, 1
add $a0, $zero, $s3
syscall
beq $t1, $zero, end
li $v0, 11
add $a0, $zero, 114
syscall
li $v0, 1
add $a0, $zero, $t1
syscall # Print answer in $a0
j end

error1: li $v0, 4 # Output error message
la $a0, div0
syscall

end: # end the program
li $v0, 10
syscall

But for part two i've removed all the "brand not equals" (bne) because it doesn't need to be automatic now i need to call them with the special stack operator... :( Confuzzled.

LeperousDust
30-10-2008, 13:48
Anyone :D ;D I'm getting worried now... ;)

Mark
30-10-2008, 13:48
I'm not sure what the MIPS calling convention is and as a result I don't want to give you duff advice. :(

Even worse, there's people at work who would know but I'm on holiday these two days. :(

Sowwy.

LeperousDust
30-10-2008, 14:05
Nah, don't worry about it, its not your fault, more mine for good old last minute planning again :D

LeperousDust
30-10-2008, 14:48
#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 found 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

phykell
31-10-2008, 12:33
First of all, I don't know the MIPS architecture at all...

First thing is to fix the parse error - I'm not sure but $LC looks odd to me...

Try putting the data's variable name on the same line as the data (is the colon necessary?):


.data
$LC .asciiz “The factorial of 10 is %d\n\000”
.text


Regarding your code, the factorial example shows how the stack is used to store a return address so that the program counter can return to the calling function when the called function returns. The stack frame is used (in other architectures anyway) to store variables, etc. and the return addresses of the calling function.