Question

In: Computer Science

Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...

Write the MIPS assembly version of this C code:

int weird(char[] s, int x)
{
  int i;
  for (i = 0; i < x; i++) { 
    if (s[i] == ‘A’)
    {
        return power(10, i);
    }
  }
  return -1;
}

int power(int base, int i)
{
    int j = 0;
    while (j < base)
    {
        base = base * base;
        j++;
    }
    return base;
}

Solutions

Expert Solution

MIPS GCC 5.4 Code for the given c code:

power(int, int):

addiu $sp,$sp,-24

sw $fp,20($sp)

move $fp,$sp

sw $4,24($fp)

sw $5,28($fp)

sw $0,8($fp)

$L3:

lw $3,8($fp)

lw $2,24($fp)

nop

slt $2,$3,$2

beq $2,$0,$L2

nop

lw $3,24($fp)

lw $2,24($fp)

nop

mult $3,$2

mflo $2

sw $2,24($fp)

lw $2,8($fp)

nop

addiu $2,$2,1

sw $2,8($fp)

b $L3

nop

$L2:

lw $2,24($fp)

move $sp,$fp

lw $fp,20($sp)

addiu $sp,$sp,24

j $31

nop

weird(char*, int):

addiu $sp,$sp,-40

sw $31,36($sp)

sw $fp,32($sp)

move $fp,$sp

sw $4,40($fp)

sw $5,44($fp)

sw $0,24($fp)

$L9:

lw $3,24($fp)

lw $2,44($fp)

nop

slt $2,$3,$2

beq $2,$0,$L6

nop

lw $2,24($fp)

lw $3,40($fp)

nop

addu $2,$3,$2

lb $3,0($2)

li $2,65 # 0x41

bne $3,$2,$L7

nop

lw $5,24($fp)

li $4,10 # 0xa

jal power(int, int)

nop

b $L8

nop

$L7:

lw $2,24($fp)

nop

addiu $2,$2,1

sw $2,24($fp)

b $L9

nop

$L6:

li $2,-1 # 0xffffffffffffffff

$L8:

move $sp,$fp

lw $31,36($sp)

lw $fp,32($sp)

addiu $sp,$sp,40

j $31

nop

if you like the answer please provide a thumbs up.


Related Solutions

convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1,...
Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1, int b2, int h2); int Area(int b, int h); int main(int argc, char **argv) { int base1, base2, height1, height2; base1=10; base2=12; height1=8; height2=7; Compare(base1, height1, base2, height2); return 0; } void Compare(int b1, int h1, int b2, int h2) { int A1=Area(b1, h1); int A2=Area(b2, h2); if (A1>=A2) printf("Area1 is greater than Area2.\n"); else printf("Area2 is greater than Area1.\n"); } int Area(int b,...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string */ if (c == 0) return s; /* convert the first character to upper case*/ if (c >= ‘a’ && d <= ‘z’) { c -= 32; s[0] = c; } /* convert the remaining characters*/ strtoupper(s + 1); return s; }
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT