Write a MIPS assembly program that sorts an array using bubble sort translating
ID: 3761967 • Letter: W
Question
Write a MIPS assembly program that sorts an array using bubble sort translating
the C code below
----------------------------------
int main(void)
{
int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array
int arraySize = sizeof(array)/sizeof(array[0]);
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
//Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm
j++; // j= sorted elements
int i=0;
/* "arraySize - j" is used because after the first run of the array , the last element of the
array will be sorted , so j=1 and now we have to sort arraySize-1 elements, then after
the second run of the array the next to last element will be sorted so j=2 and now we
have to sort arraySize-2 elements. */
for ( i = 0; i < arraySize - j; i++) {
if (array[i] > array[i + 1]) {
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
int i=0;
//print sorted array
for(i=0; i < arraySize; i++){
printf("%d", array[i]);
}
return 0;
}
Explanation / Answer
.Ltext0: .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .globl main main: .LFB971: .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 4883EC40 subq $64, %rsp .LBB2: 0008 C745E00A movl $10, -32(%rbp) 000000 000f C745E402 movl $2, -28(%rbp) 000000 0016 C745E807 movl $7, -24(%rbp) 000000 001d C745EC05 movl $5, -20(%rbp) 000000 0024 C745F00F movl $15, -16(%rbp) 000000 002b C745F41E movl $30, -12(%rbp) 000000 0032 C745F808 movl $8, -8(%rbp) 000000 0039 C745FC06 movl $6, -4(%rbp) 000000 0040 B8080000 movl $8, %eax 00 0045 8945D8 movl %eax, -40(%rbp) 0048 C645CB01 movb $1, -53(%rbp) 004c C745CC00 movl $0, -52(%rbp) 000000 0053 EB78 jmp .L2 .L6: .LBB3: 0055 C645CB00 movb $0, -53(%rbp) 0059 8345CC01 addl $1, -52(%rbp) 005d C745D000 movl $0, -48(%rbp) 000000 0064 C745D000 movl $0, -48(%rbp) 000000 006b EB51 jmp .L3 .L5: 006d 8B45D0 movl -48(%rbp), %eax 0070 4898 cltq 0072 8B5485E0 movl -32(%rbp,%rax,4), %edx 0076 8B45D0 movl -48(%rbp), %eax 0079 83C001 addl $1, %eax 007c 4898 cltq 007e 8B4485E0 movl -32(%rbp,%rax,4), %eax 0082 39C2 cmpl %eax, %edx 0084 7E34 jle .L4 0086 8B45D0 movl -48(%rbp), %eax 0089 4898 cltq 008b 8B4485E0 movl -32(%rbp,%rax,4), %eax 008f 8945DC movl %eax, -36(%rbp) 0092 8B45D0 movl -48(%rbp), %eax 0095 83C001 addl $1, %eax 0098 4898 cltq 009a 8B5485E0 movl -32(%rbp,%rax,4), %edx 009e 8B45D0 movl -48(%rbp), %eax 00a1 4898 cltq 00a3 895485E0 movl %edx, -32(%rbp,%rax,4) 00a7 8B45D0 movl -48(%rbp), %eax 00aa 83C001 addl $1, %eax 00ad 4898 cltq 00af 8B55DC movl -36(%rbp), %edx 00b2 895485E0 movl %edx, -32(%rbp,%rax,4) 00b6 C645CB01 movb $1, -53(%rbp) .L4: 00ba 8345D001 addl $1, -48(%rbp) .L3: 00be 8B45CC movl -52(%rbp), %eax 00c1 8B55D8 movl -40(%rbp), %edx 00c4 29C2 subl %eax, %edx 00c6 89D0 movl %edx, %eax 00c8 3B45D0 cmpl -48(%rbp), %eax 00cb 7FA0 jg .L5 .L2: .LBE3: 00cd 807DCB00 cmpb $0, -53(%rbp) 00d1 7582 jne .L6 00d3 C745D400 movl $0, -44(%rbp) 000000 00da C745D400 movl $0, -44(%rbp) 000000 00e1 EB19 jmp .L7 .L8: 00f8 8345D401 addl $1, -44(%rbp) 00e3 8B45D4 movl -44(%rbp), %eax 00e6 4898 cltq 00e8 8B4485E0 movl -32(%rbp,%rax,4), %eax 00ec 89C6 movl %eax, %esi 00ee BF000000 movl $_ZSt4cout, %edi 00 00f3 E8000000 call _ZNSolsEi 00 .L7: 00fc 8B45D4 movl -44(%rbp), %eax 00ff 3B45D8 cmpl -40(%rbp), %eax 0102 7CDF jl .L8 0104 B8000000 movl $0, %eax 00 .LBE2: 0109 C9 leave .cfi_def_cfa 7, 8 010a C3 ret .cfi_endproc .LFE971: _Z41__static_initialization_and_destruction_0ii: .LFB972: 0128 BF000000 movl $_ZStL8__ioinit, %edi 00 012d E8000000 call _ZNSt8ios_base4InitC1Ev 00 0132 BA000000 movl $__dso_handle, %edx 00 0137 BE000000 movl $_ZStL8__ioinit, %esi 00 013c BF000000 movl $_ZNSt8ios_base4InitD1Ev, %edi 00 0141 E8000000 call __cxa_atexit 00 .cfi_startproc 010b 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 010c 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 010f 4883EC10 subq $16, %rsp 0113 897DFC movl %edi, -4(%rbp) 0116 8975F8 movl %esi, -8(%rbp) 0119 837DFC01 cmpl $1, -4(%rbp) 011d 7527 jne .L10 011f 817DF8FF cmpl $65535, -8(%rbp) FF0000 0126 751E jne .L10 .L10: 0146 C9 leave .cfi_def_cfa 7, 8 0147 C3 ret .cfi_endproc .LFE972: _GLOBAL__sub_I_main: .LFB973: .cfi_startproc 0148 55 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 0149 4889E5 movq %rsp, %rbp .cfi_def_cfa_register 6 014c BEFFFF00 movl $65535, %esi 00 0151 BF010000 movl $1, %edi 00 0156 E8B0FFFF call _Z41__static_initialization_and_destruction_0ii FF 015b 5D popq %rbp .cfi_def_cfa 7, 8 015c C3 ret .cfi_endproc .LFE973: .section .init_array,"aw" .align 8 0000 00000000 .quad _GLOBAL__sub_I_main 00000000 .text .Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.