Jump to content

Inverted syntax: Difference between revisions

no edit summary
m (Updated description and link for Fōrmulæ solution)
No edit summary
Line 23:
 
The task is to demonstrate support for inverted syntax forms within the language by showing both the traditional and inverted forms.
=={{header|6502 Assembly}}==
Conditional branches are limited in the range they can branch; all branch statements cannot be more than 127 or -128 bytes away from their destination.
 
The below example will result in an assemble time error. The intended action is that the X variable is decremented, and execution returns to the top of the loop. However, the branch is simply too far away.
<lang 6502asm>loop_MySubroutine:
; more than 127 bytes of code
 
dex
bne loop_MySubroutine ;assembler will display an error message that the branch is too far away.
; rest of program</lang>
 
The easiest way to overcome this limitation is to invert the branch condition and place a <code>JMP</code> back to the loop under it.
An unconditional <code>JMP</code> moves the program counter to the specified location, no matter where that is.
 
<lang 6502asm>loop_mySubroutine:
; more than 127 bytes of code
 
dex
beq continue
JMP loop_mySubroutine
continue:
; rest of program</lang>
 
Now, what happens is that the opposite condition is checked. If X = 0, execution branches 3 bytes forward to "continue." Otherwise, the statement underneath the branch is executed, which is a jump back to the top.
=={{header|Ada}}==
 
1,489

edits

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