Jump to content

Help needed with MIPS


Recommended Posts

I have the following C code I am trying to convert to MIPS assembly code.  I am having a problem getting it to work.  Can someone help?  I have attached my MIPS code.

 

void array_comp (int num)  {

    int array[10];

    int i;

    for (i=0; i<10;i++) {

          array = comp_num(num, i);

    }

}

 

int comp_num (int a, int b)  {

    if (sub(a, b) >= 0)

      return 1;

    else

      return 0;

    }

 

int sub (int a, int b)  {

    return a-b;

}

 

MIPS Code

# Data declaration section

 

.data

enter0: .asciiz "\nEnter a number between 1 & 10:  "

space0: .asciiz "\n"

valid0: .asciiz "Please enter a valid number between 1 & 10"

array0: .space 10

 

# Text segment

 

.text

.globl main

 

# Start of code section

 

 

 

main: la $a0, enter0 # Load base address of string into $a0

li $v0, 4 # Set $v0 to 4, this tells syscall to print

# text string specified by $a0

syscall # Now print the text string to console

 

li $v0, 5 # Set $v0 to 5, this tells syscall to read

# integer inputted from console

 

syscall # Now read the integer from the console

 

add $a0, $v0, $zero # Loads register $a0 with input from user

add $a1, $zero, $zero # Initializes counter, $a1 = 0

la $a2, array0 # Load base address of array0 into $a2

 

array_comp: addi $sp, $sp, -8 # Making room on stack for num and i

sw $a0, 4($sp) # Push num onto stack

sw $a1, 0($sp) # Push i onto stack

 

jal comp_num # Call function comp_num

 

lw $t0, 0($sp) # Load result of array into $t0

lw $a0, 4($sp) # Load i to $a0

lw $a1, 8($sp) # Load num to $a1

addi $sp, $sp, 12 # Restore stack

 

sw $t0, 0($a2) # Store array element

addi $t0, $t0, 4 # Move pointer to next element of array

 

addi $a0, $a0, 1 # Increment counter

slt $t2, $a0, $a1 # Set $t2 = 1 if i < input value from user

bne $t2, $zero, array_comp # Go to comp_num if i < input value from user

 

li $v0, 10 #Set $v0 to 10, this tells syscall to end

syscall #execution of this program

 

 

 

comp_num: addi $sp, $sp, -4 # Making room on stack to store $ra

sw $ra, 0($sp) # Push $ra onto stack

 

jal subtract # Call function sub

 

lw $t0, 0($sp) # Load return of sub function into $t0

lw $ra, 4($sp) # Load return address of array_comp call

addi $sp, $sp, 8 # Restore stack

 

slt $t1, $t0, $zero # $t1 = 1 if $t0 < 0

nor $v1, $t1, $zero # Set $v1 = 1 if $t1 = 0 and

# $v1 = 0 if $t1 = 1

 

addi $sp, $sp, -4 # Make room on stack for result

sw $v1, 0($sp) # Push result onto stack

 

jr $ra # Jump back to $ra of array_comp

 

 

 

 

subtract: sub $v0, $a0, $a1 # Find num-count

addi $sp, $sp, -4 # Making room on stack for result

sw $v0, 0($sp) # Push result onto stack

 

jr $ra # Go to return address

Link to comment
https://forums.phpfreaks.com/topic/176632-help-needed-with-mips/
Share on other sites

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.