Unhandled exception :Integer overflow

博客主要讲述了VC++混合编程中,idiv有符号除法导致Integer overflow的错误问题。解决办法是在idiv前确保edx为Zero,可通过异或edx或cdq将累加和的eax符号扩展到edx,使执行后edx为0。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误问题:V c++混合编程idiv有符号除法导致Integer overflow

解决:idiv前确保edx为Zero  

       异或edx 或 cdq 将累加和的eax符号扩展到edx,执行后edx为0

#include <iostream>
#define NUM 3
long average(long arr[],int num);
int main()
{
	long array[NUM] = { 111 ,3,-111 };
	long result = average(array, NUM);
    std::cout <<"result:" <<result<<"\n"; 
}
long average(long arr[], int num) {
	long temp;
	__asm {
		mov ebx, arr; 存储arr基址
		mov ecx, num; ecx = num
		xor eax, eax; 异或清零
		xor edx, edx;  eax为子程序输出参数,edx为入口参数
		avgasm : add eax, [ebx + edx * 4]
				 add edx, 1
				 cmp edx, ecx
				 jb avgasm
				 ; xor edx, edx
				 ; cdq
				 idiv ecx
				mov temp,eax
	}
	return temp;
}

 

Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) [0x0001a] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Number.ParseInt32 (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) [0x00016] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00017] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Convert.ToInt32 (System.String value) [0x0000b] in <de882a77e7c14f8ba5d298093dde82b2>:0 at ConsoleApplication1.Program.Main (System.String[] args) [0x00013] in <1709548911e24f23b17e5a7b2ccfa734>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in a correct format. at System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) [0x0001a] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Number.ParseInt32 (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) [0x00016] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00017] in <de882a77e7c14f8ba5d298093dde82b2>:0 at System.Convert.ToInt32 (System.String value) [0x0000b] in <de882a77e7c14f8ba5d298093dde82b2>:0 at ConsoleApplication1.Program.Main (System.String[] args) [0x00013] in <1709548911e24f23b17e5a7b2ccfa734>:0
最新发布
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值