Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Required to write an assembly program to double each element of an array of inte

ID: 3628901 • Letter: R

Question

Required to write an assembly program to double each element of an

array of integers by doing the following.

1. Prompt user to input array size n (n <= 10)

2. Prompt user to input element values of array A one by one

3. Display the values of the array on the console.

3. Display the result on the console.

//Below is an example of the output.

A =[2 6 -1 5],

The output is:

The values are: 2 6 -1 5

The array after doubling: 4 12 -2 10

The messages showing on the console should be:

Please input the array size: 12

!!!Invalid, try again

Please input the array size: 4

Please input the element values:

2

6

-1

5

The values are: 2 6 -1 5

The Maximum is: 4 12 -2 10

Explanation / Answer

.data

     .text

main:

ValidNvalue:

     li $v0, 4 #system call code for print string

     la $a0, Prompt1 #load address of prompt1 into $a0

     syscall #print the prompt1 message

     li $v0, 5 #system call code for read integer

     syscall #reads the value of N into $v0

    

     move counter, $v0 #moves value to counter variable to free up $v0

     bge counter, 10, ValidNvalue #makes sure it's less than 10 integers, or re-prompts

InputNumbers:

     li $v0, 4 #system call code for print string

     la $a0, Prompt2 #load address of prompt2 into $a0

     syscall #print the prompt2 message

     li $v0, 5 #system call code for read integer

     syscall #reads 1st array value into $v0

     move $t1, $v0 #moves first array value to $t1 from $v0

     sw $t1, A #stores value in first array in base address

     la $s1, A

     li counter, 1 #initializes counter to one

     addi counter, counter, 1 #should add one to the counter

    

     ble $t0, counter, InputNumbers

        Call print

     Loop:

             

     bge $t0, $s1, print # branch to print if i>10

     mul $t1, $t1, 2         # i+5

    

     add $t2, $t1, $t1       # 2 * i

     add $t2, $t2, $t2       # 4 * i

     add $t2, $t2, $a1       # $t2=address of array[i]

     sw   $t3, 0($t2)

     addi $t0, $t0, 1        # i++

     j LOOP                       # jumps to top of loop

       

print:

     li $v0, 4 #system call code for print string

     la $a0, Output #load address of prompt2 into $a0

     syscall #print the prompt2 message

      bge $t4, $s1, exit          # branch to exit if i > 10

     add $t5, $t4, $t4       # 2 * i

     add $t5, $t5, $t5       # 4 * i

     add $t5, $t5, $a1       # $t2=address of array[i]

     sw   $t6, 0($t5)

     li   $v0, 1

     move $a0, $t6           #moves value to $a0 to be printed

     syscall

exit:

     li $v0, 10                      #load value for exit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote