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
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
ADVERTISEMENT