⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 math8051.asm

📁 A collection of math routines including 8-bit, 16-bit, 32-bit signed and unsigned addition, subtract
💻 ASM
📖 第 1 页 / 共 3 页
字号:
;*****************************************************************;*                                                               *;*         Maths Subroutines for the 8051 microcontroller        *;*                      W.G.Marshall 2002                        *;*                                                               *;*****************************************************************; All parameters in Register bank 0, (r0 to r7); Bits 21H and 22H reserved for sign bits;=================================================================; subroutine Cr0; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:     r0 = signed byte;; output:    r0 = magnitude;            Bit 21H = sign (21H is set if r0 is a negative number);; alters:    acc;=================================================================Cr0:           mov     a, r0           ; read X into accumulator               jb      acc.7, Cr0a     ; X is negative if bit 7 is 1               clr     21H             ; clear sign bit if 'positive'               ret                     ; doneCr0a:          cpl     a               ; X negative, find abs value               inc     a               ; XA = complement(XT)+1               mov     r0, a           ; save magnitude               setb    21H             ; set sign bit if 'negative'               ret;=================================================================; subroutine Cr1; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:     r1 = signed byte;; output:    r1 = magnitude;            Bit 22H = sign (22H is set if r1 is a negative number);; alters:    acc;=================================================================Cr1:           mov     a, r1           ; read X into accumulator               jb      acc.7, Cr1a     ; X is negative if bit 7 is 1               clr     22H             ; clear sign bit if 'positive'               ret                     ; doneCr1a:          cpl     a               ; X negative, find abs value               inc     a               ; XA = complement(XT)+1               mov     r1, a           ; save magnitude               setb    22H             ; set sign bit if 'negative'               ret;===================================================================; subroutine Cr0r1; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:    r1, r0 = signed word;; output:   r1, r0 = magnitude;           Bit 21H = sign (21H is set if negative number);; alters:   acc, C;===================================================================Cr0r1:         mov     a, r1           ; high byte into accumulator               jb      acc.7, c0a      ; negative if bit 7 is 1               clr     21H             ; clear sign bit if 'positive'               ret                     ; donec0a:           setb    21H             ; set sign flag               mov     a, r0           ; number is negative               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r0, a                mov     a, r1           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r1, a               ret;====================================================================; subroutine Cr2r3; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:    r3, r2 = signed word;; output:   r3, r2 = magnitude;           Bit 22H = sign (22H is set if negative number);; alters:   acc, C;====================================================================Cr2r3:         mov     a, r3           ; read high into accumulator               jb      acc.7, c1a      ; negative if bit 7 is 1               clr     22H             ; clear sign bit if 'positive'               ret                     ; donec1a:           setb    22H             ; set sign flag               mov     a, r2           ; number is negative               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r2, a                mov     a, r3           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r3, a               ret;====================================================================; subroutine Cr4r5; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:    r5, r4 = signed word;; output:   r5, r4 = magnitude;           Bit 22H = sign (22H is set if negative number);; alters:   acc, C;====================================================================Cr4r5:         mov     a, r5           ; read high into accumulator               jb      acc.7, c3a      ; negative if bit 7 is 1               clr     22H             ; clear sign bit if 'positive'               ret                     ; donec3a:           setb    22H             ; set sign flag               mov     a, r4           ; number is negative               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r4, a                mov     a, r5           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r5, a               ret;====================================================================; subroutine Cr0r3; 32-Bit 2's Complement -> magnitude / Sign Bit Conversion;; input:    r3, r2, r1, r0 = signed word;; output:   r3, r2, r1, r0 = magnitude;           Bit 21H = sign (21H is set if negative number);; alters:   acc;====================================================================Cr0r3:         mov     a, r3           ; read high into accumulator               jb      acc.7, c2a      ; negative if bit 7 is 1               clr     21H             ; clear sign flag if 'positive'               ret                     ; donec2a:           setb    21H             ; set sign flag               mov     a, r0           ; number is negative               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r0, a                mov     a, r1           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r1,a               mov     a, r2           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r2,a               mov     a, r3           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r3, a               ret                     ; done;==================================================================; subroutine Mr0; 8-Bit magnitude / Sign Bit -> 2's Complement Conversion;; input:    r0 = magnitude;           Bits 21H & 22H = sign bits of operands X and Y;           (set if negative);; output:   r0 = signed byte;; alters:   acc;==================================================================Mr0:           jb      21H, Mr0b       ; test X sign               jb      22H, Mr0a       ; test Y sign               retMr0b:          jnb     22H, Mr0a               retMr0a:          mov     a, r0           ; if r0 negative, get abs value               cpl     a               ; complement magnitude of X               inc     a               ; r0 = complement(r0)+1               mov     r0, a           ; save in 2's complement               ret                     ; done;====================================================================; subroutine Mr0r1; 16-Bit magnitude / Sign Bit -> 2's Complement Conversion;; input:    r1, r0 = magnitude;           Bits 21H & 22H = sign bits of operands X and Y;           (set if negative);; output:   r1, r0 = signed word;; alters:   acc, C;====================================================================Mr0r1:         jb      21H, Mr0r1b     ; test X sign               jb      22H, Mr0r1a     ; test Y sign               retMr0r1b:        jnb     22H, Mr0r1a               retMr0r1a:        mov     a, r0           ; negate number               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r0, a                mov     a, r1           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r1, a               ret;====================================================================; subroutine Mr0r3; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion;; input:    r3, r2, r1, r0 = magnitude;           Bits 21H & 22H = sign bits of operands X and Y;           (set if negative);; output:   r3, r2, r1, r0 = signed word;; alters:   acc, C;====================================================================Mr0r3:         jb      21H, Mr0r3b     ; test X sign               jb      22H, Mr0r3a     ; test Y sign               retMr0r3b:        jnb     22H, Mr0r3a               retMr0r3a:        mov     a, r0           ; negate number               cpl     a               ; complement               add     a, #1           ; and add +1               mov     r0, a                mov     a, r1           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r1, a               mov     a, r2           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r2, a               mov     a, r3           ; get next byte               cpl     a               ; complement               addc    a, #0               mov     r3, a               ret                     ; done;====================================================================; subroutine CONV816; 8-bit Signed number to 16-Bit Signed number conversion;; input:     r0 = X;; output:    r1, r0 = X with sign extended to 16 bits;; alters:    acc;====================================================================CONV816:       mov    A, r0               jnb    acc.7, Pos               mov    r1, #0FFH               retPos:           mov    r1, #0               ret;====================================================================; subroutine ADD16; 16-Bit Signed (2's Complement) Addition;; input:     r1, r0 = X;            r3, r2 = Y;; output:    r1, r0 = signed sum S = X + Y;            Carry C is set if the result (S) is out of range;; alters:    acc, C, OV;====================================================================ADD16:         anl    PSW, #0E7H       ; Register Bank 0               mov     a, r0           ; load X low byte into acc               add     a, r2           ; add Y low byte               mov     r0, a           ; put result in Z low byte               mov     a, r1           ; load X high byte into acc               addc    a, r3           ; add Y high byte with carry               mov     r1, a           ; save result in Z high byte               mov     C, OV               ret;====================================================================; subroutine ADD32; 32-Bit Signed (2's Complement) Addition;; input:     r3, r2, r1, r0 = X;            r7, r6, r5, r4 = Y;; output:    r3, r2, r1, r0 = signed sum S = X + Y;            Carry C is set if the result (S) is out of range;; alters:    acc, C, OV;====================================================================ADD32:         anl    PSW, #0E7H       ; Register Bank 0               mov     a, r0           ; load X low byte into acc               add     a, r4           ; add Y low byte               mov     r0, a           ; save result               mov     a, r1           ; load X next byte into acc               addc    a, r5           ; add Y next byte with carry               mov     r1, a           ; save result               mov     a, r2           ; load X next byte into acc               addc    a, r6           ; add Y next byte               mov     r2, a           ; save result               mov     a, r3           ; load X high byte into acc               addc    a, r7           ; add Y high byte with carry               mov     r3, a

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -