In: Computer Science
Write a mips assembly language program. Ask the user whether he wants to repeat the program: "\nRepeat [y/n]? ". Use service code 12 to read a character and the branch instruction to repeat the main function if the user input is character 'y'.
Answer -
MIPS Assembly Language Program:
#Define text section
.text
#Define main
.globl main
#Define main method
main:
#Push to stack
sub $sp,$sp,12
#Store return address
sw $ra,4($sp)
#String output
li $v0,4
#Load address
la $a0,pow
#Display string
syscall
#Integer input
li $v0,5
#system call
syscall
#Move data into register
move $t2,$v0
#Load value
li $v0,4
#load address
la $a0,bas
#Display string
syscall
#Load
li $v0,5
#System call
syscall
#Move value to register
move $a0,$t2
#Move value to register
move $a1,$v0
#Jump
jal euc
#Store
sw $v0,8($sp)
#Load
li $v0,4
#Load value
la $a0,str
#System call
syscall
#Load value
li $v0,1
#Load value
lw $a0,8($sp)
#System call
syscall
#Restore address
lw $ra,4($sp)
#Pop stack
add $sp,$sp,12
#Jump
jr $ra
#Define data
.data
#Define label
str:
#Display message
.asciiz "value = "
#Define label
bas:
#Display message
.asciiz "second integer = "
#Define label
pow:
#Display message
.asciiz "first integer = "
#Define text
.text
#Define label
euc:
#Push stack
sub $sp,$sp,8
#Store address
sw $ra,4($sp)
#Exit if b is not 0
bne $a1, $zero, L1
#Return a0
add $v0,$zero,$a0
#Pop stack
add $sp,$sp,8
#Return
jr $ra
#Define L1
L1:
#Set c to b
move $t4,$a1
#Compute remainder
rem $a1,$a0,$a1
#Set a
move $a0,$t4
#Call recursively
jal euc
#Load
lw $ra,4($sp)
#Move value to $v0
move $v0,$a0
#Pop stack
add $sp,$sp,
#Return
jr $ra
Note: If you have any related doubts, queries, feel free to ask by commenting down below.