简体   繁体   English

异常程序终止turbo c

[英]abnormal program termination turbo c

I am trying to run an combined code of C that calling assembly procedure, and I get abnormal program termination message. 我正在尝试运行一个调用汇编过程的C组合代码,并且收到异常程序终止消息。 Its very simple code, the assembly procedure scans a number and return the result to c. 它的代码很简单,汇编程序扫描一个数字并将结果返回给c。

;main code
#include<stdio.h>
extern long getPnum();
int main()
{
    long x;
    x = getPnum();
    printf("%d", x);
    return 0;
}


;getPNum
.MODEL SMALL
.STACK 100H
.DATA
NUM DD 0

.CODE
.386
PUBLIC _getPnum
_getPnum PROC NEAR

PUSH BP
MOV BP,SP
PUSH EAX
PUSH EBX
PUSH ECX
MOV EBX,10

SCAN:
        MOV EAX,NUM
        MUL EBX
        MOV ECX,EAX
        XOR EAX,EAX
        MOV AH,1
        INT 21H
        CMP AL,13
        JE NEXT
        SUB AL,'0'
        MOV AH,0
        ADD ECX,EAX
        MOV NUM,ECX
        JMP SCAN
NEXT:
MOV AX,WORD PTR NUM
MOV DX,WORD PTR NUM+2
ADD SP,14
RET
_getPnum ENDP
END

I changed the %d to ld% , and now I get another error: Dimdie error It's very strange when I run the DEBUGER I return the number through AX DX,and X gets the wrong value debugger result scrren 我将%d更改为ld% ,现在又遇到另一个错误: Dimdie error当运行DEBUGER时,我通过AX DX返回该数字,而X却得到了错误的值调试器 结果 ,这很奇怪。


I Changed 我变了

ADD SP,14
RET

to

ADD SP,12
POP BP
RET

and now I don't get any errors, but the printed value is incorrect, despite that the returned value trough DX:AX is correct 现在我没有任何错误,但是尽管返回值通过DX:AX是正确的,但打印值还是不正确

BP must be restored when you leave the procedure. 退出程序时, 必须恢复BP

Change 更改

ADD SP,14
RET

to

ADD SP,12
POP BP
RET

Better is: 更好的是:

MOV SP, BP
POP BP
RET

BTW: Why do you push a bunch of registers which you don't restore at the end of the function? 顺便说一句:为什么您要推送一堆在功能结束时未还原的寄存器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM