MIPS Input/Output: Translate & Test Program

  • Thread starter Thread starter heyhey
  • Start date Start date
  • Tags Tags
    Mips
Click For Summary
SUMMARY

The discussion focuses on translating a C program that checks for perfect squares into MIPS assembly language for execution on the SPIM compiler. The provided MIPS code attempts to replicate the functionality of the C program but fails to execute correctly. Key issues include the absence of the PerfectSquare function and potential errors in the assembly code structure, particularly in the handling of system calls and loop control.

PREREQUISITES
  • Understanding of MIPS assembly language syntax and structure
  • Familiarity with SPIM simulator and its system call conventions
  • Knowledge of C programming, specifically input/output operations
  • Concept of perfect squares and their mathematical properties
NEXT STEPS
  • Implement the PerfectSquare function in MIPS assembly to complete the program
  • Research MIPS assembly debugging techniques to identify runtime errors
  • Explore SPIM documentation for detailed information on system calls
  • Study loop control structures in MIPS to ensure proper iteration
USEFUL FOR

This discussion is beneficial for MIPS assembly programmers, computer science students learning about low-level programming, and developers interested in translating high-level code into assembly language.

heyhey
Messages
1
Reaction score
0
This is the program I need to translate:
Code:
main()
{
    int i;
    int number=0;
    int result=0;

    for(i=0; i<10; i++){
        printf("Testing:");
        scanf("%i",&number);


        result = PerfectSquare(number);

        if(result==0)
            printf("Fail\n");
        else
            printf("%i\n",result);

    }

And this is my attempt...But it's not working on the SPIM compiler.

Code:
.data							
promptmsg:.asciiz "Testing:"	
msg1:.asciiz "Fail!"	
.text
.globl main


main:				#start of code

li 	$v0,4			#store 4 in $v0
la 	$a0,promptmsg		#copy RAM address of promptmsg into $a0 
	syscall 		#print msg
li 	$v0,5			#store 5 in $v0
li 	$t0,10			#t0 is a constant 0
li 	$t1,0			#t1 is the counter(i) for loop
loop:
beq 	$t1,$t0,end		#if t1==10 we are done
	syscall			#call operating system to perform operation
add 	$a0,$v0,$0 		#move $v0 to $a0

jal 	PerfectSquare		#call PerfectSquare function
add 	$a0,$v0,$0		#move to $a0

li 	$v0,1			#store 1 in $v0
la 	$a0,msg1		#copy RAM address of promptmsg into $a0
	syscall			# call operating system to perform operation

li 	$v0,10			#store 10 in $v0
	syscall			# call operating system to perform operation
end:
 
Technology news on Phys.org
heyhey said:
This is the program I need to translate:
Code:
main()
{
    int i;
    int number=0;
    int result=0;

    for(i=0; i<10; i++){
        printf("Testing:");
        scanf("%i",&number);


        result = PerfectSquare(number);

        if(result==0)
            printf("Fail\n");
        else
            printf("%i\n",result);

    }
Where is the code for the PerfectSquare function?
heyhey said:
And this is my attempt...But it's not working on the SPIM compiler.
"Not working" is pretty vague. Are you getting assembler errors? If not, what output are you getting?
heyhey said:
Code:
.data							
promptmsg:.asciiz "Testing:"	
msg1:.asciiz "Fail!"	
.text
.globl main


main:				#start of code

li 	$v0,4			#store 4 in $v0
la 	$a0,promptmsg		#copy RAM address of promptmsg into $a0 
	syscall 		#print msg
li 	$v0,5			#store 5 in $v0
li 	$t0,10			#t0 is a constant 0
li 	$t1,0			#t1 is the counter(i) for loop
loop:
beq 	$t1,$t0,end		#if t1==10 we are done
	syscall			#call operating system to perform operation
add 	$a0,$v0,$0 		#move $v0 to $a0

jal 	PerfectSquare		#call PerfectSquare function
add 	$a0,$v0,$0		#move to $a0

li 	$v0,1			#store 1 in $v0
la 	$a0,msg1		#copy RAM address of promptmsg into $a0
	syscall			# call operating system to perform operation

li 	$v0,10			#store 10 in $v0
	syscall			# call operating system to perform operation
end:
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 20 ·
Replies
20
Views
33K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
6K