flat assembler
Message board for the users of flat assembler.

 Index > Heap > conditional jumps...
Author
windwakr

Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
For a long time I've wanted to know more about how computers function.
I've learned math can be done with logic gates, compares are like "test" but instead of "and", they do "sub"....now for the current thing I want to know.
How exactly does a conditional jump work?
I mean how does it tell if a flag is set, without it itself comparing and doing a conditional jump....and so on.

_________________
----> * <---- My star, won HERE
07 Jul 2008, 03:28
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17250
revolution
A flag is just a one-bit binary register inside the CPU. If the register is set (or cleared depending upon the instruction) then the jcc is allowed to proceed, else the jcc is ignored.
07 Jul 2008, 04:10
Borsuc

Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
I don't think that's what he asked revolution

As far as I can tell, he asked how it works, or a possible algorithm that makes it work (internally!).

Here my 2 cents:

Let's do it in pseudo-code, to make it easier. The '&' symbol is logical AND (and instruction). (from C as I am familiar with it). We'll also only operate on bytes to make it easier. Let's also suppose that we use the jc (jump on carry) instruction.

Say you have the following bit (conditional bit/carry) in variable 'c':

First thing to do is to negate (with neg) the variable:

Code:
`c = -c    `

Thus, if c was '1' (00000001), then it becomes -1 (11111111) in two's complement. If c was '0' (no carry flag set), then it doesn't change (0 is the same as -0) which is (00000000).

Thus now we have the two values: 11111111 if carry is set, and 00000000 is not.

The 'jc' instruction has a 'parameter', which represents a relative location (in this example), thus if the parameter is -2, then we will jump back 2 bytes, etc..

This means, the algorithm looks like this (note that the following IS NOT what you are looking for, because it uses 'ifs')

Code:
```if carry is set
{
ip += parameter  ; this adds to the instruction pointer the parameter
}    ```

the actual algorithm we're looking for is to use that add instruction only when the carry is all 1s (11111111). Thus, we need to AND the parameter with the new 'carry-mask'.

What does this do? Simple: if the carry is set (11111111) then the parameter remains the same (every value ANDed with all 1s will yield the original value). If the carry is not set (00000000) then the parameter will be 0, since we don't jump at all.

Thus the algorithm looks like this:

Code:
```c = -c
ip += (parameter & c)    ```

or in pseudo-asm:
Code:
```; the 'parameter' or 'offset' of the jump is in bl register

setc al  ; this puts the carry flag in al
neg al
and bl, al

Of course this is not valid assembly but it should provide an idea of how to do it in software. Furthermore, neg is simply the sequence of inverting all the bits (not instruction) followed by an increment (inc instruction).

These kind of bit manipulations are very VERY useful for thinking about low-level code, and it's one reason why asm makes you think different, even when coding HLLs.

Obviously in hardware it's much more 'special-case' it doesn't have a specific number of instructions, it's hard-wired but the idea is similar.
07 Jul 2008, 12:28
windwakr

Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
windwakr
The_Grey_Beast:
Thank you very much for writing a detailed clear response. Now I understand how conditional jumps work. Thanks

_________________
----> * <---- My star, won HERE
07 Jul 2008, 18:20
Borsuc

Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
07 Jul 2008, 20:45
f0dder

Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
It's a lot more complex internally though, because of things like branch prediction... glad I'm not an electronics engineer
07 Jul 2008, 22:27
bitRAKE

Joined: 21 Jul 2003
Posts: 2906
Location: [RSP+8*5]
bitRAKE

_________________