Basically what I want to do is count from 0 to 1,000,000,000 (1 billion.) I thought I could do this by copying 0 into eax and then putting it in a loop until it reached there. I thought I could do this by incrementing it, comparing it to a billion, then jumping back to a label if it's less.

But my program runs for a second, and then crashes. I know it's working as far as incrementing eax, because the error message prints out the values in the registers and eax == 3b9aca00 (hex for 1 billion.)

Please help me out, I've been reading about masm all day and I tried writing this program without copying any source code, only using opcode references.

Alright, here's what I have:

--------------------------------------

.386
.model flat, stdcall
option casemap:none

include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

.data
countto dd 1000000000

.code
start:
mov eax, 0

loc1:
inc eax
cmp eax, countto
jb loc1

end start
Posted on 2002-03-27 14:57:43 by Ronin Magus
You need to call exit process: invoke ExitProcess, NULL before the END Start.
Posted on 2002-03-27 14:59:18 by stryker
Yay!! That worked. Thanks a whole whole lot!
Posted on 2002-03-27 15:15:46 by Ronin Magus
Keep it up! Small steps are the way to go and not overlook things (like what you just learned ;) )


Other ways of doing this are:

.data 

countto dd 1000000000

.code
start:
xor eax, eax ; Smaller than (mov eax, 0) in bytes, but same!

loc1:
inc eax
cmp eax, countto
jb loc1

invoke ExitProcess, NULL
end start


Or squeezing out a few more bytes:
.data 

.code
start:
xor eax, eax

loc1:
inc eax
cmp eax, 1000000000 ; No need for a Constant in .data :)
jb loc1

invoke ExitProcess, NULL
end start


More advanced, you can make the labels more 'anonomous' (saves nothing, but typing efforts). The @@: is an anonomous label. MASM will make up a lable every time is comes across one, so you dont have to think and keep track of labels when they have a mild purpose like the looping your doing. DONT use these if the lable IS important and alot draws upon it! But since you *had* to make up a lable 'loc1' to spell out your loop, MASM can save you some burden here. To reference it you use @F and @B for "to the anonomous label found FORWARD in memory from here" and "to the anonomous label found BACKWORD in memory from here" respectively.

Thus you can now do this:
.data 

.code
start:
xor eax, eax

@@: ; Anonomous Lable!!
inc eax
cmp eax, 1000000000

jb @B ; Jump Backwards to the FIRST anonomous label

invoke ExitProcess, NULL
end start


Once last trick you can do (less advanced) is not use the ZERO flag to your advantage if you dont care how you get to 1 billion times through the loop. Instead of counting UP to an arbitrary number, you can count DOWN *from* an arbitrary number. When you hit zero, the ZERO flag will automatically be set by the processor. Thus you can eliminate the need for comparing for 1 billion, 1 billion times :)
.data 

.code
start:
mov eax, 1000000000
@@: ; Anonomous Lable!!
dec eax ; decrease EAX will automatically set Zero Flag when eax=0
jnz @B ; Jump Backwards to the FIRST anonomous label IF NOT ZERO

invoke ExitProcess, NULL
end start



Anywho, i tossed these ideas out there for you to 'build' upon as you learn. Its this flexibility of Assembly that keeps many people working in assembly and seeking new ways of getting things done... (discussions like this become hard-core in the Algo's section ;) , but begginer type questions are also welcome in the MAIN section )

Best of luck!
:alright:
NaN
Posted on 2002-03-30 02:59:40 by NaN
more typing time savers for simple parameters:
instead of NULL or MB_OK, just type 0 :)
also, if you're like me, and spend most of your time using HEX while writing your programs, it might be worth your while to define
.RADIX 16
in the beginning of your program so as to remove the need to append an "h" to all your numers. besides, i used to use DEBUG to write my programs (really) and i forget to include the "h" most of the time... and i spend a long time debugging.
Posted on 2002-03-30 10:46:48 by jademtech
Sorry for this newbie question, I assume the nemonic jb means "jump back". That is indeed what is occuring in the code.

Is there any place I can go to find the meaning of all the nemonics in Assembler ?

Whew ! this is a real newbie question ! Sorry guys.
Posted on 2002-04-01 12:04:13 by DarkStar
jb(jump if below than) this only works on unsigned numbers.
jl(jump if less than) works with signed numbers.

:)
Posted on 2002-04-01 12:14:23 by stryker
Intel References are the best, but technical.

For quick "heads up" information, i often consult my handy dandy little dos program called HelpPC 21

:alright:
NaN
Posted on 2002-04-01 16:20:01 by NaN

Sorry for this newbie question, I assume the nemonic jb means "jump back". That is indeed what is occuring in the code.

Is there any place I can go to find the meaning of all the nemonics in Assembler ?

Whew ! this is a real newbie question ! Sorry guys.
Please don't be sorry.. nobody borns with knowledge.

You may keep this table handy.. at least for the first months (I had to do it too, since I'm quite new to the x86 architecture):



;branch unsigned <= JBE JNA .. (C|Z)=1
;branch unsigned < JB JNAE JC .. C=1
;branch unsigned > JA JNBE .. (C|Z)=0
;branch unsigned >= JAE JNB JNC .. C=0

;branch signed <= JLE JNG .. ((S^O)|Z)=1
;branch signed < JL JNGE .. (S^O)=1
;branch signed > JG JNLE .. ((S^O)|Z)=0
;branch signed >= JGE JNL .. (S^O)=0

;equal JE JZ .. Z=1
;not equal JNE JNZ .. Z=0

;zero JZ JE .. Z=1
;not zero JNZ JNE .. Z=0

;sign JS .. S=1
;not sign JNS .. S=0

;carry JC .. C=1
;not carry JNC .. C=0

;overflow JO .. O=1
;not overflow JNO .. O=0

;parity (even) JP JPE .. P=1
;not parity (odd) JNP JPO .. P=0

;ecx=0 JECXZ .. ECX=0
;cx=0 JCXZ .. CX=0
Posted on 2002-04-01 17:57:22 by Maverick
Thanks for the info guys. :alright:

I do alot of my programming in C/C++ ( I still need more experince ), but I think learning Assembler should be at the top of my list. Assembler is so cool !
Posted on 2002-04-02 12:33:31 by DarkStar

Intel References are the best, but technical.

For quick "heads up" information, i often consult my handy dandy little dos program called HelpPC 21
I second that.. great tool. Still today when I think of it I feel sad about the death of the author, though.. :(
Posted on 2002-04-02 18:24:50 by Maverick
who's the author? :/
Posted on 2002-04-02 18:50:22 by f0dder
David Jurgens, died in 1991.
Posted on 2002-04-02 18:58:56 by Maverick
The easiest way to get to a nice opcode refrence is to use
this little help progy.
http://linux20368.dn.net/protools/files/docs/opcodes.zip
It served me well (and sometimes it still does).
So just print the jxx reference keep it close to your hart ;)
In this way you'll soon start to feel the magic of positive and negative logic you can use in your progies.


2nd a better description of the instructions, but normaly you just need a fast look and you're off, well whatever it's all up to a persons wishes
http://linux20368.dn.net/protools/files/docs/asmhelp.zip

If the links doesn't work go to:
http://protools.cjb.net/ ->documents


Maybe just another advice from me:

'The past was onced your future. So don't stop looking at the past, as you'll soon stop looking to the future. And we are looking to the future.'

Peace
Mostek

Ok beem me up Scotty.

ssssszzzzZZZZZiiiiiiiiiiiPPPPPPpp

;)


Maverick: Well as always the diference bettween the profesionals and a Masters is in little thing like that.
Yep, you saved the day. ;)
Posted on 2002-04-05 05:40:29 by Mostek
Recently I learnt ( stupid me :grin: ) that there's this little "EDIT" button, near "QUOTE", that will do magic things to your own messages. :)

Try it!
Posted on 2002-04-05 06:36:21 by Maverick