News:

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

Access violation

Started by brixton, June 12, 2005, 10:24:50 AM

Previous topic - Next topic

brixton

I am having more newbie problems.

I wanted to take a string, strip it of it's first character, and append it onto the end.  This, repeated, would give a kind of scrolling effect.  I can't get this to work, there is probably an obvious mistake but when I debug it, I reach..

invoke szRight, ADDR new_message, ADDR buffernew, 27h

..and I get access violation.  Can someone help?

    .data

orig_message db "Here is the message.. that I shall use! ", 0  ;message with ending space
file db "file.txt", 0
newline db 13, 10, 0

handle dd 0
count dd 0

    .data?

new_message db 40 DUP (?)
buffernew db 40 DUP (?)
bufferorig db 1 DUP (?)

    .code

start:

invoke szCopy, ADDR orig_message, ADDR new_message
invoke _lcreat, ADDR file, 0
mov handle, eax

@@:

cmp count, 5
je quit
invoke szLeft, ADDR new_message, ADDR bufferorig, 1h
invoke szRight, ADDR new_message, ADDR buffernew, 27h
invoke szCatStr, ADDR buffernew, ADDR bufferorig
invoke szCopy, ADDR buffernew, ADDR new_message
invoke _lwrite, handle, ADDR new_message, 28h
invoke _lwrite, handle, ADDR newline, 2h
inc count
jmp @B

quit:
invoke _lclose, handle
invoke ExitProcess, NULL
end start
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

AeroASM

Your buffers need to be 41 bytes long, to include the zero terminator.

brixton

Haha, after I changed those 2 values and assembled it, it ran perfectly  :dance: :dance: :dance: :dance:

Thankyou!  :cheekygreen: :cheekygreen:
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

donkey

Just as a small pointer, jumps are expensive in terms of the instruction cache and execution speed. The logic of your loop should be changed to avoid them...

mov count,5
@@:

invoke szLeft, ADDR new_message, ADDR bufferorig, 1h
invoke szRight, ADDR new_message, ADDR buffernew, 27h
invoke szCatStr, ADDR buffernew, ADDR bufferorig
invoke szCopy, ADDR buffernew, ADDR new_message
invoke _lwrite, handle, ADDR new_message, 28h
invoke _lwrite, handle, ADDR newline, 2h
dec count
jnz @B


Counting backwards allows you to do a simple test for zero and since backward jumps are "normally taken" by default you don't do as much damage to the cache. Not that it makes much difference in this case but it is never a bad thing to keep up good coding practices.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

brixton

Thankyou very much, these kind of pointers are valuable!  :U
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

dsouza123

Donkey

Thank you, that was the clearest explaination and example of the issue/optimization of conditional jumps I've read.
It should be included in an optimization/good coding practices tutorial.