Question

Write MIPS code for each of the following instructions, Your assembly should implement the C code...

Write MIPS code for each of the following instructions,

Your assembly should implement the C code directly – i.e.,do not ’optimize’ the C code to change the order of operations or reduce computations.

Use commands only like add, sub, lw, sw, immediate

Part 1. x = 3-13*x; Do not use multiply. One way of doing the multiply without a multiply instruction is by using many add instructions (x+x+...+x). For this problem, you should do it with fewer additions. Hint: We know how to express any integer as a sum of powers of 2 (e.g.,7 = 1+2+4), and we also know how to multiply by powers of 2 using repeated addition. This gives us a method to multiply by 13 (applying distributivity).

Part 2. a[j-3] = a[2j]+j

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1. x = 3-13*x

.data
   msg1:   .asciiz   "Enter x: "
   msg2:   .asciiz   "x= "

   # Start .text segment (program code)
.text
.globl   main
  
main:
   # Print string msg1 "Enter x: "
   li   $v0,4       # print_string syscall code = 4
   la   $a0, msg1   # load the address of msg
   syscall

   # Get input x from user and save
   li   $v0,5       # read_int syscall code = 5
   syscall  
   move   $t0,$v0   # input x is stored in $t0
  
   # Print string msg2 "x= "
   li   $v0,4       # print_string syscall code = 4
   la   $a0, msg2   # load the address of msg
   syscall
  
   li $a0,3
   li $a1,0
  
   # 13 = 8 + 4 + 1
   # 13 = 2^3 + 2^2 + 2^0
   move $t1,$t0
   sll $t1,$t1,0 # x*2^0
   add $a1,$a1,$t1
  
   move $t1,$t0  
   sll $t1,$t1,2 # x*2^2
   add $a1,$a1,$t1
  
   move $t1,$t0
   sll $t1,$t1,3 # x*2^3
   add $a1,$a1,$t1
  
   sub $a0,$a0,$a1 #x=x-13*x
  
   li   $v0,1          
   syscall

   li   $v0,10       # exit
   syscall

#-----------------------------------------

Sample output:

Part 2. a[j-3] = a[2j]+j

.data
   a:   .word 3, 8, 9, 2, 6, 2, 4, 7, 3, 7, 3, 1, 4, 9, 8, 3 #array
   msg1:   .asciiz   "j="
  
   # Start .text segment (program code)
.text
.globl   main
  
main:
   # Print string msg1 "j="
   li   $v0,4       # print_string syscall code = 4
   la   $a0, msg1   # load the address of msg1
   syscall
  
   # Get input j from user and save
   li   $v0,5       # read_int syscall code = 5
   syscall  
   move   $t0,$v0 # $t0 stores j
  
   la $t1, a    #base address of the array a
   sub $t2,$t0,3 # $t2=j-3
   sll $t3,$t0,1 # $t3=2*j
  
   add $t3, $t3, $t3 # double the index
add $t3, $t3, $t3    # double the index again (now 4x)
   add $t4, $t1, $t3 # combine the two components of the address
lw $t5, 0($t4) # get the value from the array cell
  
   add $t5,$t5,$t0 # a[2j]+j
  
   add $t0, $t0, $t0 # double the index
   add $t0, $t0, $t0 # double the index again (now 4x)
   add $t4, $t1, $t2   # combine the two components of the address
   sw $t5, 0($t4) # a[j-3]=a[2j]+j
  
   li   $v0,10       # exit
   syscall

#---------------------------------------

Sample output:

Initial array:

Array after program execution:

Add a comment
Know the answer?
Add Answer to:
Write MIPS code for each of the following instructions, Your assembly should implement the C code...
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
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