📄 math8051.asm
字号:
mov C, OV ret;====================================================================; subroutine SUB16; 16-Bit Signed (2's Complement) Subtraction;; input: r1, r0 = X; r3, r2 = Y;; output: r1, r0 = signed difference D = X - Y; Carry C is set if the result (D) is out of range.;; alters: acc, C, OV;====================================================================SUB16: anl PSW, #0E7H ; Register Bank 0 mov a, r0 ; load X low byte into acc clr C ; clear carry flag subb a, r2 ; subract Y low byte mov r0, a ; put result in Z low byte mov a, r1 ; load X high into accumulator subb a, r3 ; subtract Y high with borrow mov r1, a ; save result in Z high byte mov C, OV ret;====================================================================; subroutine SUB32; 32-Bit Signed (2's Complement) subtraction;; input: r3, r2, r1, r0 = X; r7, r6, r5, r4 = Y;; output: r3, r2, r1, r0 = signed difference D = X - Y; Carry C is set if the result (D) is out of range.;; alters: acc, C, OV;====================================================================SUB32: anl PSW, #0E7H ; Register Bank 0 mov a, r0 ; load X low byte into acc clr C ; clear carry flag subb a, r4 ; subract Y low byte mov r0, a ; put result in Z low byte mov a, r1 ; repeat with other bytes... subb a, r5 mov r1, a mov a, r2 subb a, r6 mov r2, a mov a, r3 subb a, r7 mov r3, a mov C, OV ; set C if external borrow ret;==================================================================; subroutine MUL8; 8-Bit x 8-Bit to 16-Bit Product Signed Multiply; 2's Complement format;; input: r0 = multiplicand X; r1 = multiplier Y;; output: r1, r0 = product P = X x Y.; ; calls: UMUL8, Cr0, Cr1, Mr0r1;; alters: acc, C, Bits 21H & 22H;==================================================================MUL8: anl PSW, #0E7H ; Register Bank 0 acall Cr0 ; 2's comp -> Mag/Sign acall Cr1 ; 2's comp -> Mag/Sign acall UMUL8 acall Mr0r1 ; Mag/Sign -> 2's Comp ret;==================================================================; subroutine UMUL8; 8-Bit x 8-Bit to 16-Bit Product Unsigned Multiply;; input: r0 = multiplicand X; r1 = multiplier Y;; output: r1, r0 = product P = X x Y.;; alters: acc;==================================================================UMUL8: push b mov a, r0 ; read X and ... mov b, r1 ; ... Y mul ab ; multiply X and Y mov r1, b ; save result high ... mov r0, a ; ... and low pop b ret;====================================================================; subroutine MUL816; 8-Bit x 16-Bit to 32-Bit Product signed Multiply; 2's Complement format;; input: r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y (r3 = sign extension);; calls: Cr0, Cr2r3, Mr0r3;; alters: acc, C, Bits 21H & 22H;====================================================================MUL816: push b anl PSW, #0E7H ; Register Bank 0 acall Cr0 ; 2's comp -> Mag/Sign acall Cr2r3 ; 2's comp -> Mag/Sign mov a, r0 ; load X low byte into acc mov b, r2 ; load Y low byte into B mul ab ; multiply push acc ; stack result low byte push b ; stack result high byte mov a, r0 ; load X into acc again mov b, r3 ; load Y high byte into B mul ab ; multiply pop 00H ; recall X*YL high byte add a, r0 ; add X*YL high and X*YH low mov r1, a ; save result clr a ; clear accumulator addc a, b ; a = b + carry flag mov r2, a ; save result pop 00H ; get low result mov r3, #0 acall Mr0r3 ; Mag/Sign -> 2's Comp pop b ret;====================================================================; subroutine MUL16; 16-Bit x 16-Bit to 32-Bit Product Signed Multiply; 2's Complement format;; input: r1, r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y;; calls: UMUL16, Cr0r1, Cr2r3, Mr0r3;; alters: acc, C, Bits 21H & 22H;====================================================================MUL16: anl PSW, #0E7H ; Register Bank 0 acall Cr0r1 ; 2's comp -> Mag/Sign acall Cr2r3 ; 2's comp -> Mag/Sign acall UMUL16 acall Mr0r3 ; Mag/Sign -> 2's Comp ret;====================================================================; subroutine UMUL16; 16-Bit x 16-Bit to 32-Bit Product Unsigned Multiply;; input: r1, r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y;; alters: acc, C;====================================================================UMUL16: push B push dpl mov a, r0 mov b, r2 mul ab ; multiply XL x YL push acc ; stack result low byte push b ; stack result high byte mov a, r0 mov b, r3 mul ab ; multiply XL x YH pop 00H add a, r0 mov r0, a clr a addc a, b mov dpl, a mov a, r2 mov b, r1 mul ab ; multiply XH x YL add a, r0 mov r0, a mov a, dpl addc a, b mov dpl, a clr a addc a, #0 push acc ; save intermediate carry mov a, r3 mov b, r1 mul ab ; multiply XH x YH add a, dpl mov r2, a pop acc ; retrieve carry addc a, b mov r3, a mov r1, 00H pop 00H ; retrieve result low byte pop dpl pop B ret;====================================================================; subroutine MAC16; 16-Bit x 16-Bit to 32-Bit Product signed Multiply-Accumulate; 2's Complement format;; input: r1, r0 = multiplicand X; r3, r2 = multiplier Y; r7, r6, r5, r4 = 32-bit accumulator Ar;; output: r7, r6, r5, r4 = accumulated result Ar = Ar + (X x Y); r3, r2, r1, r0 = multiply result M = X x Y; Carry C set if overflow;; calls: MUL16;; alters: acc, C, Bits 21H & 22H;====================================================================MAC16: anl PSW, #0E7H ; Register Bank 0 acall MUL16+3 mov A, r4 add A, r0 mov r4, A mov A, r5 addc A, r1 mov r5, A mov A, r6 addc A, r2 mov r6, A mov A, r7 addc A, r3 mov r7, A mov C, OV ret;===============================================================; subroutine DIV8; 8-Bit / 8-Bit to 8-Bit Quotient & Remainder signed Divide; 2's Complement Format;; input: r0 = Dividend X; r1 = Divisor Y;; output: r0 = quotient Q of division Q = X / Y; r1 = remainder ; ; calls: Cr0, Cr1, Mr0;; alters: acc, C, Bits 21H & 22H;===============================================================DIV8: anl PSW, #0E7H ; Register Bank 0 acall Cr0 ; 2's comp -> Mag/Sign acall Cr1 ; 2's comp -> Mag/Sign acall UDIV8 acall Mr0 ; Mag/Sign -> 2's Comp ret;===============================================================; subroutine UDIV8; 8-Bit / 8-Bit to 8-Bit Quotient & Remainder Unsigned Divide;; input: r0 = Dividend X; r1 = Divisor Y;; output: r0 = quotient Q of division Q = X / Y; r1 = remainder ; ;; alters: acc, C;===============================================================UDIV8: push b mov a, r0 ; read X and ... mov b, r1 ; ... Y div ab ; divide X and Y mov r0, a ; save result quotient mov r1, b ; save remainder pop b ret;====================================================================; subroutine DIV16; 16-Bit / 16-Bit to 16-Bit Quotient & remainder signed Divide; 2's Complement Format;; input: r1, r0 = Dividend X; r3, r2 = Divisor Y;; output: r1, r0 = quotient Q of division Q = X / Y; r3, r2 = remainder ; Carry C is set if Y = 0, i.e. divide by 0 attempted;; calls: UDIV16, Cr0r1, Cr2r3, Mr0r1;; alters: acc, r4, r5, r6, r7, flags, Bits 21H & 22H;====================================================================DIV16: anl PSW, #0E7H ; Register Bank 0 mov a, r3 ; get divisor high byte orl a, r2 ; OR with low byte jnz div_OK ; divisor OK if not 0 setb C ; else, overflow retdiv_OK: push dpl push dph push b acall Cr0r1 ; 2's comp -> Mag/Sign acall Cr2r3 ; 2's comp -> Mag/Sign acall UDIV16 acall Mr0r1 ; Mag/Sign -> 2's Comp clr C pop b pop dph pop dpl ret ; done;====================================================================; subroutine UDIV16; 16-Bit / 16-Bit to 16-Bit Quotient & Remainder Unsigned Divide;; input: r1, r0 = Dividend X; r3, r2 = Divisor Y
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -