首页 > c++ > x86英特尔汇编和C ++ - 堆栈数据损坏

x86英特尔汇编和C ++ - 堆栈数据损坏 (x86 Intel Assembly and C++ - Stack around array corrupted)

2015-11-18 c++cassembly

问题

错误:

Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.

这似乎是这个论坛上的一个常见错误; 但是,我无法找到一个混合了汇编代码的程序。基本上,我的程序是将十进制转换为二进制(16位表示)。完成编码后,一切似乎都正确计算并将十进制转换为二进制而没有问题; 然而,在“按任意键继续......”之后,弹出上面的错误。

我不相信C ++代码导致问题,因为它是非常基本的,并且仅用于调用汇编函数。

再次,计算是正确的,因为程序将产生正确的转换(即:十进制= 10,二进制转换:0000000000001010),但只是在程序结束时给出错误。

C ++代码:

#include <iostream>

using namespace std;

extern"C" void decToBin(char[], int, int);

int main()
{
//Initialize array and variables
const int SIZE = 16;
char arr[SIZE] = { NULL };
int dec = 0;

//Ask user for integer that they want to convert
cout << "Please enter integer you want to convert to binary: ";
cin >> dec;

//Assembly function to convert integer
decToBin(arr, dec, SIZE);

cout << "The 16-bit binary representation of " << dec << " is: ";

//Display the 16-bit binary conversion
for (int i = 0; i < SIZE; i++)
    cout << arr[i];

cout << endl;

system("PAUSE");
return 0;
}

汇编代码:

.686
.model flat

.code

_decToBin PROC              ;Start of project

start:
push ebp
mov ebp,esp             ;Stack pointer to ebp

mov eax,[ebp+8]         ;Address of first array element
mov cx,[ebp+12]         ;Integer number being passed - Copying onto 16 bit register
mov edx,[ebp+16]        ;Size of array

loopme:                     ;Loop to fill in array
mov ebx,0               ;Initializes ebx to store carry flag after shift
cmp edx,0               ;Compare edx with 0 to see if we should continue
je alldone              

shl cx,1                ;Shift the value to the left
adc ebx,0               ;Check carry flag and add 1 if CF(CY) is set to 1 and stay at 0 if CF(CY) is 0
add ebx,48              ;Since array is CHAR, adding 48 will give correct 0 or 1 instead of null

mov [eax],ebx           ;Copy the 0's or 1's into the array location

dec edx                 ;Decrement the counter
inc eax                 ;Move the array up an index

jmp loopme

alldone:    
pop ebp
ret

_decToBin ENDP

END

解决方法

我没有汇编程序来编译你的代码,但你char[]在这一行写了32位值:

mov [eax],ebx           ;Copy the 0's or 1's into the array location

所以,最后写入将更新的存储位置arr[SIZE-1]arr[SIZE+2]

问题

Error:

Run-Time Check Failure #2 - Stack around the variable 'arr' was corrupted.

This seems to be a common error on this forum; however, I was unable to find one that had assembly code mixed into it. Basically, my program is to convert decimal to binary (16-bit representation). After completing the coding, everything seems to compute correctly and convert the decimal to binary without an issue; however, after the "Press any key to continue . . .", the error above pops up.

I do not believe the C++ code is causing the issue as it is very basic, and is there only to invoke the assembly function.

Again, the computation is correct as the program will produce the correct conversion (i.e: Decimal = 10, Binary Conversion: 0000000000001010), but just giving me the error at the end of the program.

C++ Code:

#include <iostream>

using namespace std;

extern"C" void decToBin(char[], int, int);

int main()
{
//Initialize array and variables
const int SIZE = 16;
char arr[SIZE] = { NULL };
int dec = 0;

//Ask user for integer that they want to convert
cout << "Please enter integer you want to convert to binary: ";
cin >> dec;

//Assembly function to convert integer
decToBin(arr, dec, SIZE);

cout << "The 16-bit binary representation of " << dec << " is: ";

//Display the 16-bit binary conversion
for (int i = 0; i < SIZE; i++)
    cout << arr[i];

cout << endl;

system("PAUSE");
return 0;
}

Assembly Code:

.686
.model flat

.code

_decToBin PROC              ;Start of project

start:
push ebp
mov ebp,esp             ;Stack pointer to ebp

mov eax,[ebp+8]         ;Address of first array element
mov cx,[ebp+12]         ;Integer number being passed - Copying onto 16 bit register
mov edx,[ebp+16]        ;Size of array

loopme:                     ;Loop to fill in array
mov ebx,0               ;Initializes ebx to store carry flag after shift
cmp edx,0               ;Compare edx with 0 to see if we should continue
je alldone              

shl cx,1                ;Shift the value to the left
adc ebx,0               ;Check carry flag and add 1 if CF(CY) is set to 1 and stay at 0 if CF(CY) is 0
add ebx,48              ;Since array is CHAR, adding 48 will give correct 0 or 1 instead of null

mov [eax],ebx           ;Copy the 0's or 1's into the array location

dec edx                 ;Decrement the counter
inc eax                 ;Move the array up an index

jmp loopme

alldone:    
pop ebp
ret

_decToBin ENDP

END

解决方法

I have no assembler to compile your code, but you write 32-bit values into a char[] at this line:

mov [eax],ebx           ;Copy the 0's or 1's into the array location

So, the last write will update the memory locations arr[SIZE-1] to arr[SIZE+2].

相似信息