MIPS Help

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 21
Reputation: ashkash is an unknown quantity at this point 
Solved Threads: 0
ashkash ashkash is offline Offline
Newbie Poster

MIPS Help

 
0
  #1
May 16th, 2007
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.

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int a[3][3],i,j,k,min,max,col,count=0;
  6. printf("enter elements row-wise");
  7.  
  8. for(i=0;i<4;i++)
  9. {
  10. for(j=0;j<4;j++)
  11. {
  12. scanf("%d",&a[i][j]);
  13. }
  14. }
  15.  
  16. for(i=0;i<4;i++)
  17. {
  18. min=a[i][0];
  19. for(j=0;j<4;j++)
  20. {
  21. if(a[i][j]<=min)
  22. {
  23. min=a[i][j];
  24. col=j;
  25. }
  26. }
  27. max=a[0][col];
  28. for(k=0;k<4;k++)
  29. {
  30. if(a[k][col]>=max)
  31. {
  32. max=a[k][col];
  33. }
  34. }
  35. if(max==min)
  36. {
  37. printf("saddle pt.at (%d,%d)",i+1,col+1);
  38. count++;
  39. }
  40. }
  41. if(count==0)
  42. printf("no saddle points");
  43. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: MIPS Help

 
0
  #2
May 16th, 2007
gcc -S prog.c
Then look at prog.s
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 21
Reputation: ashkash is an unknown quantity at this point 
Solved Threads: 0
ashkash ashkash is offline Offline
Newbie Poster

Re: MIPS Help

 
0
  #3
May 17th, 2007
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.

  1. .data
  2. strA: .asciiz "Saddle Point Value:"
  3. strB: .asciiz "There are no saddle points"
  4. newline: .asciiz "\n"
  5. space: .asciiz " "
  6. .align 2
  7. A0: .word 1, 2, 3, 4
  8. A1: .word 5, 6, 7, 8
  9. A2: .word 5, 6, 7, 8
  10. A3: .word 1, 2, 3, 4
  11.  
  12. .text
  13. main: li $t0, 4
  14. la $t1, A0
  15. li $t3, 4
  16. li $t8, 4
  17. li $s4, 0
  18. loop1: lw $t2, 0($t1)
  19. move $t4, $t1
  20. loop2: lw $t5, 0($t4)
  21. bgt $t5, $t2, Next
  22. move $t2, $t5
  23. move $t6, $t4
  24. Next: addi $t4, $t4, 4
  25. addi $t3, $t3, -1
  26. bne $t3, $zero, loop2
  27. lw $t7, 0($t6)
  28. move $t9, $t7
  29. loop3: lw $s0, 0($t9)
  30. blt $s0, $t7, Skip
  31. move $s1, $s0
  32. Skip: addi $t9, $t9, 16
  33. addi $t8, $t8, -1
  34. bne $t8, $zero, loop3
  35. bne $s1, $t2, No
  36. la $a0, strA
  37. li $v0, 4
  38. syscall
  39. move $a0, $s1
  40. li $v0, 1
  41. syscall
  42. la $a0, newline
  43. li $v0, 4
  44. syscall
  45. addi $s4, $s4, 1
  46. No: addi $t1, $t1, 16
  47. addi $t0, $t0, -1
  48. bne $t0, $zero, loop1
  49. beq $s4, $zero, Nos
  50. j e
  51. Nos: la $a0, strB
  52. li $v0, 4
  53. syscall
  54. e: li $v0, 10
  55. syscall
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: MIPS Help

 
0
  #4
May 17th, 2007
And those errors would be what exactly?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: MIPS Help

 
0
  #5
May 17th, 2007
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:
  1. lw $t7, 0($t6) // you putted the first element of t6 (A0 array) to t7 (which is 1)
  2. move $t9, $t7 // than you copied t7 to t9 and now t9 is 1.
  3. loop3: lw $s0, 0($t9) // now you can see that t9 is the first element of A0 array and its not some address
Last edited by andor; May 17th, 2007 at 5:18 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC