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
  • 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              #...

  • 7 [24 marks] Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does not implement forwarding or stalling in hardware. (1) add $4, $1, $1 (2) add $7,...

    7 [24 marks] Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does not implement forwarding or stalling in hardware. (1) add $4, $1, $1 (2) add $7, $4, $9 (3) lw $2, 40($8) (4) sub $8, $1, $2 (5) sw $8, 80(S2) (6) sub $2, $8, $4 (7) lw S8, 2($1) (8) add $8, $4, S2 Identify the data dependences that cause hazards. You are to use the following format to inform each...

  • Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does...

    Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does not implement forwarding or stalling in hardware.    (1) add $4, $1, $1                               (2) add $7, $4, $9                               (3) lw $2, 40($8)                               (4) sub $8, $1, $2                               (5) sw $8, 80($2)                               (6) sub $2, $8, $4                               (7) lw $8, 2($1)                               (8) add $8, $4, $2 Identify the data dependences that cause hazards. You are to use the...

  • Usc only the following MIPS instructions for assignment questions 3, 4 and 5: add, sub, addi,...

    Usc only the following MIPS instructions for assignment questions 3, 4 and 5: add, sub, addi, j, beq, bne, lw, sw. You may not need as many lines as we provide space for 4. (4 pts) Write a MIPS program starting at address 20 that writes a value of 488 to register $7. Next, you will test if register $10 is equal to register $7. If the values are equal, continue execution at address 48; otherwise set the value in...

  • Compiling C Programs into MIPS Assembly and Machine Code sll $t1, $a1, 2 add $t1, $a0....

    Compiling C Programs into MIPS Assembly and Machine Code sll $t1, $a1, 2 add $t1, $a0. $t1 lw $t0, 0($t1) lw $t2, 4($t1) sw $t2, 0($t1) sw $t0, 4($t1) 1. From the assembly code, what machine code might a MIPS assembler produce? 2. What does this program do? Write the C code for this assembly program.

  • MIPS Assembly Computer Organization and Design MIPS Algorithm Design (25 pts. Throughout the course, we have...

    MIPS Assembly Computer Organization and Design MIPS Algorithm Design (25 pts. Throughout the course, we have learned different types of instruction sets in MIPS programming: arithmetic instructions, branch instructions, jump instructions, and memory access instructions. Let's assume, we have a 10 line of MIPS code in which we perform 5 arithmetic operations, 2 branch operations. 1 jump operation, and 2 memory operations (1 store, 1 load). In the datapath, we are given that: fetching the instruction set takes 2 states....

  • Assume the MIPS instruction subset is redefinied to contain only the following instructions: 1. Assume that...

    Assume the MIPS instruction subset is redefinied to contain only the following instructions: 1. Assume that our MIPS instruction subset is redefined to contain only the following instructions: Instruction Instruction fetch Register read & ALU operation Data Memory Register write decode 0 ns R-format 2ns 1 ns lw ns l ns 2 ns 5 ns 1 ns ns 1 ns ns 0 0 bne The table lists the times required for each step within each instruction. Recall that with the...

  • I just need part (d) answered 7) [24 marks] Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does not implement forwarding or stalling in hardware....

    I just need part (d) answered 7) [24 marks] Consider the following MIPS code segment that is executed on a 5-stage pipeline architecture that does not implement forwarding or stalling in hardware. (1) add $4, $1, $1 (2) add $7, $4, $9 (3) lw $2, 400S8) (4) sub $8, $1, $2 (5) SKSs, so($2) (6) sub $2, $8, $4 (7) lw $3, 2($1) (8) add $8, $4, $2 Identify the data dependences that cause hazards. You are to use the...

  • a).For the following MIPS assembly code to be executed using the pipelined datapath, identify all of...

    a).For the following MIPS assembly code to be executed using the pipelined datapath, identify all of the data dependencies (which register in which instruction needs the result register value from which another instruction?) and enumerate them (give them numbers as 1,2, ...). b).Which dependencies are data hazards that will be resolved via forwarding and without a stall (you can specify the number(s) from the part a)? c).Which dependencies are data hazards that will cause a stall (you can specify the...

  • 4. List the values on the control signals for the following instructions. The MIPS architecture and...

    4. List the values on the control signals for the following instructions. The MIPS architecture and instruction formats studied in class are shown below for reference. Your answer needs to be 1, 0, or X for each signal (a 0 or 1 will not be accepted as a substitute for X) MemtoReg MemWriteBranchALUSrc RegDst RegWrite sub r2, r5, r23 beq rl, r3, L2 sw rl, 36(r4) lw r3,100(r6) addi r2,rl4,-24 j L3 ontro Unit Write ranch PCSrc Op Funct LUSre...

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