Question

translate c++ to mips bool singleton(int value) { //Check condition if (value == 0) //Return return...

translate c++ to mips


bool
singleton(int value) {
//Check condition
if (value == 0)

//Return
return false;

//Declare and initialize
int compare = value-1;
bool isTrue = value & compare;

//Check condition
if (isTrue == 0)

//Return
return true;

//Otherwise
else

//Return
return false;
}

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

MIPS Code:

singleton(int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
cmp DWORD PTR [rbp-20], 0
jne .L2
mov eax, 0
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-20]
sub eax, 1
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-20]
and eax, DWORD PTR [rbp-4]
test eax, eax
setne al
mov BYTE PTR [rbp-5], al
movzx eax, BYTE PTR [rbp-5]
test eax, eax
jne .L4
mov eax, 1
jmp .L3
.L4:
mov eax, 0
.L3:
pop rbp
ret

Code Screenshot:

Line-wise Explanation:

1) bool singleton(int value)

push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi

2) if (value == 0)
return false;

cmp DWORD PTR [rbp-20], 0
jne .L2

3) int compare = value-1;

mov eax, DWORD PTR [rbp-20]
sub eax, 1
mov DWORD PTR [rbp-4], eax

4) bool isTrue = value & compare;

mov eax, DWORD PTR [rbp-20]
and eax, DWORD PTR [rbp-4]
test eax, eax
setne al
mov BYTE PTR [rbp-5], al

5) if (isTrue == 0)
return true;

movzx eax, BYTE PTR [rbp-5]
test eax, eax
jne .L4
mov eax, 1
jmp .L3

6) else
return false;

.L4:
mov eax, 0

7) End of the program

.L3:
pop rbp
ret

Note: For any doubt please feel free to comment.

Add a comment
Know the answer?
Add Answer to:
translate c++ to mips bool singleton(int value) { //Check condition if (value == 0) //Return return...
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
  • translate c++ to mips int get_singleton(int value) { //Declare and initialize int position = 0; bool...

    translate c++ to mips int get_singleton(int value) { //Declare and initialize int position = 0; bool isFound = false; //Loop while(!isFound) { //Update isFound = 1 & value; value = value >> 1; //Increment position++; } //Return return position; }

  • C to MIPS Conversion C variable h i j k x int a[] or &a[0] MIPS...

    C to MIPS Conversion C variable h i j k x int a[] or &a[0] MIPS register replacement $s0 $s1 $s2 $s3 $s4 $a0 Translate to MIPS. DO NOT USE pseudo MIPS instructions (e.g., BGE). Answer MUST use true 32-bit MIPS instructions: if(j < k ) a[j] = 1; else j = a[j];

  • For C to MIPS Conversion C variable h i j k x int a[] or &a[0]...

    For C to MIPS Conversion C variable h i j k x int a[] or &a[0] MIPS register replacement $s0 $s1 $s2 $s3 $s4 $a0 Translate to MIPS. No credit for pseudo-MIPS instructions (e.g., BGE). The answer MUST use true 32-bit MIPS instructions: if(j < k ) a[j] = 1; else j = a[j];

  • For C to MIPS Conversion h i u. k х C variable int a[] or &a[0]...

    For C to MIPS Conversion h i u. k х C variable int a[] or &a[0] $50 $51 $52 $s3 $54 $a0 MIPS register replacement Translate to MIPS. No credit for pseudo MIPS instructions (e.g., BGE). Answer MUST use true 32-bit MIPS instructions: if(j <k) a[j] = 1; else j = a[j];

  • Help me translate this C code into MIPS int set(int n, int a[], int v) {...

    Help me translate this C code into MIPS int set(int n, int a[], int v) { int i; for (i=4; i < (n-1); i++){ a[i] = v; } return i; }

  • Translate the following C code to MIPS assembly code. Question 1) int counter = 0; void...

    Translate the following C code to MIPS assembly code. Question 1) int counter = 0; void change_global(int value) { counter = counter + value; } void main() { change_global(5); change_global(10); }

  • C++ Need the step count for this function. int binarySearch(const int array[], int size, int value)...

    C++ Need the step count for this function. int binarySearch(const int array[], int size, int value) { int first = 0, last = size − 1, middle, position = −1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle − 1; else first = middle + 1; } return position; }

  • For C to MIPS Conversion C variable h i j k int a[] or &a[0] MIPS...

    For C to MIPS Conversion C variable h i j k int a[] or &a[0] MIPS register replacement $50 $s1 $s2 $53 $54 $a0 Translate to MIPS. No credit for pseudo MIPS instructions (e.g., BGE). Answer MUST use true 32-bit MIPS instructions: Note that all variables (h,ij.x,a[]) are 32-bit signed integers. while ( h < 3 ) { a[j++] = 0; x = i >> 3; ܒܝܢ

  • C to MIPS Conversion C variable h i j k x int a[] or &a[0] MIPS...

    C to MIPS Conversion C variable h i j k x int a[] or &a[0] MIPS register replacement $s0 $s1 $s2 $s3 $s4 $a0 Translate to MIPS. DO NOT USE pseudo MIPS instructions (e.g., BGE). Answer MUST use true 32-bit MIPS instructions: Note that all variables (h,i,j,x,a[]) are 32-bit signed integers. while ( h < 3 ) { a[j++]= 0; x = i >> 3; }

  • QUESTION 1 What is the output of the following code snippet? int main() { bool attendance...

    QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...

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