I need to write a program in MIPS that will find the saddle points of a 4x4 matrix. It will print the value of the saddle points and if there is no saddle points it will print a message that says so. A saddle point is a value which is a minimum in a row and the maximum in its column. I have written this program in c and it works and am having trouble converting it to assembly. My c code is below. I am having trouble putting all the loops in assembly. Any help is appreciated. thanks.

#include<stdio.h>
 
int main()
{
   int a[3][3],i,j,k,min,max,col,count=0;
   printf("enter elements row-wise");
 
   for(i=0;i<4;i++)
    {
     for(j=0;j<4;j++)
      {
       scanf("%d",&a[i][j]);
      }
   }
 
   for(i=0;i<4;i++)
    {
     min=a[i][0];
     for(j=0;j<4;j++)
      {
       if(a[i][j]<=min)
        {
         min=a[i][j];
         col=j;
         }
      }
     max=a[0][col];
     for(k=0;k<4;k++)
      {
       if(a[k][col]>=max)
        {
         max=a[k][col]; 
        }
      }
    if(max==min)
     {
      printf("saddle pt.at (%d,%d)",i+1,col+1);
      count++;
     }
   }
  if(count==0)
  printf("no saddle points");  
}

gcc -S prog.c
Then look at prog.s

I have tried to write MIPS assembly code for this problem and am trying to convert my C code to assembly. Below is what I have and when I run it using PCSpim I get a lot of errors. If anyone can see what I am doing wrong or help me out in anyone I would really appreciate it. Thanks.

.data
strA:  .asciiz "Saddle Point Value:"
strB:  .asciiz "There are no saddle points"
newline: .asciiz "\n"
space:  .asciiz "  "
.align 2
A0: .word 1, 2, 3, 4
A1: .word 5, 6, 7, 8
A2: .word 5, 6, 7, 8
A3: .word 1, 2, 3, 4
 
.text
main:  li $t0, 4
          la $t1, A0
          li $t3, 4
          li $t8, 4
          li $s4, 0
loop1: lw $t2, 0($t1)
          move $t4, $t1
loop2: lw $t5, 0($t4)
          bgt $t5, $t2, Next
          move $t2, $t5
          move $t6, $t4
Next:  addi $t4, $t4, 4
          addi $t3, $t3, -1
          bne  $t3, $zero, loop2
          lw $t7, 0($t6)
          move $t9, $t7
loop3: lw $s0, 0($t9)
          blt $s0, $t7, Skip
          move $s1, $s0
Skip:  addi $t9, $t9, 16
         addi $t8, $t8, -1
         bne  $t8, $zero, loop3
         bne  $s1, $t2, No
         la $a0, strA
         li $v0, 4
         syscall
         move $a0, $s1
         li  $v0, 1
         syscall
         la  $a0, newline
         li  $v0, 4           
         syscall 
         addi $s4, $s4, 1
No:    addi $t1, $t1, 16
         addi $t0, $t0, -1
         bne  $t0, $zero, loop1
         beq  $s4, $zero, Nos
         j e
Nos:  la $a0, strB
         li $v0, 4
         syscall
e:      li $v0, 10
         syscall

And those errors would be what exactly?

hm this line is bad: loop3: lw $s0, 0($t9) The value of t9 is not an address its only 1. So you have illegal address exception. If your next question is why its 1 than the answer is:

lw $t7, 0($t6) // you putted the first element of t6 (A0 array) to t7 (which is 1)
          move $t9, $t7 // than you copied t7 to t9 and now t9 is 1.
loop3: lw $s0, 0($t9) // now you can see that t9 is the first element of A0 array and its not some address
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.