|
30-10-2008, 08:27 | #1 |
Bananaman
Join Date: Jul 2006
Location: Liverpool/Edinburgh
Posts: 4,817
|
MIPS assembly code anyone? (Long shot).
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
|
30-10-2008, 12:00 | #2 |
Screaming Orgasm
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
|
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.
|
30-10-2008, 12:38 | #3 | ||
Bananaman
Join Date: Jul 2006
Location: Liverpool/Edinburgh
Posts: 4,817
|
Quote:
I'm using PCSpim (windows version of xspim) my question is here: Quote:
Heres my current code: 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 |
||
30-10-2008, 13:48 | #4 |
Bananaman
Join Date: Jul 2006
Location: Liverpool/Edinburgh
Posts: 4,817
|
Anyone I'm getting worried now...
|
30-10-2008, 13:48 | #5 |
Screaming Orgasm
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
|
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. |
30-10-2008, 14:05 | #6 |
Bananaman
Join Date: Jul 2006
Location: Liverpool/Edinburgh
Posts: 4,817
|
Nah, don't worry about it, its not your fault, more mine for good old last minute planning again
|
30-10-2008, 14:48 | #7 |
Bananaman
Join Date: Jul 2006
Location: Liverpool/Edinburgh
Posts: 4,817
|
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 |
31-10-2008, 12:33 | #8 |
Vodka Martini
Join Date: Sep 2007
Posts: 833
|
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?): Code:
.data $LC .asciiz “The factorial of 10 is %d\n\000” .text
__________________
|
Thread Tools | |
Display Modes | |
|
|