Basically, this program will multiply arg by arg2 My problem is this........ I w
ID: 3794804 • Letter: B
Question
Basically, this program will multiply arg by arg2
My problem is this........ I want to alter this program so that I can accept two value as user input (from keyboard) and then multiply the two. As the program is now, I can get the two numers to multiply and display the answer to the console, however, in order to do that I have to go in and change the values of arg and arg2. How can I change the program so that the numbers multiplying are the user inputs ??
# Optimization of what.s
.data
prompt: .asciiz " Enter a number: "
prompt2: .asciiz " Enter another number: "
arg: .word 5 # first argument
arg2: .word 4 # in order to a ccept another argument
.text
.globl main
main:
li $v0, 4 # loading print_string (code 4) into $v0
la $a0, prompt # prompt into argument register
syscall # execute syscall
li $v0, 5 # loading read_int (code 5) into $v0
syscall # executing syscall
li $v0, 4 # loading print_string (code 4) into $v0
la $a0, prompt2 # prompt into argument register
syscall # execute syscall
li $v0, 5 # loading read_int (code 5) into $v0
syscall # execute syscall
la $t3, arg
la $t4, arg2 # loads address of arg (not its value) to $t3 so that we can access it through its address
lw $t2, 0($t3) # copying value of $t3 to $t2 through lw (load word) command. So value in arg is now in $t2 ($t2=5)
lw $t3, 0($t4) # copying value of $t3 to $t1 through lw (load word) command. So value in arg is now in $t3 ($t3=5)
addi $t1, $zero, 0 # using addi command(add immediate) to add $zero and "0" then store that into $t1 ($t1=0)
beqz $t2, fin # go to fin if $t2 is equal to 0, false as $t2=5 ** Branch to fin if $t0 is zero (branch if equal zero)
fori: # a loop starts from this point to bnez
add $t1, $t1, $t2 # add $t1 and $t2 and store it in $t1, 1st Iteration-> 5+0=5, $t1=5 ,2nd Iteration ->5+5=10, $t1=10 ext
addi $t3, $t3, -1 # addi $t3 and -1 & store it in $t1,1st Iteration >5-1=4, $t3=4,2nd Iteration->4-1=3, $t3=3 ext
bnez $t3, fori # go to fori if $t3 is not equal to 0, false as $t3 = 4 ,2nd Iteration ->false , $t1 = 3 and so on
fin:
move $a0, $t1 # moving contents $t1 into $a0
li $v0, 1 # loading syscall print_int (code 1) into $v0
syscall # make the actual syscall
li $v0, 10 # syscall for exit exit
syscall # executing the syscall
Explanation / Answer
Its simple..
You are already prompting for user to enter the input as below
prompt: .asciiz " Enter a number: "
prompt2: .asciiz " Enter another number: "
Replace the lines :
la $t3, arg
la $t4, arg2
with
la $t3, prompt
la $t4, prompt2
This will load the address of the respective numbers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.