News:

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

[.while]

Started by ic2, October 14, 2006, 08:52:21 AM

Previous topic - Next topic

ic2

I have a lot of while blocks in my program and i am trying to replace them with common looping coding like this:

xor ecx,ecx
mov esi, offset w_Windows_Version
mov edi, offset s_XP
LoopXP:
mov al, [esi]
mov ah, [edi]
inc esi
inc edi
cmp al, ah
jne _not_XP
cmp al, 0
jne LoopXP

jmp _GOT_XP


Can someone translate this so it don't have to use [.while].  Then i have a good idea of how to do the rest of them.  This seems like the most compucated one in my program.  It's from the kernel zip.

Thanks in advance



GetKernelBase PROC USES EDI ESI, dwTopStack : DWORD

MOV  EDI, dwTopStack        ; start the search
AND  EDI, 0FFFF0000h        ; wipe the LOWORD !

; ....
.WHILE 1   ;TRUE
   .IF WORD PTR [EDI] == IMAGE_DOS_SIGNATURE
      MOV  ESI, EDI
      ADD  ESI, [ESI+03Ch]
      .IF  DWORD PTR [ESI] == IMAGE_NT_SIGNATURE
.BREAK
      .ENDIF
   .ENDIF
   SUB EDI, 010000h
   .IF EDI < MIN_KERNEL_SEARCH_BASE    ;;;;;;;;;;;;;;;;;;;;;;
      MOV  EDI, 0BFF70000h        ;;;;;;;;;;;;;;;;;;;;;;
   .ENDIF        ;;;;;;;;;;;;;;;;;;;;;;
.ENDW
; ....

XCHG EAX, EDI
RET
GetKernelBase ENDP


donkey

Well, I won't translate the whole thing but a WHILE loop is fairly simple. Unlike a REPEAT loop it will exit without running the code between .WHILE and .ENDW if the condition is not met upon entering the loop so it will look like this...

using MASM HLL syntax

...
.WHILE eax = 1
; code goes here
.ENDW
...


Hand coded

...
whilestart:
cmp eax,1
jne exitloop

;code goes here

jmp whilestart
exitloop:
...
"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

Shantanu Gadgil

To ic2,
Not to go off-topic, but WHY???  :dazzled: :eek

coding in "HLL-looking" constructs doesn't make anyone "less of an ASM programmer"!  :P :P

Is it that you to make the code compatible across different assemblers or something?

Again...I would like to ask WHY???  ::)

And also why are you asking others to translate stuff for you? You know...that might NOT be considered too polite, so...???  ::)

Regards,
Shantanu
To ret is human, to jmp divine!

donkey

Quote from: Shantanu Gadgil on October 14, 2006, 09:01:33 AM
To ic2,
Not to go off-topic, but WHY???  :dazzled: :eek

coding in "HLL-looking" constructs doesn't make anyone "less of an ASM programmer"!  :P :P

Is it that you to make the code compatible across different assemblers or something?

Again...I would like to ask WHY???  ::)

And also why are you asking others to translate stuff for you? You know...that might NOT be considered too polite, so...???  ::)

Regards,
Shantanu

Hi,

There are many reasons to hand code loops, MASM does a horrible job of coding both loops and .IF/.ENDIF blocks (especially the latter) in many case it will insert unnecessary jumps at the end of .IF blocks that can be eliminated by hand coding. Jumps cost cycles and can (even worse) blow the instruction cache and the whole reason that most people turn to assembly is for optimizing code, it is self-defeating to revert to HLL constructs that are inefficient in order to do something that is rather simple anyway. If you are not concerned about the speed of the routine, for example it simply waits for an API to return then by all means use the constructs but the code he posted does not contain an API call in the loop and he may want a more effiicient loop.

Donkey
"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

Shantanu Gadgil

Hi donkey,
I understand and appreciate the speed gains of asm over HLLs. Was just a teeny-weeny :bg :green2 bit peeved  about the "could someone translate for me", thats all!!  :bg

Also, appreciate your point about hand-coded loops over ".while".

(Don't want to start yet another "why ASM"  :bg topic here...so I guess I would say... "I stand corrected")  :bg

Cheers,
Shantanu
To ret is human, to jmp divine!

TNick

Hope not to be off-topic; just want to say that

.WHILE eax<=45
   inc eax
.ENDW


will be translated in something like

jmp    WhileCondition
StartWhile:
   inc   eax
WhileCondition:
cmp     eax, 45
jbe     StartWhile


Regards,
Nick

sinsi

Quote from: TNick on October 14, 2006, 09:34:18 AM
.WHILE eax<=45
   inc eax
.ENDW
heh heh what about
mov ax,45
Sorry, couldn't resist... :bg ...although I've seen similar in VB code  :bdg
Light travels faster than sound, that's why some people seem bright until you hear them.

TNick

 :lol :lol :lol :lol :lol
What, you don't think that this is a good way to put 45 in eax? :toothy

Regards,
Nick

sinsi

Light travels faster than sound, that's why some people seem bright until you hear them.

ic2

Shantanu Gadgil, I thought that if you show people that you have made an effort and that you know something about coding but having too much trouble to do something, some of us have no choice but to ask for help.  I have about 7 or 8 issues hanging over my head since July.  It may take me many more month of searching, reading, etc to move on.  So i don't ask for much help.  This is one that i have to ask about simple as it may be for you but not me.  I been at it for over 2 weeks with no luck.  Working 12 hour with 3 hours commute time per day, 5 days a week, it hard to find time to do anything but i try.  Beside who else can i turn to for help.  M$.  Evidently not YOU...hee hee ... Just joking maybe.  I don't think it bother people at this forum.  They always seems to try to help others no matter what they may think of their questions.  If they did not help others i would not know sh*t about ASM unless i go to school for the next 10 years.  That's impossible for me.

As far as your other question i am trying to code in a way that other assembler can handle it without to much changes.  POASM can do it but how about others.  And i like knowing that what i give the assembler it do exactly what i expect with no favors added.

donkey,  Im still working with it but i can't seem to get it right right now but Thanks a Lot for the lead and explanation of how while work... I never had a clue.  You explain things so well, it don't always be like that in the books.  WoW.


PS:  Shantanu Gadgil,  I should have said "could someone tranlate this code so that others who don't know like myself will learn something more about [.while]".

BTW,  Why should that bother you.  This is not your forum.

Vortex

A quick macro :

.386
.model flat, stdcall
option casemap :none

_DO MACRO

@@:

ENDM

_WHILE MACRO arg1,cond,arg2

    cmp arg1,arg2
    j&cond @b

ENDM

.code

start:

xor     eax,eax

_DO
        inc eax
_WHILE  eax,b,45
   
ret

END start

hutch--

ic2,

Shantanu was just passing tp you what he thought was good advice, there is no malice in it but what you are after learning is also a good idea in that it will help you to better understand branching code in assembler. Now a number of members have shown you how a .WHILE conditional works but you can in fact do it yourself with the tools in MASM32.

Write a simple .WHILE block of code, put a number of NOPs in front of it then disassemble it using DumpPE, search for the NOPs and you will get to see exactly what happens to your code. Now this is a better method than you may thnk as the pseudo high level code in MASM can handle multiple test and conditionals using the C runtime style of operators and this method helps you to understand these more complex conditional evaluations.

While the pseudo high level notation has its place in API and similar high level style code, they are often not fast enough for critical algorithms so what you are after is worth the effort as you will learn to write better algorithms over time.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Shantanu Gadgil

Wow, so much activity in about an hour.... :bg :U

To ic2,
the most innocent of  statements/comments can get misinterpreted, which can generate "fire-in-the-belly" reactions...  :bg :bg

Let me explain...
What I was asking foremost was...
"if you already have a while loop, why do you really need to convert it into "mnemonic (is that the word?) code"
to which...
donkey answered to the point as to why you (ic2) might be wanting to do it (speed, et al)
to which...
I agreed and also said that I stood corrected.

(Also you have said that you want to make the code compatible across assemblers, so that answers my question  :bg)

My "peeved" comment was because I interpreted your statement
QuoteCan someone translate this so it don't have to use [.while]
that you wanted someone else to do the job FOR you.

Out of context, it _DOES_ seem that way, doesn't it?

Also, don't quite appreciate the "why are you bothered, its not forum" comment.  :naughty: :naughty:

I have never claimed that, so I think the last comment of yours is justified.

Anyway...let bygones be bygones...  :bg

Regards,
Shantanu
To ret is human, to jmp divine!

asmrixstar

RE: the dump tool idea

Another similar method, ic2, would be to set up your debugger to break on 'int 3'
and then something like this in an empty masm project

int 3
.while blah<10
    inc blah
.endw

Then compile and run it. Your debugger should allow you to see code there and then.
Might help a bit speedwise since you're so time-starved. ;)

ps. dont take Shantanu comments to heart.
Hes a nice guy but he dont like slackers and script-kiddies that all too often frequent the forums looking for people to do their work for them.  :bdg

Keep it friendly,dude, and you'll find friends here.
good luck with you're efforts. :U

ic2

Thanks Tnick, for your quick idea's. It's very helpful here, i don't know where sinsi is coming from.

Thank you Votex and hutch.  On that note i will be able to understand even more ...

Forget it Shantanu Gadgil
I only expected some great ideas anyway but ended up with all kinds of great  suggestions.

I was tired and still woke and trying.  I could have posted a better way of asking for help like i usually do when ever i do, but WTH it turned out very fair.  I see your concern, hutch maybe be right at lease i hope so anyway, forgotten but i hope that any future infomation about (.while) will be posted here ...

Hey asmrixstar, Thanks, i will get this right.