1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Phoenix [80]
2 years ago
5

Translate the following C++ program to MIPS assembly program. *Please explain each instruction of your code by a comment and sub

mit a .asm file*#include using namespace std; void myfunction(int arr[], int n) { int writes = 0; for(int start = 0; start <= n-2; start++){ int item = arr[start]; int pos = start; for(int i = start+1; i< n; i++) if (arr[i]
Computers and Technology
1 answer:
maw [93]2 years ago
5 0

Answer:

Check the explanation

Explanation:

ASM of MIPS gcc 5.4:

myfunction(int*, int):

       addiu   $sp,$sp,-56

       sw      $31,52($sp)

       sw      $fp,48($sp)

       move    $fp,$sp

       sw      $4,56($fp)

       sw      $5,60($fp)

       sw      $0,24($fp)

       sw      $0,28($fp)

$L16:

       lw      $2,60($fp)

       nop

       addiu   $3,$2,-2

       lw      $2,28($fp)

       nop

       slt     $2,$3,$2

       bne     $2,$0,$L17

       nop

       lw      $2,28($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $2,0($2)

       nop

       sw      $2,44($fp)

       lw      $2,28($fp)

       nop

       sw      $2,32($fp)

       lw      $2,28($fp)

       nop

       addiu $2,$2,1

       sw      $2,36($fp)

$L5:

       lw      $3,36($fp)

       lw      $2,60($fp)

       nop

       slt     $2,$3,$2

       beq     $2,$0,$L3

       nop

       lw      $2,36($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $3,0($2)

       lw      $2,44($fp)

       nop

       slt     $2,$3,$2

       beq     $2,$0,$L4

       nop

       lw      $2,32($fp)

       nop

       addiu   $2,$2,1

       sw      $2,32($fp)

$L4:

       lw      $2,36($fp)

       nop

       addiu   $2,$2,1

       sw      $2,36($fp)

       b       $L5

       nop

$L3:

       lw      $3,32($fp)

       lw      $2,28($fp)

       nop

       beq     $3,$2,$L18

       nop

$L6:

       lw      $2,32($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $3,0($2)

       lw      $2,44($fp)

       nop

       bne     $3,$2,$L8

       nop

       lw      $2,32($fp)

       nop

       addiu   $2,$2,1

       sw      $2,32($fp)

       b       $L6

       nop

$L8:

       lw      $3,32($fp)

       lw      $2,28($fp)

       nop

       beq     $3,$2,$L9

       nop

       lw      $2,32($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $3,$3,$2

       addiu   $2,$fp,44

       move    $5,$3

       move    $4,$2

       jal     void std::swap<int>(int&, int&)

       nop

       lw      $2,24($fp)

       nop

       addiu   $2,$2,1

       sw      $2,24($fp)

$L9:

       lw      $3,32($fp)

       lw      $2,28($fp)

       nop

       beq     $3,$2,$L7

       nop

       lw      $2,28($fp)

       nop

       sw      $2,32($fp)

       lw      $2,28($fp)

       nop

       addiu   $2,$2,1

       sw      $2,40($fp)

$L13:

       lw      $3,40($fp)

       lw      $2,60($fp)

       nop

       slt     $2,$3,$2

       beq     $2,$0,$L11

       nop

       lw      $2,40($fp)

     nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $3,0($2)

       lw      $2,44($fp)

       nop

       slt     $2,$3,$2

       beq     $2,$0,$L12

       nop

       lw      $2,32($fp)

       nop

       addiu   $2,$2,1

       sw      $2,32($fp)

$L12:

       lw      $2,40($fp)

       nop

       addiu   $2,$2,1

       sw      $2,40($fp)

       b       $L13

       nop

$L11:

       lw      $2,32($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $3,0($2)

       lw      $2,44($fp)

       nop

       bne     $3,$2,$L14

       nop

       lw      $2,32($fp)

       nop

       addiu   $2,$2,1

       sw      $2,32($fp)

       b       $L11

       nop

$L14:

       lw      $2,32($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $2,$3,$2

       lw      $3,0($2)

       lw      $2,44($fp)

       nop

       beq     $3,$2,$L9

       nop

       lw      $2,32($fp)

       nop

       sll     $2,$2,2

       lw      $3,56($fp)

       nop

       addu    $3,$3,$2

       addiu   $2,$fp,44

       move    $5,$3

       move    $4,$2

       jal     void std::swap<int>(int&, int&)

       nop

       lw      $2,24($fp)

       nop

       addiu   $2,$2,1

       sw      $2,24($fp)

       b       $L9

       nop

$L18:

       nop

$L7:

       lw      $2,28($fp)

       nop

       addiu   $2,$2,1

       sw      $2,28($fp)

       b       $L16

       nop

$L17:

       nop

       move    $sp,$fp

       lw      $31,52($sp)

       lw      $fp,48($sp)

       addiu   $sp,$sp,56

       j       $31

       nop

$LC0:

       .ascii "array:\000"

$LC1:

       .ascii " \000"

main:

       addiu   $sp,$sp,-72

       sw      $31,68($sp)

       sw      $fp,64($sp)

       move    $fp,$sp

       li      $2,1                        # 0x1

       sw      $2,32($fp)

       li      $2,8                        # 0x8

       sw      $2,36($fp)

       li      $2,3                        # 0x3

       sw      $2,40($fp)

       li      $2,9                        # 0x9

       sw      $2,44($fp)

       li      $2,10                 # 0xa

       sw      $2,48($fp)

       li      $2,2                        # 0x2

       sw      $2,52($fp)

       li      $2,4                        # 0x4

       sw      $2,56($fp)

       li      $2,7                        # 0x7

       sw      $2,28($fp)

       addiu   $2,$fp,32

       lw      $5,28($fp)

       move    $4,$2

       jal     myfunction(int*, int)

       nop

       lui     $2,%hi($LC0)

       addiu   $5,$2,%lo($LC0)

       lui     $2,%hi(_ZSt4cout)

       addiu   $4,$2,%lo(_ZSt4cout)

       jal     std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

       nop

       move    $3,$2

       lui     $2,%hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

       addiu   $5,$2,%lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

       move    $4,$3

       jal     std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

       nop

       sw      $0,24($fp)

$L21:

       lw      $3,24($fp)

       lw      $2,28($fp)

       nop

       slt     $2,$3,$2

       beq     $2,$0,$L20

       nop

       lw      $2,24($fp)

       nop

       sll     $2,$2,2

       addiu   $3,$fp,24

       addu    $2,$3,$2

       lw      $2,8($2)

       nop

       move    $5,$2

       lui     $2,%hi(_ZSt4cout)

       addiu   $4,$2,%lo(_ZSt4cout)

       jal     std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

       nop

       move    $3,$2

       lui     $2,%hi($LC1)

       addiu   $5,$2,%lo($LC1)

       move    $4,$3

       jal     std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

       nop

       lw      $2,24($fp)

       nop

       addiu   $2,$2,1

       sw      $2,24($fp)

       b       $L21

       nop

$L20:

       move    $2,$0

       move    $sp,$fp

       lw      $31,68($sp)

       lw      $fp,64($sp)

       addiu   $sp,$sp,72

       j       $31

       nop

void std::swap<int>(int&, int&):

       addiu   $sp,$sp,-24

       sw      $fp,20($sp)

       move    $fp,$sp

       sw      $4,24($fp)

       sw      $5,28($fp)

       lw      $2,24($fp)

       nop

       lw      $2,0($2)

       nop

       sw      $2,8($fp)

       lw      $2,28($fp)

       nop

       lw      $3,0($2)

       lw      $2,24($fp)

       nop

       sw      $3,0($2)

       lw      $2,28($fp)

       lw      $3,8($fp)

       nop

       sw      $3,0($2)

       nop

       move    $sp,$fp

       lw      $fp,20($sp)

       addiu   $sp,$sp,24

       j       $31

       nop

__static_initialization_and_destruction_0(int, int):

       addiu   $sp,$sp,-32

       sw      $31,28($sp)

       sw      $fp,24($sp)

      move    $fp,$sp

       sw      $4,32($fp)

       sw      $5,36($fp)

       lw      $3,32($fp)

       li      $2,1                        # 0x1

       bne     $3,$2,$L26

       nop

       lw      $3,36($fp)

       li      $2,65535                    # 0xffff

       bne     $3,$2,$L26

       nop

       lui     $2,%hi(_ZStL8__ioinit)

       addiu   $4,$2,%lo(_ZStL8__ioinit)

       jal     std::ios_base::Init::Init() [complete object constructor]

       nop

       lui     $2,%hi(__dso_handle)

       addiu   $6,$2,%lo(__dso_handle)

       lui     $2,%hi(_ZStL8__ioinit)

       addiu   $5,$2,%lo(_ZStL8__ioinit)

       lui     $2,%hi(_ZNSt8ios_base4InitD1Ev)

       addiu   $4,$2,%lo(_ZNSt8ios_base4InitD1Ev)

       jal     __cxa_atexit

       nop

$L26:

       nop

       move    $sp,$fp

       lw      $31,28($sp)

       lw      $fp,24($sp)

       addiu   $sp,$sp,32

       j       $31

       nop

You might be interested in
Consider the following general code for allowing access to a resource:
Margaret [11]

Answer:

a) The code allows access even when  IsAccessAllowed(...) method fails.

b) Either use If

(dwRet = ACCESS_ALLOWED)

or use

if (dwRet == NO_ERROR)

to avoid flaw

Explanation:

Lets first see what the code chunk does:

DWORD dwRet = IsAccessAllowed(...);

if (dwRet == ERROR_ACCESS_DENIED) {

// Security check failed.

// Inform user that access is denied.

} else {

// Security check OK.

}

In the given code, DWORD is basically a data type for double word type integers and this is defined in windows.h

So there is DWORD type variable dwRet that is assigned a method calls.

The method is IsAccessAllowed() which checks if the access is allowed to user.

if (dwRet == ERROR_ACCESS_DENIED) condition basically checks if the value of DWORD type variable dwRet is equal to ERROR_ACCESS_DENIED

If this condition evaluates to true then the security checks fails and user is informed via some message or action that the access is denied.  But when the if condition evaluates to false then the else part executes which allows access.

So basically this chunk of code checks if the error ERROR_ACCESS_DENIED is returned.

Now the flaw in this program is what if the method IsAccessAllowed() by any reason. The reasons can be system failure or the memory failure. In memory failure case for example, the system returns out of memory error. So this means that the error is not  ERROR_ACCESS_DENIED. Instead it is out of memory error. So in such a case the user is allowed access as the if condition evaluates to false and else part executes. So if any other error is produced due to some reason like mentioned above, then user has unrestricted access.

This shows that the doe should not check for the failure or rely on checking ERROR_ACCESS_DENIED to allow access but instead it should check for success. Code should only give access privilege if access is allowed successfully or no error is produced.

So to avoid this flaw the code is altered as:

DWORD dwRet = IsAccessAllowed(...);

If (dwRet = ACCESS_ALLOWED) {

//Security check OK.

} else {

//Security check failed.

//Inform user that access is denied.

}

This will only allow access if ACCESS_ALLOWED evaluates to true and success is checked instead of failure here

You can also alter the if condition as:

If (dwRet = No_Error)

or

If (dwRet = 0)

The above if conditions checks if the access is allowed or if no error is produced. Only then it will allowed access otherwise not. So the access check is a success is checked first and failure (for any reason). The user is allowed access only if there is no error otherwise user is not allowed access.

6 0
2 years ago
Select the most likely outcome of making only on-time minimum payments to a credit
Serjik [45]
The answer is B. Your will have gone mostly towards paying interest and you will still owe the majority of the balance that you had from ago
4 0
3 years ago
_____ rows indicate that there is different formatting for odd and even rows.
Sladkaya [172]
Banded rows indicate that there are different formatting for odd and even rows.
5 0
3 years ago
In JAVA, answer the following:
erica [24]

Answer:

The JAVA program is as follows.

import java.util.Scanner;

public class Program

{

   static int n;

public static void main(String[] args) {

    //scanner object

    Scanner stdin = new Scanner(System.in);

    //loop executes till number entered is in the range of 1 to 10

    do

    {

        System.out.print("Enter any number: ");

        n = stdin.nextInt();

    }while(n<1 || n>10);

    System.out.println("Number is valid. Exiting...");

}

}

OUTPUT

Enter any number: 0

Enter any number: 23

Enter any number: 10

Number is valid. Exiting...

Explanation:

The program is explained.

1. The integer variable, n, is declared static since the variable should be accessible inside main() which is static.

2. An object of Scanner, stdin, is created inside main().

3. Inside do-while loop, the user is asked input any value for n.

4. The loop continues till user inputs a number which is not in the given range, beginning from 1 to 10.

5. The loop will not be terminated till the user enters a valid input.

6. Once a valid input is entered, the message is displayed to the console and the program ends.

7. The variable, n, is declared at the class level and is a class variable. Class variables are declared inside the class but outside all the methods in that class.

8. Since JAVA is a purely object-oriented language, all the code is written inside class.

9. The object of class is not created since only one class is included in the program. If more than one class is included, then the object of the class which does not has the main() method will be created inside main().

10. User input for integer is taken via scanner object and using nextInt() method.

11. The methods, print() and println() display the messages to the console. The only difference is that println() inserts a new line after displaying the message.

12. The name of the class having main() and the name of the program should be the same.

7 0
2 years ago
Plz help
Rama09 [41]

Answer:

name

Explanation:

name =

is an assignment to the variable called 'name'.

5 0
2 years ago
Read 2 more answers
Other questions:
  • ​printers, monitors,​ tablets, cpus, and laptops are examples of​ ____________.
    14·1 answer
  • A popular encryption method used to protect data that travel over a wireless network is ____
    12·1 answer
  • How to buy free big money computers that earn free money everydays?
    15·1 answer
  • Which two components in a system might make a loud whining noise when there is a problem? Why?
    14·1 answer
  • A healthcare organization received notification that a hospital employee’s laptop that contained PHI was inadvertently left at a
    14·1 answer
  • Beyond adding equations, what else does the Insert Equation feature allow users to do?
    7·1 answer
  • What is the code for this please?​
    13·1 answer
  • A list is sorted in ascending order if it is empty or each item except the last one is less than or equal to its successor.
    14·1 answer
  • I can talk to you! How about we talk through the you know where people comment and say stuff about the question1
    10·2 answers
  • How to change the microsoft word pages to black background
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!