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

Click For Summary
SUMMARY

This discussion focuses on converting C code to MIPS assembly language to find saddle points in a 4x4 matrix. The saddle point is defined as the minimum value in a row that is also the maximum value in its column. The user has provided a working C implementation and is facing challenges with the assembly conversion, specifically using PCSpim for execution. Key issues include memory layout, register assignment, and iteration techniques such as indirect addressing.

PREREQUISITES
  • MIPS assembly language programming
  • Understanding of saddle points in matrices
  • PCSpim simulator usage
  • Basic C programming skills
NEXT STEPS
  • Study MIPS assembly language control structures and data handling
  • Learn about indirect addressing in MIPS for efficient memory access
  • Explore debugging techniques in PCSpim to resolve assembly errors
  • Review optimization strategies for MIPS assembly code
USEFUL FOR

Students learning computer architecture, software developers converting C code to assembly, and anyone interested in matrix algorithms and MIPS programming.

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
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
13
Views
39K
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
5
Views
11K