📄 code_asm.asm
字号:
;/*****************************************************************************
;*
;* Motorola Inc.
;* (c) Copyright 2001 Motorola, Inc.
;* ALL RIGHTS RESERVED.
;*
;******************************************************************************
;*
;* File Name: code_asm.asm
;*
;* Description:
;* File contains basic mathematical functions,
;*****************************************************************************/
;/*****************************************************************************
;* External Symbol Definition
;*****************************************************************************/
XDEF udiv_16to8
XDEF shl_8
XDEF shl
XDEF ushl_8
XDEF add_8
XDEF add
XDEF uadd_8uu
XDEF uadd_uu
XDEF sub_8
XDEF sub
XDEF usub_8uu
XDEF usub_uu
XDEF sub_8uu
XDEF neg_8
;/*****************************************************************************
;*
;* Module: unsigned char udiv_16to8(unsigned int x, unsigned int y)
;*
;* Description:
;* Unsigned dividing 16 bit by high 8 bit from 16 bit Uword.
;* Both divisor and divident are scaled to get high results precision.
;* The Result is saturated at 0xFF if overflow occures.
;*
;* Returns: 256*x/y
;*
;* Arguments: x (in)
;* y (in)
;*
;* Range Issues: if y=0 the result is saturated at 0xFF
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
udiv_16to8:
TSTA ;/* if (y=0 return 0xFF)*/
BNE scalein
TSTX
BEQ zerodiv
scalein:LSLA ;/* scale divisor */
ROLX
BCS rangeout ;/* the divisor>0xFFFF */
LSL 4,SP ;/* scale divident */
ROL 3,SP
BCC scalein ;/* divident<=0xFFFF */
ROR 3,SP
ROR 4,SP
CLC
rangeout: ;/* make division */
RORX
LDA 3,SP
PSHA
PULH
LDA 4,SP
DIV
BCC divok ;/* if not overflow */
zerodiv:LDA #0FFh
divok: RTS
;/*****************************************************************************
;*
;* Module: signed char shl_8(signed char x, unsigned char n)
;*
;* Description:
;* The function returns value x arithmeticall shifted by n bits result
;* with overflow control and saturation. The 8-bit result is set at +127
;* when overflow occurs, or at -128 when underflow occurs.
;*
;* Returns: x * 2^n
;*
;* Arguments: x (in)
;* n (in)
;*
;* Range Issues: None
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
; signed char shl_8 (signed char x, unsigned char n)
shl_8: TSTX
BEQ shlend8 ;/* if(n=0) return */
TSTA
BEQ shlend8 ;/* if(x=0) return */
BMI shlneg8 ;/* x<0 */
;/* shift for positive x */
shlpos8:LSLA ;/* while (n>0) {x<<1; test overflow; n--;} */
BMI posout8 ;/* overflow */
DBNZX shlpos8
RTS
posout8:LDA #07Fh ;/* too high */
RTS
;/* shift for negative x */
shlneg8:LSLA ;/* while (n>0) {x<<1; test overflow; n--;} */
BPL negout8 ;/* overflow */
DBNZX shlneg8
RTS
negout8:LDA #080h ;/* too low */
shlend8:RTS
;/*****************************************************************************
;*
;* Module: signed int shl(signed int x, unsigned char n)
;*
;* Description:
;* The function returns value x arithmetical shifted by n bits result
;* with overflow control and saturation. The 8-bit result is set at +32767
;* when overflow occurs, or at -32768 when underflow occurs.
;*
;* Returns: x * 2^n
;*
;* Arguments: x (in)
;* n (in)
;*
;* Range Issues: None
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
shl: PSHA ;/* save n */
LDA 5,SP ;/* L byte of x */
LDX 4,SP ;/* H byte of x */
TST 1,SP ;/* (if n>0) */
BEQ shlend ;/* { */
TSTX ;/* if (x=0) return */
BNE x_not_0
TSTA
BEQ shlend
TSTX ;/* if(x>=0) */
x_not_0:BMI shlneg ;/* { */
shlpos: LSLA ;/* while(n>0) */
ROLX ;/* { */
BMI posout ;/* x<<1 */
DBNZ 1,SP,shlpos ;/* if (x>32767) {x=+32767; return */
PULH ;/* n--; */
RTS ;/* } */
posout: LDA #0FFh ;/* } */
LDX #07Fh
PULH
RTS
shlneg: LSLA ;/* else */
ROLX ;/* { */
BPL negout ;/* while (n>0) */
DBNZ 1,SP,shlneg ;/* { */
PULH ;/* x<<1 */
RTS ;/* if (x<32768) {x=-32768; return */
negout: CLRA ;/* n--; */
LDX #080h ;/* } */
shlend: PULH ;/* } */
RTS ;/* } */
;/*****************************************************************************
;*
;* Module: unsigned char ushl_8 (unsigned char x, unsigned char n)
;*
;* Description:
;* The function returns value x arithmeticall shifted by n bits result
;* with overflow control and saturation. The 8-bit result is set at +255
;* when overflow occurs.
;*
;* Returns: x * 2^n
;*
;* Arguments: x (in)
;* n (in)
;*
;* Range Issues: None
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
; unsigned char ushl_8 (unsigned char x, unsigned char n)
ushl_8: TSTX
BEQ ushlend8 ;/* if(n=0) return */
TSTA
BEQ ushlend8 ;/* if(x=0) return */
;/* shift for positive x */
ushlpos8:LSLA ;/* while (n>0) {x<<1; test overflow; n--;} */
BCS uposout8 ;/* overflow */
DBNZX ushlpos8
RTS
uposout8:LDA #0FFh ;/* too high */
ushlend8:RTS
;/*****************************************************************************
;*
;* Module: signed char add_8(signed char x, signed char y)
;*
;* Description:
;* The function performs the addition x+y with overflow control
;* and saturation. The 8-bit result is set at +127 when overflow occurs,
;* or at -128 when underflow occurs.
;*
;* Returns: x + y
;*
;* Arguments: x (in)
;* y (in)
;*
;* Range Issues: None
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
; /* (V - Overflow Flag, N - Negative flag) */
; /* V=1 & N=1 too high */
; /* V=1 & N=0 too low */
add_8: PSHX
TSX
ADD ,X ; /* x + y */
PULH
BLT ad8tlow ;V^N: /* can be too low */
BPL ad8ret1 ;V=0 & N=0
LDA #07Fh ;V=1 & N=1 /* too high */
ad8ret1:RTS
ad8tlow:BMI ad8ret2 ;V=0 & N=1
LDA #080h ; /* too low */
ad8ret2:RTS
;/*****************************************************************************
;*
;* Module: signed int add(signed int x, signed int y);
;*
;* Description:
;* The function performs the addition x+y with overflow control
;* and saturation. The 16-bit result is set at +32767 when overflow occurs,
;* or at -32768 when underflow occurs.
;*
;* Returns: x + y
;*
;* Arguments: x (in)
;* y (in)
;*
;* Range Issues: None
;*
;* Special Issues: The result is saturated
;*
;*****************************************************************************/
; /* (V - Overflow Flag, N - Negative flag) */
; /* V=1 & N=1 too high */
; /* V=1 & N=0 too low */
add: ADD 4,SP ; /* x + y */
PSHA
TXA
ADC 4,SP
TAX
PULA
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -