ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Пятница
22 ноября
134707 Топик полностью
MBedder, терминатор (18.10.2008 16:59, просмотров: 384) ответил whale на на асме
На, лентяй  ;******************************************************************** ; Умножение знаковых и беззнаковых целых 16-битных переменных ; на постоянный коэффициент k = 0.00000..0.99998 без применения float ; (например, для масштабирования значений АЦП). ;******************************************************************** ;-------------------------------------------------------------------- ; Scales unsigned int r5r4 by unsigned r17r16 value presented as fractional (0000 = 0, FFFF = 0.99998) ; ; Any registers can be used as operands ; Result stored in r9r8 ; 14 program words, 17 clocks + ret ; ; (c) 2008 MBedder ; scale16uu: clr r2 mul r5,r17 ; r5r4 = 23456, r17r16 = 0.76543*65536 movw r8,r0 mul r4,r16 mov r7,r1 mul r5,r16 add r7,r0 adc r8,r1 adc r9,r2 mul r4,r17 add r7,r0 adc r8,r1 adc r9,r2 ; r9r8 = 23456*0.76543 =~ 0x4621 = 17953 ret ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ; Scales signed int r19r18 by unsigned r17r16 value presented as fractional (0000 = 0, FFFF = 0.99998) ; ; Only r16..r23 registers can be used as operands due to the MULSU command limitations ; Result stored in r9r8 ; 15 program words, 18 clocks + ret ; ; (c) 2008 leon_ ; scale16su: clr r2 mulsu r19,r17 ; r19r18 = -23456, r17r16 = 0.76543*65536 movw r8,r0 mul r18,r16 mov r7,r1 mulsu r19,r16 sbc r9,r2 add r7,r0 adc r8,r1 adc r9,r2 mul r18,r17 add r7,r0 adc r8,r1 adc r9,r2 ; r9r8 = -23456*0.76543 =~ 0xB9DE = -17953 ret ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ; Scaling test code .equ myint = 23456 ; int to be scaled .equ coeff = 0.76543*65536 ; Scaling coefficient*2e16 ; ldi r16,low(myint) ldi r17,high(myint) movw r4,r16 ; r5r4 = myint ldi r16,low(coeff) ldi r17,high(coeff) ; r17r16 = coeff rcall scale16uu ldi r18,low(-myint) ldi r19,high(-myint); r19r18 = -myint rcall scale16su rjmp PC ;--------------------------------------------------------------------