将下面代码转换为汇编语言void insert ( int arr[], int n){ int key = arr[n]; int i = n; while (arr[i - 1] > key) { arr[i] = arr[i-1]; i--; if(i == 0) break; } arr[i] = key; } void insertionSort( int arr[], int n){ int i; for(i = 1; i < n; i++) { insert(arr, i); } } main() { int arr[] = {99, 2, 3, 1, 22, 88, 7, 77, 54}; int i; insertionSort(arr, 9); for (int i = 0; i < 9; i++) cout << arr[i] << endl; return 0; }
时间: 2023-03-26 12:03:47 浏览: 97
汇编语言代码转换
我可以回答这个问题。以下是将代码转换为汇编语言的结果:
insert:
push ebp
mov ebp, esp
push ebx
mov ebx, [ebp+12]
mov eax, [ebp+16]
mov edx, [eax+ebx*4]
mov ecx, ebx
cmp ecx,
jle .L2
.L3:
mov eax, [ebp+16]
mov esi, [eax+ecx*4-4]
cmp esi, edx
jle .L2
mov eax, [ebp+16]
mov edi, [eax+ecx*4-4]
mov eax, [ebp+16]
mov [eax+ecx*4], edi
sub ecx, 1
cmp ecx,
jg .L3
.L2:
mov eax, [ebp+16]
mov [eax+ecx*4], edx
pop ebx
pop ebp
ret
insertionSort:
push ebp
mov ebp, esp
push ebx
push esi
mov ebx, [ebp+12]
mov esi, 1
.L5:
cmp esi, ebx
jge .L4
push ebx
push esi
call insert
add esp, 8
add esi, 1
jmp .L5
.L4:
pop esi
pop ebx
pop ebp
ret
main:
push ebp
mov ebp, esp
and esp, -16
sub esp, 16
mov DWORD PTR [esp+12], 99
mov DWORD PTR [esp+8], 2
mov DWORD PTR [esp+4], 3
mov DWORD PTR [esp], 1
mov DWORD PTR [esp-4], 22
mov DWORD PTR [esp-8], 88
mov DWORD PTR [esp-12], 7
mov DWORD PTR [esp-16], 77
mov DWORD PTR [esp-20], 54
lea eax, [esp+4]
push eax
push 9
call insertionSort
add esp, 8
mov DWORD PTR [esp],
call _ZNSolsEi
mov DWORD PTR [esp+4], OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T_ES6_
mov DWORD PTR [esp], eax
call _ZNSolsEPFRSoS_E
mov DWORD PTR [esp],
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
mov DWORD PTR [esp+4], OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T_ES6_
mov DWORD PTR [esp], eax
call _ZNSolsEPFRSoS_E
mov eax,
leave
ret
阅读全文