Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Question: The table below shows 32-bit values of an array stored in memory.

2.6.1 For the memory locations in the table above, write C code to sort the data from lowest to highest, placing the lowest value in the smallest memory location shown in the figure. Assume that the data shown represents the C variable called Array, which is an array of type int, and that the first number in the array shown is the first element in the array. Assume that this particular machine is a byte-addressable machine and a word consists of four bytes.

2.6.2 For the memory locations in the table above, write MIPS code to sort the data from lowest to highest, placing the lowest value in the smallest memory location. Use a minimum number of MIPS instructions. Assume the base address of Array is stored in register $s6.

Short Answer

Expert verified

2.6.1)

#sort the data from lowest to highest

temp1 = Array[0]; //assign 1st value of array to temp1

temp2 = Array[1]; //assign 2nd value of array to temp1

Array[1] = temp1; //store the temp1 value to 2nd value of array

Array{0] = Array[4]: //store the value at index 4 of

//array into 1st value of array

Array[4] = Array{3);//store the value at index 3 of array

//into the value at index 4 of array

Array[3] = temp2; store the temp2 value into index 3 of array

2.6.2)

#data section

data

#Declare array

Array: .word 2, 4, 3, 6, 1

#Declare the strings

result: .asciiz "The sorted data: \n:"

newline: .asciiz "\n"

#Text section

text

#main

Step by step solution

01

Given memory locations

2.6.1)

Consider the following memory locations:

Consider the name of the variable as Array, which is an array of type int. It contains the given data values. Assume the integer variables temp1 and temp2 for temporary storage values.

02

C program to sort

#sort the data from lowest to highest

temp1 = Array[0]; //assign 1st value of array to temp1

temp2 = Array[1]; //assign 2nd value of array to temp1

Array[1] = temp1; //store the temp1 value to 2nd value of array

Array{0] = Array[4]: //store the value at index 4 of

//array into 1st value of array

Array[4] = Array{3);//store the value at index 3 of array

//into the value at index 4 of array

Array[3] = temp2; store the temp2 value into index 3 of array

03

Step 3:Result after sorting the memory location

After sorting the data, the lowest value is placed in the smallest memory location.

The memory locations and data after sorting the data are shown in the below table:

04

Sample output:

The sorted data:

1

2

3

4

6

2.6.2)

05

MIPS code to sort the data from lowest to highest

#data section

data

#Declare array

Array: .word 2, 4, 3, 6, 1

#Declare the strings

result: .asciiz "The sorted data: \n:"

newline: .asciiz "\n"

#Text section

text

#main

06

 code to sort the data from lowest to highest

#the base address of Array is stored in register $s6.

la $s6, Array

lw $t0, 0($s6) # load the data in Array[0] to $t0

lw $t1, 4($s6) #load the data in Array[1] to $t1

sw $t0, 4($s6) #store the data from $t0 to Array[1]

lw $t2, 16($s6) # load the data in Array[4] to $t2

sw $t2, 0($s6) #store the data from $t2 to Array[0]

lw $t3,12($s6) # load the data in Array[3] to $t3

sw $t3, 16($s6) #store the data from $t3 to Array[4]

sw $t1, 12($s6) #store the data from $t1 to Array[3]

07

Sample output

The sorted data:

1

2

3

4

6

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

For each MIPS instruction, show the value of the opcode(OP), source register(RS), and target register(RT) fields. For the I-type instructions, show the value of the immediate field, and for the R-type instructions, show the value of the destination register(RD) field.

addi \(t0,\)s6,4

add \(t1.\)s6,\(0

sw \)t1, 0(\(t0)

lw \)t0,0(\(t0)

add \)s0,\(t1,\)t0

Question: Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register \(a0 to hold the address of a null-terminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register \)v0. If a non-digit character appears anywhere in the string, your program should stop with the value -1 in register \(v0. For example, if register \)a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the null-terminating string โ€œ24โ€), then when the program stops, register $v0 should contain the value 24ten .

Question: For the following C statement, what is the corresponding MIPS assembly code? Assume that the variables f, g, h, i, and j are assigned to registers \(s0, \)s1, \(s2, \)s3, and \(s4, respectively. Assume that the base address of the arrays A and B are in registers \)s6 and $s7, respectively.

Question: Assume \(t0 holds the value 0x00101000. What is the value of \)t2 after the following instructions?

slt \(t2, \)0, \(t0

bne \)t2, \(0, ELSE

j DONE

ELSE: addi \)t2,$t2, 2

DONE:

If the current value of the PC is 0x00000000, can you use a single jump instruction to get to the PC address as shown in Exercise 2.39?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free