flat assembler
Message board for the users of flat assembler.

Index > Main > Newbie - Conditional Jumps and Logical Operand Comparisons

Thread Post new topic Reply to topic

Joined: 12 Jul 2006
Posts: 2
I think the tables from the following links should be added to the FASM documentation section - perhaps tacked onto the "Programming Tutorial".

Which flags [ OF, SF, ZF, AF, PF, CF ] are *checked* by which conditional jump instructions?

Which flags are *set" by the (cmp, or, xor, etc) logical operand comparison instructions?

I am familiar with conditional jumps from VB and started programming with a pre-gui "TI-99 4/A" way back when. None the less, when I saw this code, though I could tell what the condidtional loop did and why, I could not tell what was being referenced (which flags?) to do it.

I hope the following will help future ASM newbies with conditional jumps in assembler. I am putting this post in the Main section because the logic applies everywhere. The code snippet below, however, was taken from a tutorial reference on WinAPI.

Below is a snippet of code from Iczelion's tutorial #3 which was translated to FASM by "sulaiman" - see original post
(My gratitude to both for their contributions.) I've only included the conditional loop bit, as that is where I had to go google-ing for the table references above. The rest I could figure out with the comments Iczelion provided and from knowing VB.

I have commented below the code for a newbie, but please check to see that I expained it correctly. If this is helpful, I would be happy to write FASM explanations for the other (MASM) tutorials as I go along. I have to do it, regardless, to "grok" it.

<><> Code <><>

invoke GetMessage,wMsg,NULL,0,0
or eax,eax
je window_message_loop_end

<><> End Code <><>

>>Line 1

GetMessage returns FALSE (1) to eax register only if WM_QUIT message is received.

>>Line 2

The "or" instruction sets C & O flags to (0) everytime regardless of the
logical result.

The logical result of the "or" instruction, when comparing two operands, is to return (0) when both operands are (0) and to return (1) in all other conditions.

When "or" is used to compare something to itself, as in this case, a simplified logic applies. It returns (0) when the operand is (0) and returns (1) otherwise. This logical result is applied to the Z, S, and P flags.

As any statement that sets or clears a flag can serve as a test basis for a conditional jump, this allows us to run the test in line #3.

>>Line 3

The "je" instruction looks to the Z flag to determine whether or not to jump.

As a result of the test in line #2, the Z flag is "set" (1) only if a WM_Quit message has been returned to eax by the "GetMessage" command.

"je" jumps only when the Z Flag is "set" (1) and does not jump when Zero Flag (ZF) is "clear" (0). I think of it as "Jump when ZF Equals 1".

"je" is functionally identical to "jz"
Post 13 Jul 2006, 21:40
View user's profile Send private message Reply with quote

Joined: 04 Jun 2006
Posts: 50
Location: Australia

Table 2.1 in the FASM manual lists which flags are tested by each condition. Also, the flags that are are provided in the section describing the instructions.

However, let me say that IMHO the manual could do with a complete re-write. For starters, the English need some scubbing up. Also, I think that there should be more emphasis on listing items in tables rather than being lost in long wordy paragraphs. I am thinkng of the various directives, for example, which could be organised into a table so that people can visualise all of them at a glance. Perhaps more example code should also be part of the manual.

Now after being critical, let me express my sincere respect to Tomasz for the work that has been done and in his humility. I think we should all support him to our very best and keep working towards improving an already supurb product.

Chris Leslie
Post 14 Jul 2006, 03:14
View user's profile Send private message Reply with quote

Joined: 12 Jul 2006
Posts: 2
I searched for "je" and came up empty; but after looking at Table 2.1, I see now that "j" and "e" are really seperate things combined. This Table makes more sense than listing all the possible combinations.

This is exactly the connection I was looking for. Coming from a HL language, retraining the brain to see the little pieces is the trick.

Post 14 Jul 2006, 15:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.