Question

2. Q2 [25 points) Consider the MIPS code given in the following. main: addi Ssp, Ssp, -4 addi Sa0, $0, 2 sw $a0, 4(Ss1) addi Sal, $0, 3 sw Sal, 8(Ss1) jal dofsum sw SvO, 16(8s1) lw Sra, O(Ssp) addi Ssp, Ssp, 4 jr Sra dofsum: jal sum jr Sra sum: add SvO, Sa0, Sal jr Sra Draw the snapshot of the stack before, during, and after dofsum) function call. As- sume SSP, 0x45fff00 before dofsum() function call.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ScreenShot

----------------------------------------

Program

#Data declaration section
.data
numbers_len:         .word     11
numbers:             .word     -27, 3, 46, -7, 11, 24, -5, 14, -18, 12, 35
index:               .asciiz   "Enter an integer: \n"
value:               .asciiz   "Enter another integer: \n"
Result:              .asciiz   "Result Array Content:\n"
nextline:            .asciiz   "\n"
#Main program
.text
#Index asking prompt
la $a0,index
li $v0,4
syscall
#Read index
li $v0,5
syscall
#store in s0
move $s0,$v0
#Prompt for div value
la $a0,value
li $v0,4
syscall
#Read value
li $v0,5
syscall
#Store in s1
move $s1,$v0

#Array length
lw $t1,numbers_len
#Loop to check the index and change the array
loop:
#Check index<0
blt $s0,0,printArray
#Check index>10
bgt $s0,10,changeArray
#Otherwise
#Loop to check division and change array elements
changeArray:
#Set t2=0 for counter
li $t0,2
#Address of the array
la $t0,numbers
changeIndexVal:
#Until index
bgt $t2,$s0,printArray
#Elements fro array
lw $t3,($t0)
#divide with value
div $t3,$s1
#store remainder in t4
mfhi $t4
#chech remainder
bne $t4,0,nextVal
#if remainder 0 then multiply
mul $t3,$t3,$s1
#store the new val in array
sw $t3,($t0)
#Increment counter
addi $t2,$t2,1
#increment address for next element
addi $t0,$t0,4
#continue
j changeIndexVal
#If remainder not 0
nextVal:
addi $t2,$t2,1
addi $t0,$t0,4
j changeIndexVal

#If index greater than 10
changeLoop:
#Loop until end of the array
beq $t2,$t1,printArray
#elements in t3
lw $t3,($t0)
#check remainder
div $t3,$s1
mfhi $t4
bne $t4,0,next
#if remainder 0 then change value of the array
mul $t3,$t3,$s1
sw $t3,($t0)
addi $t2,$t2,1
addi $t0,$t0,4
j changeLoop
#Otherwise
next:
addi $t2,$t2,1
addi $t0,$t0,4
j changeLoop

#Print array
printArray:
#Prompt the result
la $a0,Result
li $v0,4
syscall
li $t2,0
la $t0,numbers
#Loop for printing array elements
printLoop:
beq $t2,$t1,exit
lw $a0,($t0)
li $v0,1
syscall
la $a0,nextline
li $v0,4
syscall
addi $t2,$t2,1
addi $t0,$t0,4
j printLoop
#end of the program
exit:
li $v0,10
syscall

----------------------------------------

Output

Enter an integer:
-1
Enter another integer:
3
Result Array Content:
-27
3
46
-7
11
24
-5
14
-18
12
35

-- program is finished running --

Add a comment
Know the answer?
Add Answer to:
2. Q2 [25 points) Consider the MIPS code given in the following. main: addi Ssp, Ssp,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone explain how you get the answer to this question? 1. Based on the following code segment what values are in the runtime stack immediately after the fourth time the instruction labeled...

    can someone explain how you get the answer to this question? 1. Based on the following code segment what values are in the runtime stack immediately after the fourth time the instruction labeled Rec is executed .text li Sa0, 7 jai Fact #assume the address of this instruction is 2000 fother code not shown Fact: addi Ssp, Ssp, -8 sw Sra, 4(Ssp) sw Sa0, 0(Ssp) slti St0, Sa0, 1 beq St0, Szero, Rec li SvO, 1 addi $sp, Ssp, 8...

  • The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of...

    The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of using stack for storing/protecting registers. What is the mistake and how should it be fixed? Correct the corresponding lines in the code. For the corrected code, sketch the stack frame contents at the time when the instruction ‘move $s1, $a1’ is being executed. f:   addi $sp, $sp, 12 sw   $ra, 8($sp) sw   $s1, 4($sp) sw   $s0, 0($sp) move $s0, $a0 move $s1, $a1              #...

  • Question 4) (12 Marks) This question is based on the MIPS assembly code shown below. data...

    Question 4) (12 Marks) This question is based on the MIPS assembly code shown below. data the Array: space 160 main: li $t6, 1 li $17, 4. sw $17, the Array($0) sw $17, theArray($17) li $t0, 8 loop: addi $t3, $t0, -8 addi $t4, $t0, -4 lw 1, the Array($t3) lw $12, the Array(St4) add $15, $t1, $t2 sw $15, theArray($to) addi $t0, $t0, 4 blt $t0, 160, loop jr Sra. Question 4 Assembly code a) what is the contents...

  • ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...

    ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...

  • .data prompt: .asciiz "Input an integer x:\n" result: .asciiz "Fact(x) = " .text main: # show prompt li $v0, 4 la $a0, prompt syscall # read x li $v0, 5 syscall # function call move $a...

    .data prompt: .asciiz "Input an integer x:\n" result: .asciiz "Fact(x) = " .text main: # show prompt li $v0, 4 la $a0, prompt syscall # read x li $v0, 5 syscall # function call move $a0, $v0 jal factorial # jump factorial and save position to $ra move $t0, $v0 # $t0 = $v0 # show prompt li $v0, 4 la $a0, result syscall # print the result li $v0, 1 # system call #1 - print int move $a0,...

  • $s 10) if $si and $$2 are to be saved on the stack which of the...

    $s 10) if $si and $$2 are to be saved on the stack which of the following would accomplish the (10Pts) task? A) addi sp, sp, 8 sw $s1, 0($sp) sw $s2, 4($sp) B) addi sp, sp, 8 lw $s1, 8($sp) lw $s2, 4($sp) addi sp, sp, -8 C) sw $s1, 0($sp) sw $s2, 4(Ssp) D) addi sp, sp, -8 lw $s1, 8($sp) lw $s2, 4($sp) 11) What instruction would be used to call a procedure called square? (5Pts) 12)...

  • Here is an assembled function in MIPS addi $sp, $sp, 4 Sw Li 1i L1 Li...

    Here is an assembled function in MIPS addi $sp, $sp, 4 Sw Li 1i L1 Li 0xe04800b8 ex23bdfffc addi $29, $29,exffff... 75: 0x084000bc exafb10000 sw $17,8x8000e000($29) 76: 0xe04000ce ex24020000 addiu $2,$0,0x00000000 78: 0x004000c4 0x24030000 addiu $3, $8,0x00000000 79: 0x004000c8 ex240a0020 addiu $18, s0,8xeeee... 80: 0x004000cc 8x240b000a addiu $11,s0,8x0000... 81: 0x884800de ex90910000 lbu $17,0x000e0000($4) 83: 0x004000d4 0x28840001 add1 $4,$4,0x80000001 84: 0x004008d8 8x122b0004 beq $17,$11,8x00000004 85: 0x004000dc 8x20420001 addi $2,$2,8x80000001 86: 0x804000e0 8x162afffb bne $17,$10,8xfffffffb 87: 0x004008e4 8x20630001 addi $3,$3,8x00000001 88: 0x004000e8...

  • The task will be to write a program in assembler to enter a number and calculate...

    The task will be to write a program in assembler to enter a number and calculate its associated Fibonacci number using a procedure (subroutine) that is called recursively. Factorial and Fibonacci(outline Programs) # MIPS assembly assembly assemblyassemblycode .data n: .word 4 .text main: la $s0,n lw $a0, 0($s0) jal factorial # move what ever is returned into $a0 move $a0, $v0 li $v0,1 syscall b finished factorial: add $sp , $sp , -8 # make room sw $a0, 4($sp )...

  • Question 3 (10 points) Convert the following MIPS assembly code into machine language. Write the instruction...

    Question 3 (10 points) Convert the following MIPS assembly code into machine language. Write the instruction in hexadecimal. The opcode for sw 43 (101011). sw St1, -4(St3) Question 4 (10 points) Consider the following MIPS assembly code: addi $s3, $0, 5 addi $s1, S0, 3 addi Ss1, $s1, 2 beq Ss3, Ssl, target addi Ss1, Ss1, 1 target: add Ss3, Ss1, Ssl a. After running the code, what is the value of Ss3? b. If the memory address of the...

  • Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls...

    Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls multiply. You are also given the argument registers to be used. /* C-program */ int multiply (int number, int times) { int f; f = number * times return f; } # MIPS PROGRAM # assumes we have called the program leaf_example # $a0=number, $a1=times, $s0=f main: $addi $a0,$zero,3 $addi $a1,$zero,2 jal multiply # place the address into $ra j EXIT2 EXIT2: j OS...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT