Question 1. Using MARS write a MIPS assembly language program to prompt for two
ID: 3878514 • Letter: Q
Question
Question 1.
Using MARS write a MIPS assembly language program to prompt for two integers A and B, store them in memory locations labelled A and B respectively, then calculate the sum between them (i.e. A + B) and store the sum into a memory location labelled S. After that print out the result (i.e. integer S) after printing the string "The sum of A and B (A + B) is ". Then assemble and run the program with MARS to show and capture the input/output. Follows instructions for programming assignments in the syllabus.
Explanation / Answer
#Please see the below code
.data
A: .word # Integer A
B: .word # Integer B
S: .word #Integer S use to store the sum of A and B
Prompt1: .asciiz "Please enter first number A: "
Prompt2: .asciiz "Please enter second number B: "
Result: .asciiz "The sum of A and B in S is : "
.text
main:
#Prompt for integers A
li $v0, 4 # Load instruction for printing the string
la $a0, Prompt1 # Load Prompt1 into $a0
syscall
#Read first integer
li $v0, 5 # Read first integer A
la $t0, A
syscall
#Store first integer A into the memory
move $t0, $v0
sw $t0, A
##Prompt for integers B
li $v0, 4 # Load instruction for printing the string
la $a0, Prompt2 # Load prompt2 into $a0
syscall
#Read second integer
li $v0, 5 # Read second integer
la $t1, B # $t0 = A
syscall
#Store second integer into memory
move $t1, $v0 # Move contents in $v0 to $t0
sw $t1, B # A = value at $t0
#Add the two variables A and B and store it in S
la $t2, S # $t2 = S
add $t2, $t0, $t1 # $t2 = $t0 + $t1
sw $t2, S # S = value at $t2
#Display the Result prompt
la $a0, Result # Loads Output label to be printed
li $v0, 4 # Sysycall to print string
syscall
#Display the sum
lw $a0, S # $a0 = value at S
li $v0, 1 # Syscall to print integer
syscall
#Exit the program
li $v0, 10 # Load exit code to $v0
syscall
OUTPUT:
Please enter first number A: 3
Please enter second number B: 4
The sum of A and B in S is : 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.