Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MIPS Assembly Language and C Language Modify swap.s to translate the following procedure directly to MIPS assembly language. The temp variable, like all local variables

MIPS Assembly Language and C Language

image text in transcribed

Modify swap.s to translate the following procedure directly to MIPS assembly language. The temp variable, like all local variables in C (when not optimized), is stored on the stack. In other words you cannot use $t0 to hold temp, though you may need it briefly. Hint: you will need to use 6 lw/sw instructions.

This exercise is slightly contrived, and could be easier if we let you optimize and use $t0 to hold the temp variable, part of the point of this exercise is to see what kind of difference optimization can make.

void swap

(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; }

Now modify your solution (swap1.s) to implement the following buggy version of the swap procedure. void swap (int *px, int *py) { int *temp; *temp = *px; *px = *py; *py = *temp; }

Q1. The bug in swap2.s is that the temp pointer is dereferenced without being initialized. Why might a programmer not notice this even after testing the buggy swap? In other words: what situation would allow buggy swap to seem to work correctly?

Code given at bottom:

.text

main:

la $a0,n1

la $a1,n2

jal swap

li $v0,1 # print n1 and n2; should be 27 and 14

lw $a0,n1

syscall

li $v0,11

li $a0,' '

syscall

li $v0,1

lw $a0,n2

syscall

li $v0,11

li $a0,' '

syscall

li $v0,10 # exit

syscall

swap: # your code goes here

.data

n1: .word 14

n2: .word 27

(Exercise) Create swap1.s Modify swap.s to translate the following procedure directly to MIPS assembly language. The temp variable, like all local variables in C (when not optimized), is stored on the stack. In other words you cannot use sto to hold temp, though you may need it briefly. Hint: you will need to use 6 1w/sw instructions. This exercise is slightly contrived, and could be easier if we let you optimize and use sto to hold the temp variable, part of the point of this exercise is to see what kind of difference optimization can make. void swap (int *px, int *py) int temp; temppx pytemp (Exercise) Create swap2.s Now modify your solution (swap1.s) to implement the following buggy version of the swap procedure. void swap (int *px, int *py) int temp; Q1. The bug in swap2.s is that the temp pointer is dereferenced without being initialized. Why might a programmer not notice this even after (Exercise) Create swap1.s Modify swap.s to translate the following procedure directly to MIPS assembly language. The temp variable, like all local variables in C (when not optimized), is stored on the stack. In other words you cannot use sto to hold temp, though you may need it briefly. Hint: you will need to use 6 1w/sw instructions. This exercise is slightly contrived, and could be easier if we let you optimize and use sto to hold the temp variable, part of the point of this exercise is to see what kind of difference optimization can make. void swap (int *px, int *py) int temp; temppx pytemp (Exercise) Create swap2.s Now modify your solution (swap1.s) to implement the following buggy version of the swap procedure. void swap (int *px, int *py) int temp; Q1. The bug in swap2.s is that the temp pointer is dereferenced without being initialized. Why might a programmer not notice this even after

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions