MIPS Input/Output: Translate & Test Program

  • Thread starter Thread starter heyhey
  • Start date Start date
  • Tags Tags
    Mips
Click For Summary
The discussion revolves around translating a C program into MIPS assembly language for use with the SPIM compiler. The original C program prompts the user to input a number ten times, checks if the number is a perfect square using a function called PerfectSquare, and prints either the result or "Fail" if the number is not a perfect square. The provided assembly code attempts to replicate this functionality but is reported as not working on the SPIM compiler. Key issues raised include the absence of the PerfectSquare function's code and the need for clarification on the specific errors or output being encountered during execution. Participants emphasize the importance of detailing any assembler errors or unexpected outputs to diagnose the problem effectively.
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:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · 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
32K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
6K