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;
}
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.
translate c++ to mips bool singleton(int value) { //Check condition if (value == 0) //Return return...
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 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] 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] $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) { int i; for (i=4; i < (n-1); i++){ a[i] = v; } return i; }
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 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 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 = 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...
Convert the following program into MIPS Instructions: int a = 0; int b = 10; int c = 100; result = isSorted(a, b, c); if (!result) isSorted(c, b, a); int isSorted(int a, int b, int c) {if (a < b && b < c) return 1; else return 0;}