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

In summary, the conversation is about writing a program using the MIPS ISA to find the saddle point of a 4x4 matrix. The program should print the value of the saddle point or a message if there is no saddle point. The user has written a program in C and is trying to convert it to assembly, but is encountering errors and is seeking help. In the conversation, they discuss possible approaches to solving the problem, such as deciding on a layout of memory locations and using indirect addressing. The user then shares their C and assembly code and asks for assistance in finding the error.
  • #1
ashkash
32
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
  • #2
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?
 
  • #3
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
 

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

1. What is assembly programming?

Assembly programming is a low-level programming language that is used to directly manipulate a computer's hardware. It is considered a "machine language" because it is written in a way that is easily understood by the computer's processor.

2. What is the purpose of assembly programming?

The purpose of assembly programming is to give programmers precise control over a computer's hardware and memory. It is often used for tasks that require high performance, such as operating systems, device drivers, and embedded systems.

3. How is assembly programming different from other programming languages?

Assembly programming is different from other programming languages because it is not as human-readable. It is written in a series of numeric codes and registers, rather than words and symbols. It also requires a deep understanding of computer architecture and hardware.

4. Is assembly programming still relevant in today's technology?

Yes, assembly programming is still relevant in today's technology. While it is not as commonly used as higher-level languages, it is still necessary for certain tasks and industries. Additionally, understanding assembly programming can give programmers a better understanding of how computers work at a low level.

5. How can I learn assembly programming?

There are many resources available for learning assembly programming, including online tutorials, books, and courses. It is recommended to have some knowledge of computer architecture and a high-level language, such as C or Java, before diving into assembly programming. Practice and hands-on experience are also crucial for mastering assembly programming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top