У меня есть упражнение, которое я не могу понять! и я попробую
Это упражнение для представления, и мы никогда не изучали сборку в классе
Это упражнение:
Напишите программу на языке ассемблера, которая объединит два массива с именами NUMBERS_A и NUMBERS_B соответственно, каждый из которых содержит 50 органов байтового размера (BYTE), причем значения органов обоих массивов не отсортированы. Слияние будет происходить в третьем массиве с именем NUMBERS_AB, где будут отсортированы все значения органов, который, конечно, содержит органы размером 100 байт. Программа сделает только два цикла (один вложенный, а другой нет! Nested)!
это то, что я пытаюсь сделать, и это не работает! (я использую в emu8086)
mergeArrays(int*, int*, int, int, int*):
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov DWORD PTR [rbp-40], ecx
mov QWORD PTR [rbp-48], r8
mov DWORD PTR [rbp-4], 0
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-12], 0
.L5:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-36]
jge .L7
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-40]
jge .L7
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-8]
cdqe
lea rcx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rcx
mov eax, DWORD PTR [rax]
cmp edx, eax
jge .L3
mov eax, DWORD PTR [rbp-4]
lea edx, [rax+1]
mov DWORD PTR [rbp-4], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
lea edx, [rax+1]
mov DWORD PTR [rbp-12], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-48]
add rdx, rax
mov eax, DWORD PTR [rcx]
mov DWORD PTR [rdx], eax
jmp .L5
.L3:
mov eax, DWORD PTR [rbp-8]
lea edx, [rax+1]
mov DWORD PTR [rbp-8], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
lea edx, [rax+1]
mov DWORD PTR [rbp-12], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-48]
add rdx, rax
mov eax, DWORD PTR [rcx]
mov DWORD PTR [rdx], eax
jmp .L5
.L7:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-36]
jge .L6
mov eax, DWORD PTR [rbp-4]
lea edx, [rax+1]
mov DWORD PTR [rbp-4], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
lea edx, [rax+1]
mov DWORD PTR [rbp-12], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-48]
add rdx, rax
mov eax, DWORD PTR [rcx]
mov DWORD PTR [rdx], eax
jmp .L7
.L6:
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-40]
jge .L9
mov eax, DWORD PTR [rbp-8]
lea edx, [rax+1]
mov DWORD PTR [rbp-8], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
lea rcx, [rdx+rax]
mov eax, DWORD PTR [rbp-12]
lea edx, [rax+1]
mov DWORD PTR [rbp-12], edx
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-48]
add rdx, rax
mov eax, DWORD PTR [rcx]
mov DWORD PTR [rdx], eax
jmp .L6
.L9:
nop
pop rbp
ret
.LC0:
.string "Array after merging"
.LC1:
.string " "
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 72
mov rax, rsp
mov rbx, rax
mov DWORD PTR [rbp-64], 1
mov DWORD PTR [rbp-60], 3
mov DWORD PTR [rbp-56], 5
mov DWORD PTR [rbp-52], 7
mov DWORD PTR [rbp-24], 4
mov DWORD PTR [rbp-80], 2
mov DWORD PTR [rbp-76], 4
mov DWORD PTR [rbp-72], 6
mov DWORD PTR [rbp-68], 8
mov DWORD PTR [rbp-28], 4
mov edx, DWORD PTR [rbp-24]
mov eax, DWORD PTR [rbp-28]
add eax, edx
cdqe
sub rax, 1
mov QWORD PTR [rbp-40], rax
mov rdx, rax
add rdx, 1
mov r8, rdx
mov r9d, 0
mov rdx, rax
add rdx, 1
mov rsi, rdx
mov edi, 0
add rax, 1
lea rdx, [0+rax*4]
mov eax, 16
sub rax, 1
add rax, rdx
mov ecx, 16
mov edx, 0
div rcx
imul rax, rax, 16
sub rsp, rax
mov rax, rsp
add rax, 3
shr rax, 2
sal rax, 2
mov QWORD PTR [rbp-48], rax
mov rdi, QWORD PTR [rbp-48]
mov ecx, DWORD PTR [rbp-28]
mov edx, DWORD PTR [rbp-24]
lea rsi, [rbp-80]
lea rax, [rbp-64]
mov r8, rdi
mov rdi, rax
call mergeArrays(int*, int*, int, int, int*)
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
mov DWORD PTR [rbp-20], 0
.L12:
mov edx, DWORD PTR [rbp-24]
mov eax, DWORD PTR [rbp-28]
add eax, edx
cmp DWORD PTR [rbp-20], eax
jge .L11
mov rax, QWORD PTR [rbp-48]
mov edx, DWORD PTR [rbp-20]
movsx rdx, edx
mov eax, DWORD PTR [rax+rdx*4]
mov esi, eax
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov esi, OFFSET FLAT:.LC1
mov rdi, rax
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
add DWORD PTR [rbp-20], 1
jmp .L12
.L11:
mov eax, 0
mov rsp, rbx
mov rbx, QWORD PTR [rbp-8]
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L16
cmp DWORD PTR [rbp-8], 65535
jne .L16
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
call __cxa_atexit
.L16:
nop
leave
ret
_GLOBAL__sub_I_mergeArrays(int*, int*, int, int, int*):
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret