ASR vs ASL and sign bit - Page 1
Forum section: electronics / general

author: Circlotron
author_rank: Super Contributor
timestamp: August 30, 2018, 01:06:18 am
content: If we do an ASR the sign bit remains unchanged, but if we do an ASL (same as LSL) the sign bit gets overwritten. Why is this allowed/meant to happen?

author: helius
author_rank: Super Contributor
timestamp: August 30, 2018, 01:11:43 am
content: If you left shift a data word so that it overflows, the result is inevitably wrong. Whether it has the right sign or not is a rather moot point.Many ISAs do not have separate opcodes for logical and arithmetic left shift. This means that there are 3 shift opcodes.

author: Circlotron
author_rank: Super Contributor
timestamp: August 30, 2018, 01:16:58 am
content: Quote from: helius on August 30, 2018, 01:11:43 amTrue, but if the sign bit got pushed into the carry bit you could deal with it. But if the sign bit is trashed even when there is no overflow...Talking about 'HC908 BTW.

author: JS
author_rank: Frequent Contributor
timestamp: August 30, 2018, 02:40:01 am
content: Quote from: Circlotron on August 30, 2018, 01:16:58 amCheck it before!?JS

author: helius
author_rank: Super Contributor
timestamp: August 30, 2018, 03:08:33 am
content: Quote from: Circlotron on August 30, 2018, 01:16:58 amIf the sign bit changes as the result of a left shift, it is an overflow by definition.The instruction table (Table 8-1) of the 68HC908 data sheet is not completely helpful in this regard:Its entry for ASR (arithmetic shift right) shows the V (overflow) bit and N (negative) bit as being set or reset according to the result, but arithmetic right shift never overflows (the sign bit always remains the same). So the V bit should say 0 (always reset) and the N bit should say - (never changed). This makes the difference between ASR and ASL clearer.

author: Circlotron
author_rank: Super Contributor
timestamp: August 30, 2018, 06:44:52 am
content: Actually I think I’m starting to get it now. Left shifting anything greater than -63 will cause an overflow.-63 or less will have msb (bit 6) as 1 so after left shift it will then occupy bit 7 position and look like a sign bit. I think...

author: hamster_nz
author_rank: Super Contributor
timestamp: August 30, 2018, 07:54:24 am
content: ... and don't forget, ASR is not the same as divide by 2!

author: newbrain
author_rank: Super Contributor
timestamp: August 30, 2018, 08:49:05 am
content: Quote from: hamster_nz on August 30, 2018, 07:54:24 amI se that as divide by two, with rounding towards the lowest value (not the lowerabsolutevalue).IIRC, it's specified like that in ANSI forth, but cannot check now.EtA: This is one of the things that changed between C89 and C99, in the former standard the result was implementation defined, so an asr could well be the implementation for /2, in the latter rounding towards 0 is specified.

author: brucehoult
author_rank: Super Contributor
timestamp: August 30, 2018, 09:44:53 am
content: Quote from: hamster_nz on August 30, 2018, 07:54:24 amIt's close.You can follow the C standard correctly by adding 1 before shifting, if and only if the number is negative. In other words, unconditionally add the sign (leftmost bit).On a machine like RISC-V with fast large shifts but no condition codes you can do it like:Code:srlitmp,x,63addx,x,tmpsraix,x,1On a machine with slow or only single bit shift, but with condition codes you can do it like:Code:lsl tmp,x,1adc x,x,0asr x,x,1If you've only got a single accumulator, e.g. 6502, then you need to juggle a little:Code:taxasl atxaadc #0asr agcc on x86_64 chooses the first method even though it has a carry flag.Any of these is going to be massively faster than doing a real divide, even if it's not just a single instruction.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 30, 2018, 01:25:14 pm
content: Quote from: brucehoult on August 30, 2018, 09:44:53 amIt depends on the CPU. For example, PIC16 has a single accumulator, but you don't need to juggleCode:btfsc a,7incf a,fasrf a,f

author: JPortici
author_rank: Super Contributor
timestamp: August 30, 2018, 02:01:07 pm
content: mm..PIC16 base/midrange core (PIC16Fxxx) doesn't have any arithmetic shift.. it only has RRF/RLF, ROTATE through carry.PIC16 enhanced midrange core (PIC16F1xxx) does have arithmetic right shift (ASRF), which preserves the MSB.. and it also has LSLF/LSRFStrange enough, PIC18 doesn't have shift instructions instead, only rotate instructions

author: NorthGuy
author_rank: Super Contributor
timestamp: August 30, 2018, 02:40:11 pm
content: Quote from: JPortici on August 30, 2018, 02:01:07 pmThese don't have any shifts at all, so you would have to manufacture ASR and bust WREG:Code:rlf a,wrrf a,fQuote from: JPortici on August 30, 2018, 02:01:07 pmMicrochip design decisions are often ... strangePIC18 is an example where Microchips didn't think well about many things.

author: DJohn
author_rank: Regular Contributor
timestamp: August 30, 2018, 02:58:56 pm
content: Quote from: brucehoult on August 30, 2018, 09:44:53 amSadly, the 6502 has no asr. This is the best I can do:Code:cmp #128adc #0cmp #128ror a

author: brucehoult
author_rank: Super Contributor
timestamp: August 31, 2018, 12:22:42 am
content: Quote from: DJohn on August 30, 2018, 02:58:56 pmOh, bugger, you're right! :-( 6800 has asr but its nephew the 6502 only has asl (where it doesn't matter) and lsr.It's over 35 years since I actually programmed those in assembler.