我试图在模型大的汇编代码中使用printf,并且我得到一个修复溢出,我需要在此代码中更改以使其工作?
.MODEL LARGE
.STACK 100h
.DATA
int DW "%d"
.CODE
.386
extrn _printf:far
PUBLIC _stack_protection
_stack_protection PROC FAR
push bp
mov bp,sp
push es
mov ax,10
push ax
push word ptr int
call _printf
add sp,4
pop es
pop bp
ret
_stack_protection ENDP
END
答案 0 :(得分:1)
你需要改变这个:
int DW "%d"
到此:
_int DB "%d",0
因为printf()
需要正常的NUL终止的C字符串,因为C字符串由字节组成,而不是单词,因为int
可能是保留字(至少在TASM中)。
您还需要更改此内容:
push word ptr int
...
add sp,4
到此:
push seg _int
push offset _int
...
add sp,6
因为在大内存模型中所有指针都很远,即由一个段和一个偏移量组成。
所以,这就是我最终的结果......
Asm文件:
; file: larg.asm
; how to assemble: tasm.exe /ml larg.asm
.MODEL LARGE
;.STACK 100h ; unnecessary and too small anyway,
; let the C compiler take care of it
DATA segment word public 'DATA' ; make sure class='DATA' (default)
;int DW "%d" ; 'int' is a reserved word,
; C strings must consist of bytes/chars and end with byte 0
_int DB "%d", 0
DATA ends
extrn _printf:far ; for some reason this must be
; outside of the code segment
CODE segment byte public 'CODE' USE16 ; make sure it's
; 16-bit code, class='CODE' (default)
assume cs:CODE
.386
PUBLIC _stack_protection
_stack_protection PROC FAR
push bp
mov bp,sp
push es
mov ax,10
push ax
;push word ptr int ; must be the far address of '_int'
; because in the 'large' memory model all pointers are 'far'
push seg _int
push offset _int
call _printf
;add sp,4 ; must account for the far address size on stack
add sp,6
pop es
pop bp
ret
_stack_protection ENDP
CODE ends
END
C档案:
// file: large.c
// how to compile: tcc.exe -mlarge large.c larg.obj
#include <stdio.h>
extern void stack_protection(void);
int main(void)
{
stack_protection();
printf("\n");
return 0;
}
然后我编译程序(使用Turbo C ++ 1.01和TASM 3.2):
tasm.exe /ml larg.asm
tcc.exe -mlarge large.c larg.obj
运行它:
c:\large.exe
10