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

Create MIPS assembly code for the following C function. It is an insertion sort

ID: 3666547 • Letter: C

Question

Create MIPS assembly code for the following C function. It is an insertion sort program You don't need to generate any code for the include, namespace, and function pro- totype lines. Assume the random number function is at memory address RAND and don't generate code for it. Remember that any local values in temporary registers will NOT be maintained across function calls. If you use a saved register, you must main- tain the original value by placing it on the stack, and then restoring the original value at the end. It might be easier to write the shift function first, then write the main function

Explanation / Answer

Answer:

Assembly Code :

   .Ltext0:
        .globl   main
    main:
    .LFB0:
        .cfi_startproc
0000 55        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
0001 4889E5        movq   %rsp, %rbp
        .cfi_def_cfa_register 6
0004 4881ECB0        subq   $4016, %rsp

    .LBB2:
000b C78550F0        movl   $0, -4016(%rbp)

0015 EB1A        jmp   .L2
    .L3:
002a 838550F0        addl   $1, -4016(%rbp)
  
0017 8B8550F0        movl   -4016(%rbp), %eax

001d 4898        cltq
001f C7848560        movl   $2001, -4000(%rbp,%rax,4)

    .L2:
0031 81BD50F0        cmpl   $999, -4016(%rbp)

003b 7EDA        jle   .L3
    .LBE2:
    .LBB3:
003d C78554F0        movl   $0, -4012(%rbp)
  
0047 E9B00000        jmp   .L4

    .L8:
    .LBB4:
004c B8000000        movl   $0, %eax

0051 E8000000        call   rand

0056 89C1        movl   %eax, %ecx
0058 BAD34D62        movl   $274877907, %edx

005d 89C8        movl   %ecx, %eax
005f F7EA        imull   %edx
0061 C1FA07        sarl   $7, %edx
0064 89C8        movl   %ecx, %eax
0066 C1F81F        sarl   $31, %eax
0069 29C2        subl   %eax, %edx
006b 89D0        movl   %edx, %eax
006d 89855CF0        movl   %eax, -4004(%rbp)

0073 8B855CF0        movl   -4004(%rbp), %eax

0079 69C0D007        imull   $2000, %eax, %eax

007f 29C1        subl   %eax, %ecx
0081 89C8        movl   %ecx, %eax
0083 89855CF0        movl   %eax, -4004(%rbp)
  
    .LBB5:
0089 C78558F0        movl   $0, -4008(%rbp)
  
0093 EB54        jmp   .L5
    .L7:
0095 8B8558F0        movl   -4008(%rbp), %eax

009b 4898        cltq
009d 8B848560        movl   -4000(%rbp,%rax,4), %eax

00a4 3B855CF0        cmpl   -4004(%rbp), %eax

00aa 7E36        jle   .L6
00ac 8B9558F0        movl   -4008(%rbp), %edx

00b2 488D8560        leaq   -4000(%rbp), %rax

00b9 89D6        movl   %edx, %esi
00bb 4889C7        movq   %rax, %rdi
00be E8000000        call   shift

00c3 8B8558F0        movl   -4008(%rbp), %eax

00c9 4898        cltq
00cb 8B955CF0        movl   -4004(%rbp), %edx

00d1 89948560        movl   %edx, -4000(%rbp,%rax,4)

00d8 C78558F0        movl   $1000, -4008(%rbp)
    .L6:
00e2 838558F0        addl   $1, -4008(%rbp)
    .L5:
00e9 81BD58F0        cmpl   $999, -4008(%rbp)
00f3 7EA0        jle   .L7
    .LBE5:
    .LBE4:
00f5 838554F0        addl   $1, -4012(%rbp)
    .L4:
00fc 81BD54F0        cmpl   $999, -4012(%rbp)
  
0106 0F8E40FF        jle   .L8

    .LBE3:
010c B8000000        movl   $0, %eax
  
0111 C9        leave
        .cfi_def_cfa 7, 8
0112 C3        ret
        .cfi_endproc
    .LFE0:
        .globl   shift
    shift:
    .LFB1:
        .cfi_startproc
0113 55        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
0114 4889E5        movq   %rsp, %rbp
        .cfi_def_cfa_register 6
0117 48897DE8        movq   %rdi, -24(%rbp)
011b 8975E4        movl   %esi, -28(%rbp)
    .LBB6:
011e C745FCE7        movl   $999, -4(%rbp)

0125 EB30        jmp   .L11
    .L12:
0153 836DFC01        subl   $1, -4(%rbp)
0127 8B45FC        movl   -4(%rbp), %eax
012a 4898        cltq
012c 488D1485        leaq   0(,%rax,4), %rdx

0134 488B45E8        movq   -24(%rbp), %rax
0138 4801C2        addq   %rax, %rdx
013b 8B45FC        movl   -4(%rbp), %eax
013e 4898        cltq
0140 48C1E002        salq   $2, %rax
0144 488D48FC        leaq   -4(%rax), %rcx
0148 488B45E8        movq   -24(%rbp), %rax
014c 4801C8        addq   %rcx, %rax
014f 8B00        movl   (%rax), %eax
0151 8902        movl   %eax, (%rdx)
    .L11:
0157 8B45FC        movl   -4(%rbp), %eax
015a 3B45E4        cmpl   -28(%rbp), %eax
015d 7FC8        jg   .L12
    .LBE6:
015f 5D        popq   %rbp
        .cfi_def_cfa 7, 8
0160 C3        ret
        .cfi_endproc
    .LFE1:
    .Letext0: