I really like the way you have done your homework in advance, and shown us some written code.
I super especially like the fact that you commented your code; excellent sir. Now I'm happy to help (if this is indeed helpful).
Okay, I'll focus on this stuff...
mov $5, %ebx #######################
cmp %ebx, %eax # <-- HERE or #
jg problem #to long # <-- HERE #
# IS SOMETHING WRONG #
# I GUESS !! #
#output of $name #######################
Worry not; nothing's really wrong. Everybody goes through this (including me).
Wow, where do we start.
Okay, I got it, a 15 minute exercise that will clear this matter up for you.
First, question: Are you familiar with signed as opposed to unsigned math at the register and bit level ? If not, prepare for some brain cell activity.
Secondly, to confuse things even further, assembler nomenclature and syntax will have an effect on what result you can expect (and what you wind up getting).
I'm really not savvy with the GNU assembler, so let me explain how I got a grasp on this.
The instruction Cmp This,That
can be thought to mean something like...
- What is the relationship of
This
with respect to That
?
More to the point, is This
above, equal, or below That
?
I will stick with unsigned integer for now; because it's simplest.
Not sure about GNU, but with other assemblers, when you are using unsigned thinking in your brain, you want to use the instructions JA
and JB
and JE
for comparisons.
JA
is "Jump if above"
JB
is "Jump if below"
JE
is "Jump if equal"
The JG
that you are using could be signed arithmetic; we'll worry about that later.
To confuse things even more, assembler syntax can reverse the order of the operands, and you will need to alter your mental state and think this question instead of the original one...
- What is the relationship of
That
with respect to This
?
Okay, now, setting breakpoints, here's how you get your mind into this..
Mov $5, %ebx # Your original way
Cmp %ebx, %eax # Same compare
Ja problem # Unsigned maybe jump
Mov $5, %ebx # Same steps
Cmp %ebx, %eax # Same steps
Jb problem # Opposite jump sense
Mov $5, %ebx # Now, similar steps
Cmp %eax, %ebx, # Comparing them in an opposite manner
Ja problem # Watch to see if this jump hits
Mov $5, %ebx # Arrange them again
Cmp %eax, %ebx, # Backwards compare, but,,,,
Jb problem # Opposite opinion of the flags
These four examples assume that the two numbers (in Ebx and Eax) are different. If they turn out to be the same, then we will have an extended discussion on this matter another day.
This is, again, all about unsigned integer arithmetic. For some, that would be elementary stuff; for others, worry not, struggling around with confusion is, honestly, the norm. I know I did it.
Anyway, set breakpoints and run each of those three line snippets, and get the idea in your head, "...What is the relationship of THIS with respect to THAT ?..."
As you step through each 3 line snippet, you'll get the idea.
If you screw up, it will make sense the second (or third or fourth or fifth) time.
Not sure if this is helping or not, just thought I would type it up for others who are new to the regs and bits and flags and stuff.