How to Convert C Code to MIPS Assembly for Finding Saddle Points in a Matrix?

AI Thread Summary
The discussion focuses on converting a C program that finds saddle points in a 4x4 matrix into MIPS assembly language. The original C code successfully identifies saddle points, defined as the minimum in a row and maximum in its column, but the user encounters errors during the assembly conversion. Key points include the need for proper memory layout for the matrix and efficient use of registers and indirect addressing in MIPS. The user has shared both the C code and initial assembly attempts, seeking assistance to resolve the errors. The goal is to ensure the assembly code accurately replicates the functionality of the C program.
ashkash
Messages
31
Reaction score
0

Homework Statement


Write a program using the MIPS ISA to find the saddle point of a 4x4 matrix. Print the value of the saddle points or if there is no saddle point print a message that says so. A saddle point is a value that is the minimum value in a row and also the maximum value in its column.


Homework Equations





The Attempt at a Solution


I have written a program in c to do this and it works fine. The problem I am having is converting it to assembly. Any help would be appreciated. I can show my c code if it would be helpful.
 
Physics news on Phys.org
What have you done so far?

For instance:
Did you decide on a layout of the memory locations of the matrix elements?
Which variables can you assign to a permanent register?
Can you take advantage of iteration over the matrix elements using indirect adressing?
 
So far I have written the program in C and am trying to convert that to assembly. I have written some code in assembly which is just a conversion of my C program, but am getting multiple errors. I am posting both to see if anyone can notice what I am doing wrong. I am using PCSpim to execute the assembly code. Thanks.

Code:
#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");  
}

Code:
.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
 
Back
Top