[Flashforth-devel] Switching Endians -- swapbytes for FF18
Brought to you by:
oh2aun
From: craig b. <dab...@ya...> - 2014-03-27 18:21:58
|
I needed to swap bytes to send to another machine, so I implamented one of my favorite tricks from "back in the day" when I was learning MACRO-11 on the college PDP-11/40... Swapping register contents without auxiliary storage. Mike's made it easy for us in the Pic18-s, probably the rest of the chips, too. swapbytes ( xy -- yx ) \ reverses byte order on Top-Of-Stack craig bair --------------- \ Reverse ENDIAN-ness of 16-bit on Top Of Stack \ i.e. Swap Bytes \ ...minimalist approach, no temp storage loc \ \ needed constants (could be hardwired into code $ffe9 constant fsr0 \ ; 0xFE9 FSR0L \ $ffea constant fsr0h \ ; 0xFEA FSR0H $ffef constant indf0 \ INDirect-by-FSR0 FileRegister 1 constant f, \ Destination File \ \ needed assembler constructor : as2 ( opcode "name" -- ) ( f a -- ) co: does> rot ic, or ic, ; \ \ needed assembler instructions $04 as3 decf, ( f d a -- ) $28 as3 incf, ( f d a -- ) $18 as3 xorwf, ( f d a -- ) $6e as2 movwf, ( f a -- ) \ \ actual word : swapbytes ( xy -- yx ) \ swap Hi:Lo bytes on Top of Stack [ indf0 w, a, movf, ] [ fsr0 f, a, decf, ] [ indf0 f, a, xorwf, ] [ indf0 w, a, xorwf, ] [ indf0 f, a, xorwf, ] [ fsr0 f, a, incf, ] [ indf0 a, movwf, ] ; ...averaging may or may not help clarify data. If Bill Gates walks into a bar, the average patron becomes a millionaire... |