permisi, mau minta tolong.
Barang kali ada yang punya subroutin perkalian 8 bit dengan 16 bit yang
mana hasil lebar data maksimalnya mampu 32 bit (8 bit x 16 bit = 32
bit)
saya udah coba nyari2 di internet dan menemukan beberapa cuma hasilnya
kurang memuaskan (tidak benar) .
contoh ini:
;====================================================================
; 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 SignX & SignY
;====================================================================
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 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 SignX ; clear sign bit if 'positive'
ret ; done
Cr0a: cpl a ; X negative, find abs value
inc a ; XA = complement(XT)+1
mov r0, a ; save magnitude
setb SignX ; set sign bit if 'negative'
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 SignY ; clear sign bit if 'positive'
ret ; done
c1a: setb SignY ; 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 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 SignX, Mr0r3b ; test X sign
jb SignY, Mr0r3a ; test Y sign
ret
Mr0r3b: jnb SignY, Mr0r3a
ret
Mr0r3a: 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
nah ini revisi yang agak baru dari sang penulis program (W.G.Marshall)
tapi tetep masih belum benar juga.
;====================================================================
; 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, r4, Bits SignX & SignY
;====================================================================
MUL816: push b
acall Cr0 ; 2's comp -> Mag/Sign
acall Cr2r3 ; 2's comp -> Mag/Sign
mov a, r0 ; load X into acc
mov b, r2 ; load Y low into B
mul ab ; multiply
xch a, r0 ; save X*YL low, reload X
mov r4, b ; temp save X*YL high
mov b, r3 ; load Y high into B
mul ab ; multiply
add a, r4 ; add X*YL high and X*YH low
mov r1, a ; save result
clr a
addc a, b ; X*YH high + 0 + carry flag
mov r2, a ; save result
mov r3, #0 ; MS result = 0
acall Mr0r3 ; Mag/Sign -> 2's Comp
pop b
ret
;=================================================================
; subroutine Cr0
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r0 = signed byte
;
; output: r0 = magnitude
; Bit SignX = sign (SignX is set if r0 is a negative number)
;
; alters:
;=================================================================
Cr0: xch a, r0 ; read X into accumulator
clr SignX ; clear sign flag
jnb acc.7, Cr0a ; X is negative if bit 7 is 1
cpl a ; X negative, find abs value
inc a ; X = complement(X)+1
setb SignX ; set sign flag
Cr0a: xch a, r0 ; save magnitude
ret
;====================================================================
; subroutine Cr2r3
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r3, r2 = signed word
;
; output: r3, r2 = magnitude
; Bit SignY = sign (SignY is set if negative number)
;
; alters: acc, C
;====================================================================
Cr2r3: mov a, r3
clr SignY ; clear sign flag
jnb acc.7, C1a ; high byte negative?
setb SignY ; set sign flag
xch a, r2 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
xch a, r2 ; get MS byte
cpl a ; complement
addc a, #0
mov r3, a
C1a: ret
;====================================================================
; subroutine Mr0r3
; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion
;
; input: r3, r2, r1, r0 = magnitude
; Bits SignX & SignY = sign bits of operands X and Y
; (set if negative)
;
; output: r3, r2, r1, r0 = signed word
;
; alters: acc, C
;====================================================================
Mr0r3: jb SignX, Mr0r3b ; test X sign
jb SignY, Mr0r3a ; test Y sign
ret
Mr0r3b: jnb SignY, Mr0r3a
ret
Mr0r3a: 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
terima kasih
Salam