CMP Instructions

From Toyota Wiki
Jump to: navigation, search

[edit] Example Code

Here is a small test for the 8C opcode by 3p141592654 from supramania.com. It seems to work exactly as advertised, namely a test for register x against an immediate value:

; start of test code
800f 8e122f ld x, #0x122f ; load x with test value
8012 8c122f cmp x, #0x122f ; compare to value 0x122f (should be equal)
8015 4609 bne j1 ; if not equal, jump to j1
8017 8f80a0 ld y, #0x80a0 ; pointer to "1 okay" string
801a 018059 jsr 0x8059 ; call printf at 0x8059 to dump to terminal
801d 038026 jmp 0x8026 ; jump to test2

LABEL:j1
8020 8f80a9 ld y, #0x80a9 ; pointer to "1 bad" string
8023 018059 jsr 0x8059 ; call printf at 0x8059 to dump to terminal

LABEL:Test2
8026 8e122f ld x, #0x122f ; load x with test value
8029 8c122e cmp x, #0x122e ; compare to value 0x122e (should be not equal)
802c 4609 bne j2 ; if not equal, jump to j1
802e 8f80b1 ld y, #0x80ba ; pointer to "2 bad" string
8031 018059 jsr 0x8059 ; call printf at 0x8059 to dump to terminal
8034 03803d jmp 0x803d ; jump to end

LABEL:j2
8037 8f80ba ld y, #0x80b1 ; pointer to "2 okay" string
803a 018059 jsr 0x8059 ; call printf at 0x8059 to dump to terminal

LABEL:end
803d 8f80d4 ld y, #0x80d4 ; stream out done
8040 6117 bsr 0x8059 ;printf
8042 40fe bra 0x8045 ; halt

and the resulting output to the terminal

Toshiba 8X test Code v1.0
1 okay
2 okay

done.

[edit] Use In If/Else Construct

This instruction can be used as a "NOP" with another two byte instruction hidden in the argument. The instruction executes as CMP (effectively NOP) unless a previous branch causes a jump to the hidden instruction. This can save a byte or two.

Here is an example by Brutus

f6dc 371f12 tbbc b.0, $_OMODE, 0xf6f1
f6df 33ffd1 ld #0xff, $0xd1
f6e2 797aa9 cmp #0x7a, $0xa9
f6e5 4406 bcc 0xf6ed
f6e7 76a9 inc $0xa9
f6e9 355901 tbbs b.2, $_ASR2L, 0xf6ed
f6ec 8c7773 cmp x, #0x7773 ; Executed only in the case no branch to 0xf6ed occurs before
f6ed 7773 setb bit3, $_ASR0NL ; Executed only in the case a branch to 0xf6ed is done
f6ef 400a bra 0xf6fb
f6f1 7573 clrb bit3, $_ASR0NL

Since there is a "branch always" after the compare opcode, the 8c byte is only there to "neutralize" the next two bytes and avoid a branch or a jump instruction that would "cost" more than one byte.

Personal tools