News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help with compiler generated code.

Started by KcDan, November 06, 2005, 03:47:36 PM

Previous topic - Next topic

KcDan

Im currently working on a compiler and Ive just gotten throught with a few control statements, subs/functions and all that good stuff and Im starting to work on basic I/O (print, input, locate, color, cls) and I thought print was finished but there seems to be a bug in it.

Here is the source for a little demo that shows the bug.

main{
   int count;

label loop;
   
   if(count>100){goto endit;};

   print(count);
   count=count+1;

   goto loop;

label endit;
   end;
}


Here is the assembler code

.486
.model flat, stdcall
option casemap :none
include windows.inc
include user32.inc
include kernel32.inc
include gdi32.inc
include C:\masm32\macros\macros.asm
include masm32.inc
includelib gdi32.lib
includelib user32.lib
includelib kernel32.lib
includelib masm32.lib
.data?
ConsoleOut dd ?
ConsoleIn dd ?
printstr db ?
.code
start:
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov ConsoleOut,eax
invoke GetStdHandle,STD_INPUT_HANDLE
mov ConsoleIn,eax

;int

;label
lblloop:

;if
MOV eax, varcount
push eax
MOV eax,100
mov ebx,eax
pop eax
cmp eax,ebx
jb KCLBL1

;goto
jmp lblendit
KCLBL1:

;print
MOV eax, varcount
invoke wsprintf,addr printstr,CTXT('%u'),eax
invoke WriteFile,ConsoleOut,addr printstr,len(addr printstr),NULL,NULL

;count
MOV eax, varcount
PUSH eax
MOV eax,1
POP ebx
ADD eax,ebx
MOV varcount,eax

;goto
jmp lblloop

;label
lblendit:

;end
invoke ExitProcess,0
.data?
varcount dd ?
end start

As you can see if you test it, it doesnt work. It stay looping on 1. I believe the problem is with Wsprintf but I dont know how to fix it.

Help would be greatly appreciated, thanks.

tenkey

Questions about code generation should be in the assembler/compiler technology forum.

But the problem is basic data allocation - you're only allocating ONE byte for the printstr buffer. wsprintf will overwrite any data that follows printstr. Whatever follows printstr (I see that an unaligned varcount follows printstr) will get overwritten.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

GregL

KcDan,

  You only allocated a single byte for printstr, you need more than that.  :bg

  Oops, tenkey posted while I was writing this.




KcDan

Bah! Thanks for the help, it now works correctly.

printstr db 1024 dup (0)