8. Jumps and branching
Most programs need to run code that is dependent on the outcome of some test within the software, this is called 'branching'.
Common programming forms include loops, iteration, subroutine calls and function calls, and these need branching and jumping to work.
Sometimes the code needs to jump to another location regardless. This is called an unconditional jump. For example to avoid the next line of code being exectued at the end of a routine.
On the other hand, a jump may be dependent on some condition being met such as a flag being 1 or 0. This is called a conditional jump.
A relative jump will cause the instruction an offset number away from the current one to be executed on the other hand an absolute jump will go to the instruction at a specific address.
Memonics to support branching and jumping include
Mnemonic | Comment | Code | Operation |
---|---|---|---|
JMP | Jump | JMP dest | Jump to dest |
JE | Jump if equal | JE dest | |
JZ | Jump if zero | JZ dest | |
JG | Jump if greater than | JG dest | |
JL | Jump if less than | JL dest | |
CALL | Call a subroutine | CALL dest | |
RET | return from subroutine | RET | Uses the stack to find the return address |
The conditional jumps shown above usually look to the Program Status Word (PSW) register to see if the zero or carry flag has been set, if the condition is true, then the program counter is loaded with the address of the instruction given in the jump command.
The CALL and RET commands make use of the stack to locate subroutine locations and return addresses.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: writing assembly language
Copyright © www.teach-ict.com