简体   繁体   中英

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. Its very simple code, the assembly procedure scans a number and return the result to 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


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

BP must be restored when you leave the procedure.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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