Jump to content

Conditional structures: Difference between revisions

m (coming back later to reduce lag on my computer)
Line 220:
RTS ;this "RTS" acts as a JMP to the address we just put on the stack.</lang>
=={{header|68000 Assembly}}==
Like [[6502 Assembly]], 68000 Assembly has several different condition states the CPU can use to branch. As is typical with assembly languages, branching code is less straightforward than on high-level languages. There is no "if" statement per se; the correct branch to use depends more so on the expression being evaluated.
 
===CMP===
The most commonly used comparator is <code>CMP</code>. It can operate at byte, word, or long length. Anything outside of the "scope" of its size parameter is ignored.
<lang 68000devpac>MOVE.L #$FFFFFF00,D0
CMP.B #0,D0 ;equals zero, so zero flag is set.
CMP.W #0,D0 ;doesn't equals zero, so zero flag is clear.</lang>
 
Other than its variable size parameter, <code>CMP</code> works just like it does in [[6502 Assembly]]. It returns both a test for equality and an unsigned comparison. This chart from [http://www.easy68k.com/paulrsm/doc/trick68k.htm 68000 Tricks and Traps] sums it up nicely. If you use <code>CMP D0,D1</code> at any data size, this is what you get:
<pre>
Relationship Signed Unsigned
-------------------------------------------------------
D1 < D0 BLT BCS (branch on Carry Set)
D1 <= D0 BLE BLS
D1 = D0 BEQ BEQ
D1 <> D0 BNE BNE
D1 > D0 BGT BHI
D1 >= D0 BGE BCC (branch on Carry Clear)
</pre>
 
===Bit Testing===
Individual bits can be tested with <code>BTST</code>, <code>BSET</code>, <code>BCLR</code>, and <code>BCHG</code>. The <code>BTST</code> command takes a bit as its left operand and the value being tested in the right (either a data register, address register with or without parentheses, or memory address).
<lang 68000devpac>BTST #7,D0 ;test bit 7 of D0, i.e. the leftmost bit in the rightmost byte.
BNE goHere ;if that bit is 1, branch to "goHere"
BEQ goThere ;if that bit is 0, branch to "goThere"</lang>
 
<code>BSET</code>, <code>BCLR</code>, and <code>BCHG</code> are similar, in that they also allow you to branch based on the value of the bit being tested. However, they also alter the bit in the destination that was tested, AFTER the test. The new state of that bit is not reflected in the test results. Branching occurs as if you used <code>BTST</code> instead. <code>BSET</code> makes the bit in the destination 1, <code>BCLR</code>makes it zero, and <code>BCHG</code> flips it.
 
In addition to bit testing, <code>TST</code> will set the processor flags as if the value in a register or memory was just loaded there, even if it had been there for a while. This does not change the value in any register or memory; it just updates the flags, so it is very handy for introspection into the CPU's internal memory without altering it in any way.
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.