Jump to content

Conditional structures: Difference between revisions

m (→‎{{header|68000 Assembly}}: clarification and correction)
Line 249:
 
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.
 
===Switch===
Switch and cases are easy to implement with a return spoof. If the cases are not a consecutive numeric sequence like in the example below, you can use a lookup table to match the selector variable's values with an index into the table of case routine addresses.
 
There is no built-in way to "default" if none of the expected cases match. A bounds check will have to be programmed in manually. Most of the time when writing a return spoof the programmer already knows what the maximum possible cases will be.
 
<lang 68000devpac>SwitchCase:
DC.L foo,bar,baz,default ;case 0, case 1, case 2, case 3. (Case 0,1,2 are the "valid" cases.)
; D0 is the case selector variable (byte-sized)
doSwitchCase: ;this is a subroutine that gets called elsewhere.
LEA SwitchCase,A0
 
;this is our bounds check
CMP.B #3,D0 ;is D0 > 3?
BLS InBounds ;if not, keep going
MOVE.B #3,D0 ;if it is, set it to 3.
 
 
InBounds:
LSL.W #2,D0 ;multiply by 4 to index into a table of longs
MOVE.L (A0,D0),A0 ;deref the pointer and store the desired routine in A0
MOVE.L A0,-(SP) ;push it onto the stack
RTS ;"return" to the selected routine. If it ends in an RTS,
; that RTS will return to just after "JSR doSwitchCase"
 
 
foo:
;your code for this case goes here.
rts
 
bar:
;your code for this case goes here.
rts
 
baz:
;your code for this case goes here.
rts
 
default:
rts</lang>
 
=={{header|AArch64 Assembly}}==
1,489

edits

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