Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Translate The Following Code To MIPS Assembly #include <stdio.h> #include <stdli

ID: 3835107 • Letter: T

Question

Translate The Following Code To MIPS Assembly

#include <stdio.h>
#include <stdlib.h>

int collatz(int num, int wor)
{
   if (num == 1)
   {
       return wor;
   }
   else if (num % 2 == 0)
   {
       wor = wor + 1;
       num = num / 2;
       printf("%d ", num);
       collatz(num, wor);
   }
   else
   {
       wor = wor + 1;
       num = 3 * num + 1;
       printf("%d ", num);
       collatz(num, wor);
   }
}

int main()
{
   int num;
   int wor = 0;
   printf("xxxxxxxx xxxxxx ");
   printf("Enter an integer greater than 1: ");
   scanf_s("%d", &num);
   printf("%d ", num);
   printf("Conjecture verified in %d iterations ", collatz(num, wor));

   return 0;
}

Explanation / Answer

.file   1 ""
   .section .mdebug.abi32
   .previous
   .gnu_attribute 4, 1
   .abicalls
   .rdata
   .align   2
$LC0:
   .ascii   "%d "
   .text
   .align   2
   .globl   _Z7collatzii
$LFB0 = .
   .set   nomips16
   .ent   _Z7collatzii
   .type   _Z7collatzii, @function
_Z7collatzii:
   .frame   $fp,32,$31       # vars= 0, regs= 2/0, args= 16, gp= 8
   .mask   0xc0000000,-4
   .fmask   0x00000000,0
   .set   noreorder
   .cpload   $25
   .set   nomacro
  
   addiu   $sp,$sp,-32
$LCFI0:
   sw   $31,28($sp)
$LCFI1:
   sw   $fp,24($sp)
   movz   $31,$31,$0
$LCFI2:
   move   $fp,$sp
$LCFI3:
   .cprestore   16
   sw   $4,32($fp)
   sw   $5,36($fp)
   lw   $3,32($fp)
   li   $2,1           # 0x1
   bne   $3,$2,$L2
   nop

   lw   $2,36($fp)
   nop
   move   $3,$2
   move   $2,$3
   b   $L5
   nop

$L2:
   lw   $2,32($fp)
   nop
   andi   $2,$2,0x1
   bne   $2,$0,$L3
   nop

   lw   $2,36($fp)
   nop
   addiu   $2,$2,1
   sw   $2,36($fp)
   lw   $2,32($fp)
   nop
   srl   $3,$2,31
   addu   $2,$3,$2
   sra   $2,$2,1
   sw   $2,32($fp)
   lw   $2,%got($LC0)($28)
   nop
   addiu   $4,$2,%lo($LC0)
   lw   $5,32($fp)
   lw   $2,%call16(printf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   lw   $4,32($fp)
   lw   $5,36($fp)
   lw   $2,%got(_Z7collatzii)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   b   $L6
   nop

$L3:
   lw   $2,36($fp)
   nop
   addiu   $2,$2,1
   sw   $2,36($fp)
   lw   $3,32($fp)
   nop
   move   $2,$3
   sll   $2,$2,1
   addu   $2,$2,$3
   addiu   $2,$2,1
   sw   $2,32($fp)
   lw   $2,%got($LC0)($28)
   nop
   addiu   $4,$2,%lo($LC0)
   lw   $5,32($fp)
   lw   $2,%call16(printf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   lw   $4,32($fp)
   lw   $5,36($fp)
   lw   $2,%got(_Z7collatzii)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
$L6:
$L5:
   move   $sp,$fp
   lw   $31,28($sp)
   lw   $fp,24($sp)
   addiu   $sp,$sp,32
   j   $31
   nop

   .set   macro
   .set   reorder
   .end   _Z7collatzii
$LFE0:
   .size   _Z7collatzii, .-_Z7collatzii
   .rdata
   .align   2
$LC1:
   .ascii   "xxxxxxxx xxxxxx"
   .align   2
$LC2:
   .ascii   "Enter an integer greater than 1: "
   .align   2
$LC3:
   .ascii   "%d"
   .align   2
$LC4:
   .ascii   "Conjecture verified in %d iterations "
   .text
   .align   2
   .globl   main
$LFB1 = .
   .set   nomips16
   .ent   main
   .type   main, @function
main:
   .frame   $fp,40,$31       # vars= 8, regs= 2/0, args= 16, gp= 8
   .mask   0xc0000000,-4
   .fmask   0x00000000,0
   .set   noreorder
   .cpload   $25
   .set   nomacro
  
   addiu   $sp,$sp,-40
$LCFI4:
   sw   $31,36($sp)
$LCFI5:
   sw   $fp,32($sp)
   movz   $31,$31,$0
$LCFI6:
   move   $fp,$sp
$LCFI7:
   .cprestore   16
   sw   $0,24($fp)
   lw   $2,%got($LC1)($28)
   nop
   addiu   $4,$2,%lo($LC1)
   lw   $2,%call16(puts)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   nop
   lw   $2,%got($LC2)($28)
   nop
   addiu   $4,$2,%lo($LC2)
   lw   $2,%call16(printf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   addiu   $2,$fp,28
   lw   $3,%got($LC3)($28)
   nop
   addiu   $4,$3,%lo($LC3)
   move   $5,$2
   lw   $2,%call16(scanf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   lw   $2,28($fp)
   lw   $3,%got($LC0)($28)
   nop
   addiu   $4,$3,%lo($LC0)
   move   $5,$2
   lw   $2,%call16(printf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   lw   $2,28($fp)
   nop
   move   $4,$2
   lw   $5,24($fp)
   lw   $2,%got(_Z7collatzii)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   nop
   lw   $3,%got($LC4)($28)
   nop
   addiu   $4,$3,%lo($LC4)
   move   $5,$2
   lw   $2,%call16(printf)($28)
   nop
   move   $25,$2
   jalr   $25
   nop

   lw   $28,16($fp)
   move   $2,$0
   move   $sp,$fp
   lw   $31,36($sp)
   lw   $fp,32($sp)
   addiu   $sp,$sp,40
   j   $31
   nop

   .set   macro
   .set   reorder
   .end   main
$LFE1:
   .size   main, .-main
   .section   .eh_frame,"aw",@progbits
$Lframe1:
   .4byte   $LECIE1-$LSCIE1
$LSCIE1:
   .4byte   0x0
   .byte   0x1
   .globl   __gxx_personality_v0
   .ascii   "zP"
   .uleb128 0x1
   .sleb128 -4
   .byte   0x1f
   .uleb128 0x5
   .byte   0x0
   .4byte   __gxx_personality_v0
   .byte   0xc
   .uleb128 0x1d
   .uleb128 0x0
   .align   2
$LECIE1:
$LSFDE1:
   .4byte   $LEFDE1-$LASFDE1
$LASFDE1:
   .4byte   $LASFDE1-$Lframe1
   .4byte   $LFB0
   .4byte   $LFE0-$LFB0
   .uleb128 0x0
   .byte   0x4
   .4byte   $LCFI0-$LFB0
   .byte   0xe
   .uleb128 0x20
   .byte   0x4
   .4byte   $LCFI2-$LCFI0
   .byte   0x11
   .uleb128 0x1e
   .sleb128 2
   .byte   0x11
   .uleb128 0x1f
   .sleb128 1
   .byte   0x4
   .4byte   $LCFI3-$LCFI2
   .byte   0xd
   .uleb128 0x1e
   .align   2
$LEFDE1:
$LSFDE3:
   .4byte   $LEFDE3-$LASFDE3
$LASFDE3:
   .4byte   $LASFDE3-$Lframe1
   .4byte   $LFB1
   .4byte   $LFE1-$LFB1
   .uleb128 0x0
   .byte   0x4
   .4byte   $LCFI4-$LFB1
   .byte   0xe
   .uleb128 0x28
   .byte   0x4
   .4byte   $LCFI6-$LCFI4
   .byte   0x11
   .uleb128 0x1e
   .sleb128 2
   .byte   0x11
   .uleb128 0x1f
   .sleb128 1
   .byte   0x4
   .4byte   $LCFI7-$LCFI6
   .byte   0xd
   .uleb128 0x1e
   .align   2
$LEFDE3:
   .ident   "GCC: (Debian 4.4.5-8) 4.4.5"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote