News:

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

Checking Backwards Capabillities Without Stacks.

Started by StatusQuo, October 14, 2011, 12:59:23 AM

Previous topic - Next topic

jj2007

What about
  push edx
  dec ecx
  jle L2

...? Anyway, my favourite is this one:
FlipFlopP proc ; uses esi edi lpSrc=lpDest
  pop edx ; ret addr
  pop eax ; lpSrc, lpDest
  pushad
  xchg eax, esi ; esi=src
  mov edi, esi
  xor eax, eax ; we need to start with a nullbyte
  .Repeat
push eax
lodsb
  .Until !eax
  .Repeat
pop eax
stosb
  .Until !eax
  popad
  jmp edx
FlipFlopP endp

Weighs in at a whopping 23 bytes, and if you are not scared of using a lot of stack, it reverses about 100Mb/s :bg

StatusQuo

That is brilliant, and your saying also speaks behind a lot of things if thought was in it  :U, but i was hoping to go a little bit more basic with a change of registry. I have been relaying on the old Irvine books,  :eek and I came across a register which I understand a little bit without thorough explaination. The EBP register is from, what I understand is a base pointer, but I hear its rarely refrenced that for some odd reason. So i took the piece in Strlen which should basically measure the lenghth of a String Entry, although there are easier means which I myself am sure of, I like to take things apart  :P. So, I did the following to test EBP and took out EAX:


Strlen  PROC
        PUSH    EBP
        MOV     EBP, ESP
        PUSH    EDI
        MOV     AL, 0
        MOV     EDI, EDX
        MOV     EDX, [EBP+8]
;        MOV     ECX, 0FFFFFFFFh
        REPNZ   SCASB
        NEG     ECX
        SUB     ECX,2
        POP     EDI
        POP     EBP
        RET

Now my question now is, I need to learn how to have a better handling with EBP. I suppose half idea programming is halfed the program.  :(

Quote from: dedndave on October 21, 2011, 02:47:49 AM
hiyas Jochen

i put a branch here originally to L2
;         Jmp L1   jump on itself makes no sense
it performs the "are we done" test for strings that have a length of 0 or 1
StrRev  PROC

;Call With: EDX = address of string
;           ECX = length of string
;
;  Returns: string is reversed

        PUSH    EAX
        PUSH    ECX
        PUSH    EDX
        DEC     ECX
        ADD     ECX, EDX
        JMP     L2

L1:     MOV     AL, [EDX]
        MOV     AH, [ECX]
        MOV     [ECX], AL
        MOV     [EDX], AH
        DEC     ECX
        INC     EDX

L2:     CMP     ECX, EDX
        JA      L1

        POP     EDX
        POP     ECX
        POP     EAX
        RET

StrRev  ENDP

Then God sad, "In the beginning....There was SCOPE!", Then I asked, "What is Scope?", and God said...."A Stack Frame, geeze I forgot to include you with common sense, BUT a prototype you are, too wierd to live, to rare to die...", and it was written....

dedndave

one way to learn about stack frames (EBP, prologues, epilogues) is to look at a few routines written both ways
a while back, i made a couple posts on this subject.....

http://www.masm32.com/board/index.php?topic=15307.msg124847#msg124847

http://www.masm32.com/board/index.php?topic=14381.msg114921#msg114921

StatusQuo

Thank you SOOOOOOOO Much, a better explaination of the fuctions of EBP & ESP and the like. One mistake I just learned from that thread is that in my pervious programs. I kept using ESP as a base pointer, as the thread explains, this is wrong lol. It explains a lot because I know now that when I did use ESP as a register, I had doubled my line work instead of keeping it short and simple, but many thanks now for that answered a lot of my questions.


Quote from: dedndave on October 27, 2011, 10:17:26 PM
one way to learn about stack frames (EBP, prologues, epilogues) is to look at a few routines written both ways
a while back, i made a couple posts on this subject.....

http://www.masm32.com/board/index.php?topic=15307.msg124847#msg124847

http://www.masm32.com/board/index.php?topic=14381.msg114921#msg114921
Then God sad, "In the beginning....There was SCOPE!", Then I asked, "What is Scope?", and God said...."A Stack Frame, geeze I forgot to include you with common sense, BUT a prototype you are, too wierd to live, to rare to die...", and it was written....

dedndave

well - you can use ESP to reference items on the stack, of course
and - sometimes we do - in order to avoid the stack frame overhead

however, it is tricky because every time you push or pop something, the offset of your variable relative to ESP changes
that is the main advantage of using EBP
the ESP pointer can go up and down with pops and pushes, but EBP stays the same   :U

there are some other more subtle differences
the intructions for using EBP to access stack variables have been optimized
for example, they are typically one byte smaller than the ESP equiv
on many CPU's, EBP can be a bit faster, as well

StatusQuo

Excellent and I feel I am a bit closer to the understanding of EBP and ESP. Yet another question had arisen, and someone, mainly a friend asked me to do this same program I have done using a MACRO. Of course he wants me to do his homework and the twenty dollar fee is good enough. Problem is, the minute I sat down, wrote my outline, and am face to face with the screen and I notice I am uncertain with how to approach it. If there is anything i have been poorly taught WITHOUT THROUGH explaination, is MACROs. My understanding of Macro's is that they are supposed to be SMALL, and small titled, and a substitution of the vaulue will always occur, I.E

mPutChar MACRO char

   PUSH EAX (Somewhere in your MACRO you will use EAX.)
   MOV AL, char
   CALL WriteChar      
               
POP EAX
ENDM
               
mPutChar 'A' - The Code that will be substituted is:
PUSH EAX
MOV AL, 'A'
CALL WriteChar
POP EAX

Yet with this understanding that I have, how would approach it with the program at hand? I have an idea of it but I have to write the actual program first before I proceed. Perhaps any more expanded ideas for the community would be the most appreciative.

Quote from: dedndave on October 27, 2011, 11:28:42 PM
well - you can use ESP to reference items on the stack, of course
and - sometimes we do - in order to avoid the stack frame overhead

however, it is tricky because every time you push or pop something, the offset of your variable relative to ESP changes
that is the main advantage of using EBP
the ESP pointer can go up and down with pops and pushes, but EBP stays the same   :U

there are some other more subtle differences
the intructions for using EBP to access stack variables have been optimized
for example, they are typically one byte smaller than the ESP equiv
on many CPU's, EBP can be a bit faster, as well
Then God sad, "In the beginning....There was SCOPE!", Then I asked, "What is Scope?", and God said...."A Stack Frame, geeze I forgot to include you with common sense, BUT a prototype you are, too wierd to live, to rare to die...", and it was written....

dedndave

that depends....
what percentage of that $20 were you planning on giving to me ?   :wink
i accept pay-pal   :bg

first, you have to have a need for a macro
this particular program may not have such a need

however, we can use a macro anyplace
here is what i suggest....

have a look at Kip's macro file (i think he named it macros.asm)
pick what is likely to be the simplest macro in there - "exit"
you will find that it is pretty easy to make a macro
then, pick one that has an argument and see how that is done
from what i remember about Kip's macros, they are all fairly simple

you can also read the documentation concerning macros in the MASM reference manual....
http://www.4shared.com/file/39MdNf_v/MASMProgGuide.html

StatusQuo

Dude you just opened my eyes, you deserve more than a $20 bucks friend. If it was in my power, I would give you a case of Wild Turkey and a hired Striper, that you never know was, and take you on that journey that Dr. Hunter S. Thompson took when he entered Fear and Loathing. Lol, Seriously dude you really gave me the insight i am looking for. Thanks a Million for my questions at an end.  :U I always loved this forum, strait talk, more learning, broader horizons, and hell of a good people.  :bg

Quote from: dedndave on October 28, 2011, 01:24:07 AM
that depends....
what percentage of that $20 were you planning on giving to me ?   :wink
i accept pay-pal   :bg

first, you have to have a need for a macro
this particular program may not have such a need

however, we can use a macro anyplace
here is what i suggest....

have a look at Kip's macro file (i think he named it macros.asm)
pick what is likely to be the simplest macro in there - "exit"
you will find that it is pretty easy to make a macro
then, pick one that has an argument and see how that is done
from what i remember about Kip's macros, they are all fairly simple

you can also read the documentation concerning macros in the MASM reference manual....
http://www.4shared.com/file/39MdNf_v/MASMProgGuide.html
Then God sad, "In the beginning....There was SCOPE!", Then I asked, "What is Scope?", and God said...."A Stack Frame, geeze I forgot to include you with common sense, BUT a prototype you are, too wierd to live, to rare to die...", and it was written....

dedndave

unfortunately, wifee would kill me if i got within a mile of a stripper   :lol