gpt4 book ai didi

c++ - 调试器在编译内联汇编代码时打开

转载 作者:行者123 更新时间:2023-11-28 07:48:08 25 4
gpt4 key购买 nike

我正在使用来自 Github 的一些代码,但无法编译。调试器在代码编译过程中打开。这是一个个人项目,如果有人可以对代码进行有用的编辑,那将非常有帮助,因为我是汇编新手。

这是在调试器打开之前我得到的输出:

'Assembly.exe': Loaded 'C:\Users\Mayank\Desktop\Assembly\Debug\Assembly.exe', Symbols loaded. 'Assembly.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'Assembly.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'Assembly.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'Assembly.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. First-chance exception at 0x011013fe in Assembly.exe: 0xC00000FD: Stack overflow. Unhandled exception at 0x011013fe in Assembly.exe: 0xC00000FD: Stack overflow.

代码如下:

//
// main.cpp
// MergeSortC
//
// Copyright (c) 2012 Mayank. All rights reserved.
//

#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;
const int ARRAYSIZE = 30;

int main()
{
int arr[ARRAYSIZE];
int temp_arr[ARRAYSIZE];
int number;

for(int x = 0; x < ARRAYSIZE; x++)
{
number = (rand() % 99) + 1;
arr[x] = number;
}

Merge_Sort:
__asm
{
// EAX - Array start
// ECX - array length

// Merge_Sort (first half)
// Length of the first half
// ECX /= 2
push ECX
shr ECX, 2
call Merge_Sort
pop ECX

// Merge_Sort (second half)
push arr
push EBX
push ECX

// Length of the second half
// ECX = ECX - ECX/2
mov EDX, ECX
shr EDX, 1
sub ARRAYSIZE, EDX
imul EDX, 4
// Start index of the second half
// EAX = EAX + (ECX/2) * 4
add arr, EDX
push EDX
call Merge_Sort
pop EDX

pop ECX
pop EBX
pop arr

pushad
// Merge (first half, second half)
// Length of first half = ECX/2
// Length of second half = ECX - ECX/2
mov EDX, ECX
shr ECX, 1
sub EDX, ECX

// Start of second half = EAX + (ECX/2) * 4
mov EBX, EAX
mov EDI, ECX
imul EDI, 4
add EBX, EDI
// Index of temp array = 0
sub EDI, EDI
call Merge
popad

// Copy back the merged array from temp_arr to arr
call Merge_Copy_Back_Temp

ret
};
Merge:
__asm
{
// Merge two arrays contents.
// The final merged array will be in temp_arr
// Merging is done recursively.

// Arguments:
// EAX - First array's start
// EBX - Second array's start
// ECX - Length of first array
// EDX - Length of second array
// EDI - Index in temp array
pushad

// Handle the cases where one array is empty
cmp ARRAYSIZE, 0
jz First_Array_Over
cmp EDX, 0
jz Second_Array_Over

// Compare first elements of both the arrays
push ARRAYSIZE
push EDI
mov ECX, [arr]
mov EDI, [ECX]
cmp ECX, EDI
pop EDI
pop ECX

// Pick which ever is the least and update that array
jl Update_First_Array
jmp Update_Second_Array
ret
};

Update_First_Array:
__asm
{
// min_elem = min (first elements of first array and second array)
// Put min_elem into the temp array
push dword ptr [EAX]
pop dword ptr [temp_arr + EDI * 4]
add EAX, 4
dec ECX
inc EDI

// Recursively call Merge on the updated array and the
// other array
call Merge
popad
ret
};

Update_Second_Array:
__asm
{
// min_elem = min (first elements of first array and second array)
// Put min_elem into the temp array
push dword ptr [EBX]
pop dword ptr [temp_arr + EDI * 4]
add EBX, 4
dec EDX
inc EDI

// Recursively call Merge on the updated array and the
// other array
call Merge
popad
ret
};

Merge_Copy_Back_Temp:
__asm
{
// Copy back the temp array into original array
// Arguments:
// EAX - original array address
// ECX - original array length
pushad

// For copying back, the destination array is EAX
mov EBX, EAX
// Now, the source array is temp_arr
mov EAX, temp_arr
call Copy_Array
popad
ret
};


First_Array_Over:
__asm
{
// Copy the rest of the second array to the temp arr
// because the first array is empty
pushad
mov EAX, EBX
mov ECX, EDX
mov EBX, temp_arr
imul EDI, 4
add EBX, EDI
call Copy_Array
popad
popad
ret
};

Second_Array_Over:
__asm
{
// Copy the rest of the first array to the temp arr
// because the second array is empty
pushad
mov EBX, temp_arr
imul EDI, 4
add EBX, EDI
call Copy_Array
popad
popad
ret
};
Copy_Array:
__asm
{
// Copy array to destination array
// EAX - Array start
// EBX - Destination array
// ECX - Array length

// Trivial case
cmp ECX, 0
jz Copy_Empty_Array

push ECX
sub EDI, EDI
};
copy_loop:
__asm
{
// Copy each element
push dword ptr [EAX + EDI * 4]
pop dword ptr [EBX + EDI * 4]
inc EDI
loop copy_loop

pop ECX
ret
};

Copy_Empty_Array:
__asm
{
ret
};

Read_Arr:
__asm
{
// EAX - array start
// ECX - array length
mov ESI, arr
sub EDI, EDI
};
loop1:
__asm
{
// Read each element
lea eax,[esi+edx*4]
inc EDI
loop loop1
ret
};

return 0;
}

最佳答案

我不敢相信调试器在编译 期间打开。也就是说,这段代码

Merge_Sort:
__asm
{
// EAX - Array start
// ECX - array length

// Merge_Sort (first half)
// Length of the first half
// ECX /= 2
push ECX
shr ECX, 2
call Merge_Sort

必须在运行时溢出堆栈:压入 ECX、移动 ECX、调用自身。

关于c++ - 调试器在编译内联汇编代码时打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com