gpt4 book ai didi

c++ - 从汇编程序写入返回值时发生意外页面错误

转载 作者:行者123 更新时间:2023-12-01 22:55:14 25 4
gpt4 key购买 nike

我正在试验混合汇编 (x86) 和 C++。我用汇编编写了一个过程,然后从 C++ 中调用它。

但是,当将返回值写入局部变量时,出现写权限冲突错误。

#include <iostream>


// will return 1 if all ok and 0 if b is 0
extern "C" int integerMulDiv(int a, int b, int* prod, int* quo, int* rem);

int main() {
int a = 13, b = 4;
int p, q, r;

int res = integerMulDiv(a, b, &p, &q, &r);
std::cout << p << '\t' << q << '\t' << r << std::endl;
std::cout << res << std::endl << std::endl;

res = integerMulDiv(31, 0, &p, &q, &r);
std::cout << p << '\t' << q << '\t' << r << std::endl;
std::cout << res << std::endl << std::endl;

return 0;
}

汇编过程通过指针返回一些值,通过 RAX 返回一个 int。

; Returns : 0 Error (division by 0)
; : 1 All ok

; *prod = a * b
; *quo = a / b
; *rem = a % b
integerMulDiv proc

push ebp
mov ebp, esp
push ebx ; save ebp and ebx

xor eax, eax

mov ecx, [ebp + 8] ; get a
mov edx, [ebp + 12] ; get b (the divisor)

or edx, edx ; check divisor
jz invalidDivizor

imul edx, ecx
mov ebx, [ebp + 16] ; get address of prod
mov [ebx], edx ; write prod

mov eax, ecx
cdq ; extend to edx
idiv dword ptr[ebx + 12]

mov ebx, [ebp + 20] ; get address of quo
mov [ebp], eax ; write quo
mov ebx, [ebp + 24] ; get address of rem
mov [ebp], edx ; write rem

mov eax, 1 ; set success
jmp returnFromProc

invalidDivizor:
mov eax, 0 ; set failed

returnFromProc:
pop ebx
pop ebp
ret ; restore and return

integerMulDiv endp

我在第一次调用 integerMulDiv 后遇到错误,当时它试图将结果写入 res 变量。

反汇编看起来像这样:

    int res = integerMulDiv(a, b, &p, &q, &r);
002D24BD lea eax,[r]
002D24C0 push eax
002D24C1 lea ecx,[q]
002D24C4 push ecx
002D24C5 lea edx,[p]
002D24C8 push edx
002D24C9 mov eax,dword ptr [b]
002D24CC push eax
002D24CD mov ecx,dword ptr [a]
002D24D0 push ecx
002D24D1 call _integerMulDiv (02D133Eh)
002D24D6 add esp,14h
002D24D9 mov dword ptr [res],eax <- The #PF happens here

有谁知道发生了什么以及为什么?

最佳答案

以下代码部分对我来说很突出。

idiv dword ptr[ebx + 12]

mov ebx, [ebp + 20] ; get address of quo
mov [ebp], eax ; write quo
mov ebx, [ebp + 24] ; get address of rem
mov [ebp], edx ; write rem

我不确定您是否要除以乘积地址后 12 字节的内存内容。也许你的意思是 [ebp + 12]

之后,您将地址加载到 ebx,然后将值写入 ebp

关于c++ - 从汇编程序写入返回值时发生意外页面错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73407650/

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