📄 imth08.asm
字号:
DEC 33T,SP ;point to next multiplicand
BPL MULTLP ;loop until each multiplicand has been
;multiplied by each multiplier
*
* Initialize temporary stack variables used in the addition process
*
TSX ;transfer stack pointer to H:X
AIX #7 ;add offset for lsb of result
STHX SPVAL ;store position of lsb
CLR 35T,SP ;clear addition carry storage
LDA #7 ;
STA 33T,SP ;store lsb position of final result
LDA #3 ;
STA 34T,SP ;store counter for number of rows
*
* add all four of the enties in each column together and store the
* final 64-bit value in locations INTACC1.....INTACC2+3.
*
OUTADDLP LDA 35T,SP ;load acc with carry
CLR 35T,SP ;clear carry
INADDLP ADD ,X ;add entry in table to accumulator
BCC ADDFIN ;check for carry
INC 35T,SP ;increment carry
ADDFIN AIX #8 ;load H:X with position of next entry
;column
DEC 34T,SP ;decrement row counter
BPL INADDLP ;loop until all four entries in column
;have been added together
CLRH ;clear h-reg
LDX #3 ;
STX 34T,SP ;reset row pointer
LDX 33T,SP ;load final result byte pointer
STA INTACC1,X ;store one byte of final result
LDHX SPVAL ;load original column pointer
AIX #-1 ;decrement column pointer
STHX SPVAL ;store new pointer value
DEC 33T,SP ;decrement final result byte pointer
BPL OUTADDLP ;loop until all eight columns have
;been added up and the final results
;stored
*
* Reset stack pointer and recover original registers values
*
AIS #35T ;deallocate local storage
PULH ;restore h-reg
PULX ;restore x-reg
PULA ;restore accumulator
RTS ;return
********************************************************************************
********************************************************************************
*
* Signed 8 x 8 Multiply
*
* This routine multiplies the signed 8-bit number stored in location
* INTACC1 by the signed 8-bit number stored in location INTACC2
* and places the signed 16-bit result in INTACC1:INTACC1+1.
*
*
SMULT8 EQU *
PSHX ;save x-reg
PSHA ;save accumulator
PSHH ;save h-reg
AIS #-1 ;reserve 2 bytes of temp. storage
CLR 1,SP ;clear storage for result sign
BRCLR 7,INTACC1,TEST2 ;check multiplier sign bit
NEG INTACC1 ;two's comp number if negative
INC 1,SP ;set sign bit for negative number
TEST2 BRCLR 7,INTACC2,SMULT ;check multiplicand sign bit
NEG INTACC2 ;two's comp number if negative
INC 1,SP ;set or clear sign bit
SMULT LDX INTACC1 ;load x-reg with multiplier
LDA INTACC2 ;load acc with multiplicand
MUL ;multiply
STA INTACC1+1 ;store result lsb
STX INTACC1 ;store result msb
LDA 1,SP ;load sign bit
CMP #1 ;check for negative
BNE RETURN ;branch to finish if result is positive
NEG INTACC1+1 ;two's comp result lsb
BCC NOSUB ;check for borrow from zero
NEG INTACC1 ;two's comp result msb
DEC INTACC1 ;decrement result msb for borrow
BRA RETURN ;finished
NOSUB NEG INTACC1 ;two's comp result msb without decrement
RETURN AIS #1 ;deallocate temp storage
PULH ;restore h-reg
PULA ;restore accumulator
PULX ;restore x-reg
RTS ;return
********************************************************************************
********************************************************************************
*
* Signed 16 x 16 multiply
*
* This routine multiplies the signed 16-bit number in INTACC1:INTACC1+1 by
* the signed 16-bit number in INTACC2:INTACC2+1 and places the signed 32-bit
* value in locations INTACC1....INTACC1+3 (INTACC1 = MSB...INTACC1+3 = LSB).
*
*
SMULT16 EQU *
PSHX ;save x-reg
PSHA ;save accumulator
PSHH ;save h-reg
AIS #-1 ;reserve 1 byte of temp. storage
CLR 1,SP ;clear storage for result sign
BRCLR 7,INTACC1,TST2 ;check multiplier sign bit and negate
;(two's complement) if set
NEG INTACC1+1 ;two's comp multiplier lsb
BCC NOSUB1 ;check for borrow from zero
NEG INTACC1 ;two's comp multiplier msb
DEC INTACC1 ;decrement msb for borrow
BRA MPRSIGN ;finished
NOSUB1 NEG INTACC1 ;two's comp multiplier msb (no borrow)
MPRSIGN INC 1,SP ;set sign bit for negative number
TST2 BRCLR 7,INTACC2,MLTSUB ;check multiplicand sign bit and negate
;(two's complement) if set
NEG INTACC2+1 ;two's comp multiplicand lsb
BCC NOSUB2 ;check for borrow from zero
NEG INTACC2 ;two's comp multiplicand msb
DEC INTACC2 ;decrement msb for borrow
BRA MPCSIGN ;finished
NOSUB2 NEG INTACC2 ;two's comp multiplicand msb (no borrow)
MPCSIGN INC 1,SP ;set or clear sign bit
MLTSUB JSR UMULT16 ;multiply INTACC1 by INTACC2
LDA 1,SP ;load sign bit
CMP #1 ;check for negative
BNE DONE ;exit if answer is positive,
;otherwise two's complement result
LDX #3 ;
COMP COM INTACC1,X ;complement a byte of the result
DECX ;point to next byte to be complemented
BPL COMP ;loop until all four bytes of result
;have been complemented
LDA INTACC1+3 ;get result lsb
ADD #1 ;add a "1" for two's comp
STA INTACC1+3 ;store new value
LDX #2 ;
TWSCMP LDA INTACC1,X ;add any carry from the previous
ADC #0 ; addition to the next three bytes
STA INTACC1,X ; of the result and store the new
DECX ; values
BPL TWSCMP ;
DONE AIS #1 ;deallocate temp storage on stack
PULH ;restore h-reg
PULA ;restore accumulator
PULX ;restore x-reg
RTS ;return
********************************************************************************
********************************************************************************
*
* 32 x 16 Unsigned Divide
*
* This routine takes the 32-bit dividend stored in INTACC1.....INTACC1+3
* and divides it by the 16-bit divisor stored in INTACC2:INTACC2+1.
* The quotient replaces the dividend and the remainder replaces the divisor.
*
UDVD32 EQU *
*
DIVIDEND EQU INTACC1+2
DIVISOR EQU INTACC2
QUOTIENT EQU INTACC1
REMAINDER EQU INTACC1
*
PSHH ;save h-reg value
PSHA ;save accumulator
PSHX ;save x-reg value
AIS #-3 ;reserve three bytes of temp storage
LDA #!32 ;
STA 3,SP ;loop counter for number of shifts
LDA DIVISOR ;get divisor msb
STA 1,SP ;put divisor msb in working storage
LDA DIVISOR+1 ;get divisor lsb
STA 2,SP ;put divisor lsb in working storage
*
* Shift all four bytes of dividend 16 bits to the right and clear
* both bytes of the temporary remainder location
*
MOV DIVIDEND+1,DIVIDEND+3 ;shift dividend lsb
MOV DIVIDEND,DIVIDEND+2 ;shift 2nd byte of dividend
MOV DIVIDEND-1,DIVIDEND+1 ;shift 3rd byte of dividend
MOV DIVIDEND-2,DIVIDEND ;shift dividend msb
CLR REMAINDER ;zero remainder msb
CLR REMAINDER+1 ;zero remainder lsb
*
* Shift each byte of dividend and remainder one bit to the left
*
SHFTLP LDA REMAINDER ;get remainder msb
ROLA ;shift remainder msb into carry
ROL DIVIDEND+3 ;shift dividend lsb
ROL DIVIDEND+2 ;shift 2nd byte of dividend
ROL DIVIDEND+1 ;shift 3rd byte of dividend
ROL DIVIDEND ;shift dividend msb
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -