CPCWiki forum

General Category => Programming => Topic started by: BSC on 17:29, 26 March 14

Title: create a signed 16 bit offset from 8 bit
Post by: BSC on 17:29, 26 March 14
Just wanted to have some Z80 NOP squeezing chit chat :)

I am doing this to convert a signed 8 bit value in A into a signed 16 bit value in DE:


    ld e,a      ; 1 copy lo-byte
    rlca        ; 2 rotate sign into carry flag
    ld a,0      ; 4 subtract from 0 ..
    sbc a       ; 5 with carry, effectively subtracting the carry flag from 0 => a is either 0 or -1
    ld d,a      ; 6 copy as hi-byte
    ; => signed offset value in de


Is there a faster way to do that? If not: :-P
Title: Re: create a signed 16 bit offset from 8 bit
Post by: TFM on 17:59, 26 March 14
Quote from: BSC on 17:29, 26 March 14
Is there a faster way to do that? If not: :-P


Yes...


   
ld   e,a    ; 1 copy lo-byte   
rlca        ; 2 rotate sign into carry flag   
sbc  a,a    ; 3 with carry, effectively subtracting the carry flag from 0 => a is either 0 or -1     
ld   d,a    ; 4 copy as hi-byte    ; => signed offset value in de
Title: Re: create a signed 16 bit offset from 8 bit
Post by: BSC on 21:14, 26 March 14
Quote from: TFM on 17:59, 26 March 14

Yes...

   
ld   e,a    ; 1 copy lo-byte   
rlca        ; 2 rotate sign into carry flag   
sbc  a,a    ; 3 with carry, effectively subtracting the carry flag from 0 => a is either 0 or -1     
ld   d,a    ; 4 copy as hi-byte    ; => signed offset value in de



It seems that you are right ..
Title: Re: create a signed 16 bit offset from 8 bit
Post by: TFM on 22:07, 26 March 14
Auch ein blindes Huhn trinkt mal einen Korn oder so  ;)

Powered by SMFPacks Menu Editor Mod