News:

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

Finding a character in a string - strchr.

Started by KeepingRealBusy, June 24, 2010, 04:25:24 AM

Previous topic - Next topic

jj2007

Quote from: Queue on June 27, 2010, 01:28:58 AMWhile I really have no idea what I'm doing, I can assemble and link this code, and it's interesting to me that it fails so arbitrarily on my old computer.

To illustrate what these "unorthodox" jumps do, here a snippet:
include \masm32\include\masm32rt.inc

.code
start: mov ebx, -1
jmp @F+3 ; attention "artwork"
@@: nop
L0: inc ebx
L1: inc ebx
L2: inc ebx
L3: inc ebx
L4: inc ebx
L5: inc ebx
MsgBox 0, str$(ebx), "I hit label No. :", MB_OK
exit
end start

That code works also without the L1, L2 etc labels. Now what happens in real life is that
- the clever author of the code simply writes his code with "normal" labels, e.g. jxx MyGoodLabel
- then launches Olly or whatever to see what is the offset against a reference label (@@, i.e. the nop in the example above)
- replaces jmp MyGoodLabel with jmp @F+123
- deletes MyGoodLabel in the hope that who reads his code is too stupid to get the trick.
The real fun comes up when people use different assemblers, because in complex code the offsets *may* differ on rare occasions. If that happens, the jmp @F+123 ends in no man's land, and you have "arbitrarily" behaving source code that is meant to drive you insane.

Queue

No, the jumps don't fail for me. That is something I actually do understand about assembler and jumps in general. Actually, as an exercise, I put in the labels Lingo had chopped out so I could try and follow what was going on. Also took a stab at converting the SSE to MMX with... some degree of success. What I meant by it's failing is the SSE-related stuff isn't working properly on my old Athlon; I don't know enough about SSE and my processor to know why, it's just how it is.

Queue

jj2007

So which SSE level does the first line give you?

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)


dedndave

that could be the problem   :P
most of the SSE code in here requires at least SSE2 support

clive

Quote from: jj2007
To illustrate what these "unorthodox" jumps do, here a snippet:
include \masm32\include\masm32rt.inc

.code
start: mov ebx, -1
jmp @F+3 ; attention "artwork"
@@: nop
L0: inc ebx
L1: inc ebx
L2: inc ebx
L3: inc ebx
L4: inc ebx
L5: inc ebx
MsgBox 0, str$(ebx), "I hit label No. :", MB_OK
exit
end start



A quick static analysis suggests this doesn't work the way you wanted it too. L1 will report 4, L0 5.
It could be a random act of randomness. Those happen a lot as well.

KeepingRealBusy

Quote from: jj2007 on June 27, 2010, 08:06:16 AM
The real fun comes up when people use different assemblers, because in complex code the offsets *may* differ on rare occasions. If that happens, the jmp @F+123 ends in no man's land, and you have "arbitrarily" behaving source code that is meant to drive you insane.

Is there any way to tell from examining the .exe which assembler created the .obj, and which loader was used?

Queue

Quote from: dedndave on June 27, 2010, 09:54:42 AM
that could be the problem   :P
most of the SSE code in here requires at least SSE2 support
Well, that answers that. Why does an SSE1 processor not just outright crash the program when SSE2 opcodes are used?

Queue

lingo

"Is there any way to tell from examining the .exe which assembler created the .obj"

Yes, if you have a MS "Rich" signature after DOS Header of your exe file, ONLY... :wink 

jj2007

Quote from: clive on June 27, 2010, 01:31:33 PM
A quick static analysis suggests this doesn't work the way you wanted it too. L1 will report 4, L0 5.

Thanks, Clive. Here is the correct version.

include \masm32\include\masm32rt.inc

.code
start: mov ebx, 7
jmp @F+5 ; attention "artwork"
@@: nop
L0: dec ebx
L1: dec ebx
L2: dec ebx
L3: dec ebx
L4: dec ebx
L5: dec ebx
MsgBox 0, str$(ebx), "I hit label No. :", MB_OK
exit
end start

Queue

Quote from: lingo on June 27, 2010, 04:48:32 PM
"Is there any way to tell from examining the .exe which assembler created the .obj"

Yes, if you have a MS "Rich" signature after DOS Header of your exe file, ONLY... :wink 
The Rich header is added by the Microsoft linker. I know of some differences in structure between linkers, but not between assemblers. For example, if the code and import sections are merged, that results in a different layout between MS's LINK and POLINK. The resource secton's organization can vary based on how the RES file was converted to object code and by which linker. Different linkers also tend to use slightly different section flags, especially when merging sections.

Queue

jj2007

Quote from: Queue on June 27, 2010, 05:39:14 PM
I know of some differences in structure between linkers, but not between assemblers.

In general, there are no differences in the code generated by various versions of MASM, from ml v6.15 onwards.
Jwasm generates something else for .if eax, but that is not particularly relevant. However, there is one little bug that can make a difference for the jmp @F+123 "artwork": Jwasm generates long jumps in situations where a short jump would be sufficient. That can be cumbersome indeed :bg

lingo

"The Rich header is added by the Microsoft linker"

Congratulation, you just reinvent the wheel....
Will be better to tell as why and how to use it?
For your info Rich is just a part of PRODITEM structure for "end of tallies" rather than a "header"...  :wink

Queue

Quote from: lingo on June 27, 2010, 06:01:23 PM
"The Rich header is added by the Microsoft linker"

Congratulation, you just reinvent the wheel....
Will be better to tell as why and how to use it?
For your info Rich is just a part of PRODITEM structure for "end of tallies" rather than a "header"...  :wink
Wait, what? You said a way to tell the difference between assemblers is the presence of the Rich header. I simply pointed out it's the linker that adds the Rich header, not the assembler.

The Rich header is used to identify which versions of various MS utilities were used to make the executable.
http://ntcore.com/files/richsign.htm

Queue

KeepingRealBusy

I wonder how many hits that site will get today?

Interesting read.

Dave.